changeset 633:746447b7c3e0

add CreateCodeSegment Factory
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Fri, 01 Dec 2017 01:10:03 +0900
parents ffaacab84d1a
children 69f9c5ff1df9
files src/main/java/alice/Annotation/AliceAnnotation.java src/main/java/alice/Annotation/AliceAnnotationTest.java src/main/java/alice/codesegment/CodeSegment.java src/main/java/alice/codesegment/CreateCodeSegment.java
diffstat 4 files changed, 104 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/alice/Annotation/AliceAnnotation.java	Thu Nov 30 19:42:31 2017 +0900
+++ b/src/main/java/alice/Annotation/AliceAnnotation.java	Fri Dec 01 01:10:03 2017 +0900
@@ -44,8 +44,9 @@
     }
 
     public static void main(String[] args){
-        AliceAnnotation alice = new AliceAnnotation();
+        /*AliceAnnotation alice = new AliceAnnotation();
         alice.test();
+        */
     }
 
     public void test(){
--- a/src/main/java/alice/Annotation/AliceAnnotationTest.java	Thu Nov 30 19:42:31 2017 +0900
+++ b/src/main/java/alice/Annotation/AliceAnnotationTest.java	Fri Dec 01 01:10:03 2017 +0900
@@ -1,5 +1,6 @@
 package alice.Annotation;
 
+import alice.codesegment.CreateCodeSegment;
 import alice.codesegment.InputDataSegment;
 import alice.datasegment.CommandType;
 import alice.datasegment.Receiver;
@@ -14,9 +15,11 @@
  */
 public class AliceAnnotationTest {
     public static void main(String[] args){
-        AliceAnnotation alice = new AliceAnnotation();
-        AliceAnnotationTest aliceTest = new AliceAnnotationTest();
-        aliceTest.test(alice);
+        CreateCodeSegment createCodeSegment = new CreateCodeSegment(AliceAnnotation.class);
+        createCodeSegment.run();
+        //AliceAnnotation alice = new AliceAnnotation();
+        //AliceAnnotationTest aliceTest = new AliceAnnotationTest();
+        //aliceTest.test(alice);
     }
 
     public void test(AliceAnnotation alice){
--- a/src/main/java/alice/codesegment/CodeSegment.java	Thu Nov 30 19:42:31 2017 +0900
+++ b/src/main/java/alice/codesegment/CodeSegment.java	Fri Dec 01 01:10:03 2017 +0900
@@ -19,7 +19,7 @@
     private ArrayList<Receiver> list = new ArrayList<Receiver>();
     private int priority = Thread.NORM_PRIORITY;//?
 
-    public CodeSegment(){
+    /*public CodeSegment(){
         for (Field field : this.getClass().getDeclaredFields()) {
             if (field.isAnnotationPresent(Take.class)){
                 System.out.println(field.getName());
@@ -27,6 +27,19 @@
                 Take ano = field.getAnnotation(Take.class);
                 System.out.println(ano.value());
 
+                Receiver receiver;
+                try {
+                    receiver = (Receiver) field.get(this);
+                } catch (IllegalAccessException e) {
+                    e.printStackTrace();
+                }
+
+                try {
+                    field.set(this, ids.create(CommandType.TAKE));
+                } catch (IllegalAccessException e) {
+                    e.printStackTrace();
+                }
+
                 //Receiver receiver = new Receiver(ids, CommandType.TAKE);
                 //receiver.setKey(ano.value());
 
@@ -36,7 +49,7 @@
                     CtClass cc = cp.get(this.getClass().getName());
 
                     try {
-                        CtField cf = CtField.make("alice.datasegment.Receiver " + ano.value() + " = ids.create(CommandType.TAKE);",cc);
+                        CtField cf = CtField.make("public alice.datasegment.Receiver " + ano.value() + " = ids.create(CommandType.TAKE);",cc);
                         cc.addField(cf);
                         CtConstructor[] ctConstructors = cc.getConstructors();
                         ctConstructors[0].setBody(ano.value() + ".setKey(\"" +ano.value() + "\");");
@@ -56,9 +69,10 @@
                     e.printStackTrace();
                 }
 
+
             }
         }
-    }
+    }*/
 
     public void execute() {
         ids.receive();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/codesegment/CreateCodeSegment.java	Fri Dec 01 01:10:03 2017 +0900
@@ -0,0 +1,79 @@
+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();
+        }
+
+    }
+}