Mercurial > hg > Database > Alice
changeset 412:c9dd5ca304fc dispose
reflection test
author | sugi |
---|---|
date | Mon, 07 Jul 2014 16:20:33 +0900 |
parents | e501afa00369 |
children | b7a211b3f3e0 |
files | .project src/main/java/alice/daemon/dispose/DisposeManager.java src/main/java/alice/test/reflection/CloseEventManager.java src/main/java/alice/test/reflection/DisposeManagerTest.java src/main/java/alice/test/reflection/MetaCodeSegment.java |
diffstat | 5 files changed, 226 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/.project Sat Jul 05 16:10:57 2014 +0900 +++ b/.project Mon Jul 07 16:20:33 2014 +0900 @@ -15,4 +15,15 @@ <nature>org.springsource.ide.eclipse.gradle.core.nature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> + <filteredResources> + <filter> + <id>1404553678816</id> + <name></name> + <type>30</type> + <matcher> + <id>org.eclipse.ui.ide.multiFilter</id> + <arguments>1.0-projectRelativePath-matches-false-false-build</arguments> + </matcher> + </filter> + </filteredResources> </projectDescription>
--- a/src/main/java/alice/daemon/dispose/DisposeManager.java Sat Jul 05 16:10:57 2014 +0900 +++ b/src/main/java/alice/daemon/dispose/DisposeManager.java Mon Jul 07 16:20:33 2014 +0900 @@ -1,22 +1,80 @@ package alice.daemon.dispose; -import alice.codesegment.CodeSegment; +import javassist.CannotCompileException; +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtField; +import javassist.CtNewConstructor; +import javassist.NotFoundException; -public class DisposeManager extends CodeSegment{ +public class DisposeManager { private static DisposeManager instance = new DisposeManager(); - + private ClassPool cp = ClassPool.getDefault(); + private DisposeManager(){} + public static DisposeManager getInstance(){ return instance; } - @Override - public void run() { - - - } - - public synchronized void setCodeSegment(String managerKey, CodeSegment cs){ - ods.put("_DISPOSE"+managerKey, cs); + public <T> void setCodeSegment(String managerKey, Class<T> clazz){ + CtClass cs = null; + String metaName = "Meta"+clazz.getSimpleName(); + try { + // if cannot get mataName Class go catch NotFoundException + cs = cp.get(metaName); + // already created + Class<?> metaClazz = cp.getClassLoader().loadClass(metaName); + metaClazz.newInstance(); + } catch (ClassNotFoundException e) { + System.out.println("ClassLoder loadClass error"); + e.printStackTrace(); + } catch (InstantiationException e) { + System.out.println("newInstance() error"); + } catch (IllegalAccessException e) { + System.out.println("newInstance() error"); + } catch (NotFoundException e) { + try { + CtClass parent = cp.get(clazz.getName()); + + // check super class is CS + boolean checkSuperClass = parent.getSuperclass().getSimpleName().equals("CodeSegment"); + if (!checkSuperClass) { + // argument wrong + return; + } else { + cs = cp.makeClass("Meta"+clazz.getSimpleName()); + cs.setSuperclass(parent); + + // define meta inputDataSegment + CtClass receiver = cp.get("alice.datasegment.Receiver"); + + // need check exist name + String metaFieldName = "metaInfo"; + + CtField cf = new CtField(receiver ,metaFieldName, cs); + cs.addField(cf, "ids.create(alice.datasegment.CommandType.PEEK)"); + + String cnst = "public "+metaName+"(){" + + metaFieldName+".setKey(\"TEST\");" + + "}"; + + cs.addConstructor(CtNewConstructor.make(cnst, cs)); + + Class<?> metaClass = cs.toClass(); + metaClass.newInstance(); + } + } catch (NotFoundException e1) { + System.out.println("not found super Class"); + return; + } catch (CannotCompileException e1) { + e1.printStackTrace(); + } catch (InstantiationException e1) { + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + e1.printStackTrace(); + } + } + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/reflection/CloseEventManager.java Mon Jul 07 16:20:33 2014 +0900 @@ -0,0 +1,93 @@ +package alice.test.reflection; + +import javassist.CannotCompileException; +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtConstructor; +import javassist.CtField; +import javassist.NotFoundException; + + +public class CloseEventManager { + public static CloseEventManager closeManager = new CloseEventManager(); + + private CloseEventManager(){} + public static CloseEventManager getInstance(){ + return closeManager; + } + private ClassPool cp = ClassPool.getDefault(); + + public <T> void setCodeSegment(String managerKey, Class<T> clazz){ + System.out.println("convert class name "+clazz.getName()); + CtClass cs = null; + String metaName = clazz.getSimpleName()+"Meta"; + try { + // if cannot get mataName Class go catch NotFoundException + cs = cp.get(clazz.getName()+"Meta"); + // already created + Class<?> metaClazz = cp.getClassLoader().loadClass(metaName); + metaClazz.newInstance(); + } catch (ClassNotFoundException e) { + System.out.println("ClassLoder loadClass error"); + e.printStackTrace(); + } catch (InstantiationException e) { + System.out.println("newInstance() error"); + } catch (IllegalAccessException e) { + System.out.println("newInstance() error"); + } catch (NotFoundException e) { + try { + CtClass parent = cp.get(clazz.getName()); + + // check super class is CS + boolean checkSuperClass = parent.getSuperclass().getSimpleName().equals("CodeSegment"); + if (!checkSuperClass) { + // argument wrong + return; + } else { + cs = cp.get(clazz.getName()); + + // define meta inputDataSegment + CtClass receiver = cp.get("alice.datasegment.Receiver"); + + // need check exist field name + String metaFieldName = "metaInfo"; + CtField cf = new CtField(receiver ,metaFieldName, cs); + cs.addField(cf, "ids.create(alice.datasegment.CommandType.TAKE)"); + + CtConstructor[] ccArray = cs.getConstructors(); + CtConstructor cc = null; + for (int i = 0; i < ccArray.length; i++){ + if (ccArray[i].getName().equals(clazz.getSimpleName())) + cc = ccArray[i]; + } + String metaSetKey = metaFieldName+".setKey(\"TEST\");"; + + String meta = "System.out.println(\"test\");"; + String meta2 = "System.out.println(list.size());"; + System.out.println(cc.isConstructor()); + cc.insertAfter(metaSetKey); + cc.insertAfter(meta); + cc.insertAfter(meta2); + + cs.setName(metaName); + + Class<?> metaClass = cs.toClass(); + cp.get(cs.getName()); + System.out.println(cs.getName()); + metaClass.newInstance(); + } + } catch (NotFoundException e1) { + System.out.println("not found super Class"); + return; + } catch (CannotCompileException e1) { + e1.printStackTrace(); + } catch (InstantiationException e1) { + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + e1.printStackTrace(); + } + } + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/reflection/DisposeManagerTest.java Mon Jul 07 16:20:33 2014 +0900 @@ -0,0 +1,17 @@ +package alice.test.reflection; + +import alice.daemon.AliceDaemon; +import alice.daemon.Config; + +public class DisposeManagerTest { + public static void main(String[] args){ + new AliceDaemon(new Config(args)).listen(); // logger off + CSTemplate cs = new CSTemplate(); + cs.ods.put("key1", "String data"); + cs.ods.put("TEST", "TEST"); + //DisposeManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); + //CloseEventManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); + CloseEventManager.getInstance().setCodeSegment("local", alice.test.reflection.MetaCodeSegment.class); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/reflection/MetaCodeSegment.java Mon Jul 07 16:20:33 2014 +0900 @@ -0,0 +1,36 @@ +package alice.test.reflection; + +import org.msgpack.type.Value; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +public class MetaCodeSegment extends CodeSegment { + + // create input datasegment arg1 + Receiver arg1 = ids.create(CommandType.TAKE); + + public MetaCodeSegment(){ + arg1.setKey("key1", arg1.index); + } + + @Override + public void run() { + System.out.println("type = " + arg1.type); + System.out.println("index = " + arg1.index); + System.out.println("data = " + arg1.getVal()); + System.out.println(((Value)arg1.getVal()).getType()); + + if (arg1.index == 10) { + System.exit(0); + return; + } + System.out.println(MetaCodeSegment.class); + //DisposeManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); + CloseEventManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); + // DataSegment.get("local").update + //ods.update("local", "key1", "String data"); + } + +} \ No newline at end of file