changeset 630:77adeb85c4d0 dispose

add Annotation
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Thu, 30 Nov 2017 00:43:22 +0900
parents d0d1cd3dfc11
children ffaacab84d1a d393d55ffd72
files src/main/java/alice/Annotation/AliceAnnotation.java src/main/java/alice/Annotation/AliceAnnotationTest.java src/main/java/alice/Annotation/CS.java src/main/java/alice/Annotation/InputData.java src/main/java/alice/Annotation/InputProcesser.java src/main/java/alice/Annotation/Take.java src/main/java/alice/Annotation/TakeRemote.java src/main/java/alice/codesegment/CodeSegment.java
diffstat 8 files changed, 253 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/AliceAnnotation.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,56 @@
+package alice.Annotation;
+
+import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+import com.sun.org.apache.bcel.internal.classfile.Code;
+
+import java.awt.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+
+import static javafx.scene.input.KeyCode.T;
+
+/**
+ * Created by e125769 on 10/15/17.
+ */
+public class AliceAnnotation extends CodeSegment{
+
+    //Receiver piyo = ids.create(CommandType.TAKE);
+
+    @Take("hoge")
+    StringData stringData;
+
+    @Take("huga")
+    StringData stringData2;
+
+    //@TakeRemote(dsm = "remote", key = "piyo")
+
+    public class StringData{public String[] data = {"hoge", "huga", "piyo"};}
+
+
+    @Override
+    public void run() {
+        System.out.println("in run!!!!");
+    }
+
+
+    public AliceAnnotation() {
+        //piyo.setKey("piyo");
+        ods.put("hoge", 0);
+        ods.put("huga", 1);
+        ods.put("piyo", "piyopiyo");
+    }
+
+    public static void main(String[] args){
+        AliceAnnotation alice = new AliceAnnotation();
+        alice.test();
+    }
+
+    public void test(){
+
+    }
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/AliceAnnotationTest.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,36 @@
+package alice.Annotation;
+
+import alice.codesegment.InputDataSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Array;
+import java.lang.reflect.Method;
+
+/**
+ * Created by e125769 on 11/14/17.
+ */
+public class AliceAnnotationTest {
+    public static void main(String[] args){
+        AliceAnnotation alice = new AliceAnnotation();
+        AliceAnnotationTest aliceTest = new AliceAnnotationTest();
+        aliceTest.test(alice);
+    }
+
+    public void test(AliceAnnotation alice){
+        Class<AliceAnnotation> 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 = ids.create(CommandType.TAKE);
+                receiver.setKey(i.value());
+            }
+        }*/
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/CS.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,30 @@
+package alice.Annotation;
+
+import alice.codesegment.InputDataSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+import java.util.ArrayList;
+
+/**
+ * Created by e125769 on 11/19/17.
+ */
+public abstract class CS {
+
+    public ArrayList input = new ArrayList<InputData>();
+    public Receiver[] receivers;
+    public InputDataSegment ids;
+
+    public void setInput(String[] datas){
+        System.out.println("in CS!!");
+        receivers = new Receiver[datas.length];
+        for(int i = 0; i < datas.length; i++){
+            receivers[i] = ids.create(CommandType.TAKE);
+            receivers[i].setKey(datas[i]);
+        }
+    }
+
+    public void setInput(ArrayList input) {
+        this.input = input;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/InputData.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,19 @@
+package alice.Annotation;
+
+/**
+ * Created by e125769 on 11/15/17.
+ */
+public class InputData<T> {
+
+    private T value;
+    private String key;
+
+    public InputData(T value){
+        this.value = value;
+    }
+
+    public T getValue() {
+        return value;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/InputProcesser.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,30 @@
+package alice.Annotation;
+
+
+import javax.annotation.processing.*;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic.Kind;
+import java.util.Set;
+
+/**
+ * Created by e125769 on 10/16/17.
+ */
+@SupportedAnnotationTypes("alice.Annotation.Input")
+public class InputProcesser extends AbstractProcessor {
+
+    @Override
+    public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) {
+
+        /*for (TypeElement typeElement : typeElements) {
+            Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(typeElement);
+            for (Element element : elements) {
+                Input in = element.getAnnotation(Input.class);
+                System.out. println("@input" + in.input());
+            }
+        }*/
+
+        return true;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/Take.java	Thu Nov 30 00:43:22 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 Take {
+    String value();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/Annotation/TakeRemote.java	Thu Nov 30 00:43:22 2017 +0900
@@ -0,0 +1,17 @@
+package alice.Annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Type;
+
+/**
+ * Created by e125769 on 11/15/17.
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface TakeRemote {
+    String dsm();
+    String key();
+}
--- a/src/main/java/alice/codesegment/CodeSegment.java	Thu Sep 14 20:04:48 2017 +0900
+++ b/src/main/java/alice/codesegment/CodeSegment.java	Thu Nov 30 00:43:22 2017 +0900
@@ -1,9 +1,16 @@
 package alice.codesegment;
 
+import java.io.IOException;
+import java.lang.reflect.*;
 import java.util.ArrayList;
 
+import alice.Annotation.Take;
 import alice.codesegment.InputDataSegment;
+import alice.datasegment.CommandType;
 import alice.datasegment.Receiver;
+import javassist.*;
+import javassist.tools.reflect.Sample;
+import sun.jvm.hotspot.oops.Instance;
 
 public abstract class CodeSegment implements Runnable {
 
@@ -12,6 +19,47 @@
     private ArrayList<Receiver> list = new ArrayList<Receiver>();
     private int priority = Thread.NORM_PRIORITY;//?
 
+    public CodeSegment(){
+        for (Field field : this.getClass().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());
+
+                //Receiver receiver = new Receiver(ids, CommandType.TAKE);
+                //receiver.setKey(ano.value());
+
+
+                ClassPool cp = ClassPool.getDefault();
+                try {
+                    CtClass cc = cp.get(this.getClass().getName());
+
+                    try {
+                        CtField cf = CtField.make("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() + "\");");
+                        cc.setModifiers(cc.getModifiers());
+                    } catch (CannotCompileException e) {
+                        e.printStackTrace();
+                    }
+
+                    try {
+                        Class c = cc.toClass(ClassLoader.getSystemClassLoader(), Sample.class.getProtectionDomain());
+                        //c.newInstance();
+                    } catch (CannotCompileException e) {
+                        e.printStackTrace();
+                    }
+
+                } catch (NotFoundException e) {
+                    e.printStackTrace();
+                }
+
+            }
+        }
+    }
+
     public void execute() {
         ids.receive();
     }
@@ -36,4 +84,5 @@
     public void setPriority(int priority) {
         this.priority = priority;
     }
-}
+
+}
\ No newline at end of file