Mercurial > hg > Database > Alice
changeset 413:b7a211b3f3e0 dispose
move other package
author | sugi |
---|---|
date | Mon, 07 Jul 2014 16:21:11 +0900 |
parents | c9dd5ca304fc |
children | b42eee3348b2 |
files | src/main/java/alice/daemon/dispose/DisposeManager.java src/main/java/alice/test/reflection/DisposeManager.java |
diffstat | 2 files changed, 80 insertions(+), 80 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/daemon/dispose/DisposeManager.java Mon Jul 07 16:20:33 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -package alice.daemon.dispose; - -import javassist.CannotCompileException; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtField; -import javassist.CtNewConstructor; -import javassist.NotFoundException; - -public class DisposeManager { - private static DisposeManager instance = new DisposeManager(); - private ClassPool cp = ClassPool.getDefault(); - - private DisposeManager(){} - - public static DisposeManager getInstance(){ - return instance; - } - - 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/DisposeManager.java Mon Jul 07 16:21:11 2014 +0900 @@ -0,0 +1,80 @@ +package alice.test.reflection; + +import javassist.CannotCompileException; +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtField; +import javassist.CtNewConstructor; +import javassist.NotFoundException; + +public class DisposeManager { + private static DisposeManager instance = new DisposeManager(); + private ClassPool cp = ClassPool.getDefault(); + + private DisposeManager(){} + + public static DisposeManager getInstance(){ + return instance; + } + + 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(); + } + } + + } +}