345
|
1 package alice.test.reflection;
|
|
2
|
|
3 import javassist.ClassPool;
|
|
4 import javassist.CtClass;
|
|
5 import javassist.CtConstructor;
|
|
6 import javassist.CtField;
|
|
7 import javassist.CtMethod;
|
|
8
|
|
9 public class ReflectionTest {
|
|
10
|
419
|
11 public static void main(String[] args){
|
|
12 try {
|
|
13 new ReflectionTest().createCS();
|
|
14 } catch (Exception e) {
|
|
15 e.printStackTrace();
|
|
16 }
|
|
17 System.out.println("TEST END");
|
|
18 }
|
345
|
19
|
419
|
20 public void createCS() throws Exception{
|
|
21
|
|
22 ClassPool cp = ClassPool.getDefault();
|
|
23 CtClass tep = cp.get("alice.test.reflection.CSTemplate");
|
|
24 CtClass rec = cp.get("alice.datasegment.Receiver");
|
345
|
25
|
419
|
26 // ---- define Field -----
|
|
27 //CtField f1 = CtField.make("alice.datasegment.Receiver arg1 = ids.create(alice.datasegment.CommandType.PEEK);", tep);
|
|
28 //tep.addField(f1);
|
|
29 CtField f1 = new CtField(rec,"arg1",tep);
|
|
30 tep.addField(f1,"ids.create(alice.datasegment.CommandType.PEEK)");
|
345
|
31
|
419
|
32 // ---- define Constructor -----
|
|
33 CtConstructor c = tep.getConstructor("()V");
|
|
34 c.insertAfter("arg1.setKey(\"key1\");");
|
345
|
35
|
419
|
36 // ---- define run -----
|
|
37 CtMethod m = tep.getMethod("run", "()V");
|
|
38 m.insertAfter("System.out.println(\"type = \" + arg1.type);");
|
|
39 m.insertAfter("System.out.println(\"index = \" + arg1.index);");
|
|
40 m.insertAfter("System.out.println(\"data = \" + arg1.getVal());");
|
|
41 m.insertAfter("System.out.println(((org.msgpack.type.Value)arg1.getVal()).getType());");
|
|
42 m.insertAfter("if (arg1.index==10){ System.exit(0);}");
|
|
43 m.insertAfter("new alice.test.reflection.CSTemplate();");
|
|
44 m.insertAfter("ods.update(\"key1\", \"String Data\");");
|
345
|
45
|
419
|
46 Class<?> clazz = tep.toClass(); // load Class
|
|
47 @SuppressWarnings("unused")
|
|
48 CSTemplate cs = (CSTemplate)clazz.newInstance(); // create instance
|
|
49
|
|
50 }
|
345
|
51 }
|