blob: 2e6f5c3d3e36876beec4d2b75080d5164c5f114f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import javassist.*;
import javassist.bytecode.*;
import javassist.bytecode.annotation.*;
@interface Entity {}
@interface Table { String[] textValues() default {}; }
public class Test {
public static void main(String[] args) throws Exception {
ClassPool classPool = ClassPool.getDefault();
ClassFile cf = classPool.makeClass("TestSub").getClassFile();
ConstPool constPool = cf.getConstPool();
Annotation[] annotations = new Annotation[2];
AnnotationsAttribute attrib =
new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation annotation = new Annotation(constPool, classPool.get("Entity"));
annotations[0] = annotation;
// Add @Table(name="",schema="") to class
annotation = new Annotation(constPool, classPool.get("Table"));
annotation.addMemberValue("name", new StringMemberValue("name", constPool));
annotation.addMemberValue("schema", new StringMemberValue("schema", constPool));
// ArrayMemberValue blankMemberValueArray = new ArrayMemberValue(new AnnotationMemberValue(constPool), constPool);
// blankMemberValueArray.setValue(new MemberValue[0]);
// annotation.addMemberValue("textValues", blankMemberValueArray);
annotations[1] = annotation;
attrib.setAnnotations(annotations);
cf.addAttribute(attrib);
System.out.println("done");
}
}
|