Mercurial > hg > Database > Alice
view src/main/java/alice/codesegment/CreateCodeSegment.java @ 633:746447b7c3e0
add CreateCodeSegment Factory
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 01 Dec 2017 01:10:03 +0900 |
parents | |
children |
line wrap: on
line source
package alice.codesegment; import alice.Annotation.AliceAnnotation; import alice.Annotation.Take; import alice.datasegment.CommandType; import alice.datasegment.Receiver; import javassist.*; import javassist.tools.reflect.Sample; import sun.jvm.hotspot.oops.Instance; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.security.ProtectionDomain; /** * Created by e125769 on 11/30/17. */ public class CreateCodeSegment { private Class cs; public CreateCodeSegment(Class cs){ this.cs = cs; ClassPool cp = ClassPool.getDefault(); cp.appendSystemPath(); CtClass cc = null; try { cc = cp.get(cs.getName()); } catch (NotFoundException e) { e.printStackTrace(); } for (Field field : cs.getDeclaredFields()) { if (field.isAnnotationPresent(Take.class)){ System.out.println(field.getName()); System.out.println(field.getType()); Take ano = field.getAnnotation(Take.class); System.out.println(ano.value()); try { //Receiverを生成 CtField cf = CtField.make("public alice.datasegment.Receiver " + ano.value() + " = ids.create(alice.datasegment.CommandType.TAKE);",cc); cc.addField(cf); //setKey for (CtConstructor ctConstructors : cc.getConstructors()){ ctConstructors.setBody(ano.value() + ".setKey(\"" +ano.value() + "\");"); } //cc.setModifiers(cc.getModifiers()); } catch (CannotCompileException e) { e.printStackTrace(); } } } try { ClassLoader loader = ClassLoader.getSystemClassLoader(); ProtectionDomain domain = Sample.class.getProtectionDomain(); this.cs = cc.toClass(loader, domain);//ここで止まる。toClass()以前にクラスを1つでもloadしていたら動かない(怒) } catch (CannotCompileException e) { e.printStackTrace(); } } public void run(){ try { cs.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }