Mercurial > hg > Database > Alice
changeset 418:a269a019f5f8 dispose
reflection test
author | sugi |
---|---|
date | Tue, 15 Jul 2014 06:15:53 +0900 |
parents | aadea6a59376 |
children | aefbe41fcf12 |
files | 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 | 3 files changed, 78 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/test/reflection/CloseEventManager.java Tue Jul 15 00:06:10 2014 +0900 +++ b/src/main/java/alice/test/reflection/CloseEventManager.java Tue Jul 15 06:15:53 2014 +0900 @@ -9,85 +9,80 @@ 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()); + 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){ + + 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.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(); - } - } - } + // 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.PEEK)"); + + 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]; + break; + } + + } + String metaSetKey = metaFieldName+".setKey(\"TEST\");"; - + cc.insertAfter(metaSetKey); + cs.setName(metaName); + + 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(); + } + } + } + + }
--- a/src/main/java/alice/test/reflection/DisposeManagerTest.java Tue Jul 15 00:06:10 2014 +0900 +++ b/src/main/java/alice/test/reflection/DisposeManagerTest.java Tue Jul 15 06:15:53 2014 +0900 @@ -9,9 +9,10 @@ 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", MetaCodeSegment.class); - DisposeManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); + //DisposeManager.getInstance().setCodeSegment("local", MetaCodeSegment.class); } }
--- a/src/main/java/alice/test/reflection/MetaCodeSegment.java Tue Jul 15 00:06:10 2014 +0900 +++ b/src/main/java/alice/test/reflection/MetaCodeSegment.java Tue Jul 15 06:15:53 2014 +0900 @@ -21,13 +21,12 @@ System.out.println("index = " + arg1.index); System.out.println("data = " + arg1.getVal()); System.out.println(((Value)arg1.getVal()).getType()); - System.out.println(MetaCodeSegment.class.getName()); + System.out.println(MetaCodeSegment.class); if (arg1.index == 10) { System.exit(0); return; } - System.out.println(MetaCodeSegment.class); // DataSegment.get("local").update ods.update("local", "key1", "String data");