comparison src/main/java/alice/codesegment/CodeSegment.java @ 630:77adeb85c4d0 dispose

add Annotation
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Thu, 30 Nov 2017 00:43:22 +0900
parents 145c425db88d
children 746447b7c3e0
comparison
equal deleted inserted replaced
629:d0d1cd3dfc11 630:77adeb85c4d0
1 package alice.codesegment; 1 package alice.codesegment;
2 2
3 import java.io.IOException;
4 import java.lang.reflect.*;
3 import java.util.ArrayList; 5 import java.util.ArrayList;
4 6
7 import alice.Annotation.Take;
5 import alice.codesegment.InputDataSegment; 8 import alice.codesegment.InputDataSegment;
9 import alice.datasegment.CommandType;
6 import alice.datasegment.Receiver; 10 import alice.datasegment.Receiver;
11 import javassist.*;
12 import javassist.tools.reflect.Sample;
13 import sun.jvm.hotspot.oops.Instance;
7 14
8 public abstract class CodeSegment implements Runnable { 15 public abstract class CodeSegment implements Runnable {
9 16
10 public InputDataSegment ids = new InputDataSegment(this); 17 public InputDataSegment ids = new InputDataSegment(this);
11 public OutputDataSegment ods = new OutputDataSegment(); 18 public OutputDataSegment ods = new OutputDataSegment();
12 private ArrayList<Receiver> list = new ArrayList<Receiver>(); 19 private ArrayList<Receiver> list = new ArrayList<Receiver>();
13 private int priority = Thread.NORM_PRIORITY;//? 20 private int priority = Thread.NORM_PRIORITY;//?
21
22 public CodeSegment(){
23 for (Field field : this.getClass().getDeclaredFields()) {
24 if (field.isAnnotationPresent(Take.class)){
25 System.out.println(field.getName());
26 System.out.println(field.getType());
27 Take ano = field.getAnnotation(Take.class);
28 System.out.println(ano.value());
29
30 //Receiver receiver = new Receiver(ids, CommandType.TAKE);
31 //receiver.setKey(ano.value());
32
33
34 ClassPool cp = ClassPool.getDefault();
35 try {
36 CtClass cc = cp.get(this.getClass().getName());
37
38 try {
39 CtField cf = CtField.make("alice.datasegment.Receiver " + ano.value() + " = ids.create(CommandType.TAKE);",cc);
40 cc.addField(cf);
41 CtConstructor[] ctConstructors = cc.getConstructors();
42 ctConstructors[0].setBody(ano.value() + ".setKey(\"" +ano.value() + "\");");
43 cc.setModifiers(cc.getModifiers());
44 } catch (CannotCompileException e) {
45 e.printStackTrace();
46 }
47
48 try {
49 Class c = cc.toClass(ClassLoader.getSystemClassLoader(), Sample.class.getProtectionDomain());
50 //c.newInstance();
51 } catch (CannotCompileException e) {
52 e.printStackTrace();
53 }
54
55 } catch (NotFoundException e) {
56 e.printStackTrace();
57 }
58
59 }
60 }
61 }
14 62
15 public void execute() { 63 public void execute() {
16 ids.receive(); 64 ids.receive();
17 } 65 }
18 66
34 } 82 }
35 83
36 public void setPriority(int priority) { 84 public void setPriority(int priority) {
37 this.priority = priority; 85 this.priority = priority;
38 } 86 }
87
39 } 88 }