# HG changeset patch # User Nozomi Teruya # Date 1512306575 -32400 # Node ID 33f300d0720ac07ffe3a1381c947a3fe4fbb3986 # Parent 0423eb7fd9ee24181d648a3638760a6bca9f00c3 running CodeSegment by Annotation without create Receiver :D diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/Annotation/AliceAnnotation.java --- a/src/main/java/alice/Annotation/AliceAnnotation.java Sun Dec 03 00:09:04 2017 +0900 +++ b/src/main/java/alice/Annotation/AliceAnnotation.java Sun Dec 03 22:09:35 2017 +0900 @@ -17,41 +17,23 @@ */ public class AliceAnnotation extends CodeSegment{ - //Receiver piyo = ids.create(CommandType.TAKE); + @Take("hoge") + String hoge; - @Take("hoge") - Receiver hoge; + @Peek("piyo") + int piyo; - @Take("huga") - Receiver huga; - //@TakeRemote(dsm = "remote", key = "piyo") - - public class StringData{public String[] data = {"hoge", "huga", "piyo"};} - + public AliceAnnotation() { + ods.put("hoge", "hogehogehgoe"); + ods.put("piyo", 1); + } @Override public void run() { System.out.println("in run!"); - System.out.println(hoge.asClass(String.class)); - System.out.println(huga.asClass(String.class)); - } - - - public AliceAnnotation() { - ods.put("hoge", "hogehogehgoe"); - ods.put("huga", "hugahugahuga"); - ods.put("piyo", "piyopiyo"); - } - - public static void main(String[] args){ - /*AliceAnnotation alice = new AliceAnnotation(); - alice.test(); - */ - } - - public void test(){ - + System.out.println(hoge); + System.out.println(piyo); } } diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/Annotation/AliceAnnotationTest.java --- a/src/main/java/alice/Annotation/AliceAnnotationTest.java Sun Dec 03 00:09:04 2017 +0900 +++ b/src/main/java/alice/Annotation/AliceAnnotationTest.java Sun Dec 03 22:09:35 2017 +0900 @@ -1,6 +1,5 @@ package alice.Annotation; -import alice.codesegment.CreateCodeSegment; import alice.codesegment.InputDataSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; @@ -15,25 +14,6 @@ */ public class AliceAnnotationTest { public static void main(String[] args){ - //CreateCodeSegment createCodeSegment = new CreateCodeSegment(AliceAnnotation.class); - //createCodeSegment.run(); AliceAnnotation alice = new AliceAnnotation(); - //alice.ods.put("hoge", "hogehogehgoe"); - //alice.ods.put("huga", "hugahugahuga"); - } - - public void test(AliceAnnotation alice){ - Class aliceAnnotationClass = AliceAnnotation.class; - - for (Field field : aliceAnnotationClass.getDeclaredFields()) { - if (field.isAnnotationPresent(Take.class)){ - System.out.println(field.getName()); - System.out.println(field.getType()); - Take i = field.getAnnotation(Take.class); - System.out.println(i.value()); - Receiver receiver = alice.ids.create(CommandType.TAKE); - receiver.setKey(i.value()); - } - } } } diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/Annotation/MetaReceiver.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/Annotation/MetaReceiver.java Sun Dec 03 22:09:35 2017 +0900 @@ -0,0 +1,34 @@ +package alice.Annotation; + +import alice.codesegment.InputDataSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +import java.lang.reflect.Type; + +/** + * Created by e125769 on 12/3/17. + */ +public class MetaReceiver{ + + private Type type; + + private Receiver receiver; + + public MetaReceiver(Type t, Receiver r) { + type = t; + receiver = r; + } + + public T asClass(){ + return receiver.asClass((Class) type.getClass()); + } + + public Type getType() { + return type; + } + + public Receiver getReceiver() { + return receiver; + } +} diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/Annotation/Peek.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/Annotation/Peek.java Sun Dec 03 22:09:35 2017 +0900 @@ -0,0 +1,15 @@ +package alice.Annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Peek { + String value(); +} \ No newline at end of file diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/codesegment/CodeSegment.java --- a/src/main/java/alice/codesegment/CodeSegment.java Sun Dec 03 00:09:04 2017 +0900 +++ b/src/main/java/alice/codesegment/CodeSegment.java Sun Dec 03 22:09:35 2017 +0900 @@ -3,12 +3,16 @@ import java.io.IOException; import java.lang.reflect.*; import java.util.ArrayList; +import java.util.HashMap; +import alice.Annotation.MetaReceiver; +import alice.Annotation.Peek; import alice.Annotation.Take; import alice.codesegment.InputDataSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; import javassist.*; +import javassist.compiler.ast.Pair; import javassist.tools.reflect.Sample; import sun.jvm.hotspot.oops.Instance; @@ -16,9 +20,10 @@ public InputDataSegment ids = new InputDataSegment(this); public OutputDataSegment ods = new OutputDataSegment(); - private ArrayList list = new ArrayList(); - private ArrayList metaReceivers = new ArrayList(); + private ArrayList list = new ArrayList();//Receivers of after setKey private int priority = Thread.NORM_PRIORITY;//? + private ArrayList receivers = new ArrayList();//all Receivers + private Field[] fields; public CodeSegment(){ createReceiver(); @@ -26,32 +31,46 @@ } public void createReceiver(){ - for (Field field : this.getClass().getDeclaredFields()) { + this.fields = this.getClass().getDeclaredFields(); + for (Field field : fields) { if (field.isAnnotationPresent(Take.class)){ - metaReceivers.add(ids.create(CommandType.TAKE)); + receivers.add(new MetaReceiver(field.getType(), ids.create(CommandType.TAKE))); + } + else if (field.isAnnotationPresent(Peek.class)){ + receivers.add(new MetaReceiver(field.getType(), ids.create(CommandType.PEEK))); } } } public void setKey(){ int i = 0; - for (Field field : this.getClass().getDeclaredFields()) { + for (Field field : fields) { if (field.isAnnotationPresent(Take.class)){ Take ano = field.getAnnotation(Take.class); - - field.setAccessible(true); + receivers.get(i).getReceiver().setKey(ano.value()); + i++; + } + else if (field.isAnnotationPresent(Peek.class)){ + Peek ano = field.getAnnotation(Peek.class); + receivers.get(i).getReceiver().setKey(ano.value()); + i++; + } + } + } - metaReceivers.get(i).setKey(ano.value()); - + public void setValue(){ + int i = 0; + for (Field field : fields){ + if (field.isAnnotationPresent(Take.class) || field.isAnnotationPresent(Peek.class)) { + field.setAccessible(true); try { - field.set(this, metaReceivers.get(i)); + field.set(this, receivers.get(i).asClass()); i++; } catch (IllegalAccessException e) { e.printStackTrace(); } } } - } public void execute() { diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/codesegment/CreateCodeSegment.java --- a/src/main/java/alice/codesegment/CreateCodeSegment.java Sun Dec 03 00:09:04 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -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(); - } - - } -} diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/codesegment/InputDataSegment.java --- a/src/main/java/alice/codesegment/InputDataSegment.java Sun Dec 03 00:09:04 2017 +0900 +++ b/src/main/java/alice/codesegment/InputDataSegment.java Sun Dec 03 22:09:35 2017 +0900 @@ -71,6 +71,7 @@ public void receive() { if (count.decrementAndGet() == 0) { + cs.setValue(); CodeSegmentManager.submit(cs); } } diff -r 0423eb7fd9ee -r 33f300d0720a src/main/java/alice/datasegment/Receiver.java --- a/src/main/java/alice/datasegment/Receiver.java Sun Dec 03 00:09:04 2017 +0900 +++ b/src/main/java/alice/datasegment/Receiver.java Sun Dec 03 22:09:35 2017 +0900 @@ -2,6 +2,7 @@ import alice.codesegment.InputDataSegment; +import javassist.compiler.ast.ASTree; /** * idsコマンド(create, setKey)の処理をする。コマンドの定義はids/LDSM内にある。