view 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
line wrap: on
line source

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 {

    public InputDataSegment ids = new InputDataSegment(this);
    public OutputDataSegment ods = new OutputDataSegment();
    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();
    }

    public void register(Receiver receiver) {
        list.add(receiver);
    }//Receiverを作成?

    public void recycle(){//idsのリセット
        ids.init();
        ids.setCounter(list.size());
        for (Receiver receiver : list) {
            receiver.index = 0;
            ids.recommand(receiver);
        }
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

}