view src/main/java/alice/test/reflection/CloseEventManager.java @ 655:1c93e82e05c6 default tip

fix timestamp
author suruga
date Sat, 17 Feb 2018 00:33:00 +0900
parents 6e304a7a60e7
children
line wrap: on
line source

package alice.test.reflection;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.NotFoundException;


public class CloseEventManager {
    public static CloseEventManager closeManager = new CloseEventManager();

    private CloseEventManager(){}
    public static CloseEventManager getInstance(){
        return closeManager;
    }
    private ClassPool cp = ClassPool.getDefault();

    public <T> void setCodeSegment(String managerKey, Class<T> clazz){

        CtClass cs = null;
        String metaName = "Meta"+clazz.getSimpleName();
        try {
            // if cannot get mataName Class go catch NotFoundException
            cs = cp.get(metaName);
            // already created
            Class<?> metaClazz = cp.getClassLoader().loadClass(metaName);
            metaClazz.newInstance();
        } catch (ClassNotFoundException e) {
            System.out.println("ClassLoder loadClass error");
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.out.println("newInstance() error");
        } catch (IllegalAccessException e) {
            System.out.println("newInstance() error");
        } catch (NotFoundException e) {
            try {
                CtClass parent = cp.get(clazz.getName());

                // check super class is CS
                boolean checkSuperClass = parent.getSuperclass().getSimpleName().equals("CodeSegment");
                if (!checkSuperClass) {
                    // argument wrong
                    return;
                } else {
                    cs = cp.get(clazz.getName());

                    // define meta inputDataSegment
                    CtClass receiver = cp.get("alice.datasegment.Receiver");

                    // need check exist field name
                    String metaFieldName = "metaInfo";
                    CtField cf = new CtField(receiver ,metaFieldName, cs);
                    cs.addField(cf, "ids.create(alice.datasegment.CommandType.PEEK)");

                    CtConstructor[] ccArray = cs.getConstructors();
                    CtConstructor cc = null;
                    for (int i = 0; i < ccArray.length; i++){
                        if (ccArray[i].getName().equals(clazz.getSimpleName())){
                            cc = ccArray[i];
                            break;
                        }

                    }
                    String metaSetKey = metaFieldName+".setKey(\"TEST\");";

                    cc.insertAfter(metaSetKey);
                    cs.setName(metaName);

                    Class<?> metaClass = cs.toClass();
                    metaClass.newInstance();
                }
            } catch (NotFoundException e1) {
                System.out.println("not found super Class");
                return;
            } catch (CannotCompileException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
        }
    }


}