Mercurial > hg > Database > Alice
view src/alice/test/reflection/ReflectionTest.java @ 290:4074077d118d
Refactor iterating process with for-each sentence.
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 18 Oct 2013 03:26:38 +0900 |
parents | 16bfc302bff2 |
children |
line wrap: on
line source
package alice.test.reflection; import javassist.ClassPool; import javassist.CtClass; import javassist.CtConstructor; import javassist.CtField; import javassist.CtMethod; public class ReflectionTest { public static void main(String[] args){ try { new ReflectionTest().createCS(); } catch (Exception e) { e.printStackTrace(); } System.out.println("TEST END"); } public void createCS() throws Exception{ ClassPool cp = ClassPool.getDefault(); CtClass tep = cp.get("alice.test.reflection.CSTemplate"); CtClass rec = cp.get("alice.datasegment.Receiver"); // ---- define Field ----- //CtField f1 = CtField.make("alice.datasegment.Receiver arg1 = ids.create(alice.datasegment.CommandType.PEEK);", tep); //tep.addField(f1); CtField f1 = new CtField(rec,"arg1",tep); tep.addField(f1,"ids.create(alice.datasegment.CommandType.PEEK)"); // ---- define Constructor ----- CtConstructor c = tep.getConstructor("()V"); c.insertAfter("arg1.setKey(\"key1\");"); // ---- define run ----- CtMethod m = tep.getMethod("run", "()V"); m.insertAfter("System.out.println(\"type = \" + arg1.type);"); m.insertAfter("System.out.println(\"index = \" + arg1.index);"); m.insertAfter("System.out.println(\"data = \" + arg1.getVal());"); m.insertAfter("System.out.println(((org.msgpack.type.Value)arg1.getVal()).getType());"); m.insertAfter("if (arg1.index==10){ System.exit(0);}"); m.insertAfter("new alice.test.reflection.CSTemplate();"); m.insertAfter("ods.update(\"key1\", \"String Data\");"); Class<?> clazz = tep.toClass(); // load Class @SuppressWarnings("unused") CSTemplate cs = (CSTemplate)clazz.newInstance(); // create instance } }