view src/main/java/alice/codesegment/CodeSegment.java @ 634:69f9c5ff1df9

work reflection setKey, but not correct
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Fri, 01 Dec 2017 21:34:22 +0900
parents 746447b7c3e0
children 0423eb7fd9ee
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());

                field.setAccessible(true);

                Receiver receiver = ids.create(CommandType.TAKE);;
                receiver.setKey(ano.value());

                try {
                    field.set(this, receiver);
                } catch (IllegalAccessException 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;
    }

}