annotate src/main/java/alice/test/reflection/ReflectionTest.java @ 655:1c93e82e05c6 default tip

fix timestamp
author suruga
date Sat, 17 Feb 2018 00:33:00 +0900
parents 6e304a7a60e7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
1 package alice.test.reflection;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
2
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
3 import javassist.ClassPool;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
4 import javassist.CtClass;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
5 import javassist.CtConstructor;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
6 import javassist.CtField;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
7 import javassist.CtMethod;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
8
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
9 public class ReflectionTest {
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
10
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
11 public static void main(String[] args){
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
12 try {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
13 new ReflectionTest().createCS();
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
14 } catch (Exception e) {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
15 e.printStackTrace();
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
16 }
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
17 System.out.println("TEST END");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
18 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
19
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
20 public void createCS() throws Exception{
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
21
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
22 ClassPool cp = ClassPool.getDefault();
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
23 CtClass tep = cp.get("alice.test.reflection.CSTemplate");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
24 CtClass rec = cp.get("alice.datasegment.Receiver");
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
25
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
26 // ---- define Field -----
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
27 //CtField f1 = CtField.make("alice.datasegment.Receiver arg1 = ids.create(alice.datasegment.CommandType.PEEK);", tep);
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
28 //tep.addField(f1);
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
29 CtField f1 = new CtField(rec,"arg1",tep);
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
30 tep.addField(f1,"ids.create(alice.datasegment.CommandType.PEEK)");
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
31
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
32 // ---- define Constructor -----
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
33 CtConstructor c = tep.getConstructor("()V");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
34 c.insertAfter("arg1.setKey(\"key1\");");
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
35
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
36 // ---- define run -----
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
37 CtMethod m = tep.getMethod("run", "()V");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
38 m.insertAfter("System.out.println(\"type = \" + arg1.type);");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
39 m.insertAfter("System.out.println(\"index = \" + arg1.index);");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
40 m.insertAfter("System.out.println(\"data = \" + arg1.getVal());");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
41 m.insertAfter("System.out.println(((org.msgpack.type.Value)arg1.getVal()).getType());");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
42 m.insertAfter("if (arg1.index==10){ System.exit(0);}");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
43 m.insertAfter("new alice.test.reflection.CSTemplate();");
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
44 m.insertAfter("ods.update(\"key1\", \"String Data\");");
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
45
467
6e304a7a60e7 remove white space
sugi
parents: 419
diff changeset
46 Class<?> clazz = tep.toClass(); // load Class
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
47 @SuppressWarnings("unused")
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
48 CSTemplate cs = (CSTemplate)clazz.newInstance(); // create instance
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
49
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
50 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
51 }