Example usage for org.objectweb.asm MethodVisitor visitVarInsn

List of usage examples for org.objectweb.asm MethodVisitor visitVarInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitVarInsn.

Prototype

public void visitVarInsn(final int opcode, final int var) 

Source Link

Document

Visits a local variable instruction.

Usage

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void firstCause(FirstCause firstCause, Class type, int param, int event, MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "cause", BytecodeEventListenerGeneration.eventCauseMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.causeType.getInternalName(), "first",
            BytecodeEventListenerGeneration.causeFirstMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);/*  w  ww  .  j a v a  2  s.  c  om*/
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void lastCause(LastCause lastCause, Class type, int param, int event, MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "cause", BytecodeEventListenerGeneration.eventCauseMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.causeType.getInternalName(), "last",
            BytecodeEventListenerGeneration.causeLastMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);/*from   w w  w. j  a v  a  2  s.  c om*/
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void getContext(GetContext getContext, String paramName, Class type, int param, int event,
        MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "context", BytecodeEventListenerGeneration.eventContextMethod.getDescriptor(), true);
    visitor.visitLdcInsn(getContext.value().equals(GetContext.REFER_TO_PARAM) ? param : getContext.value());
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(), "get",
            BytecodeEventListenerGeneration.contextGetMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);/* www .  j  a va2s. c  o m*/
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void allContexts(AllContexts contexts, boolean asPair, Class type, int param, int event,
        MethodVisitor visitor) {
    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "context", BytecodeEventListenerGeneration.eventContextMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    if (asPair) {
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "all", BytecodeEventListenerGeneration.allContextMethod.getDescriptor(), false);
    } else {/* ww w. j a  v  a2 s. c  o  m*/
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "allSimple", BytecodeEventListenerGeneration.allContextSimpleMethod.getDescriptor(), false);
    }
    visitor.visitVarInsn(ASTORE, param);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void firstContext(FirstContext firstContext, boolean asPair, Class type, int param, int event,
        MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "context", BytecodeEventListenerGeneration.eventContextMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    if (asPair) {
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "first", BytecodeEventListenerGeneration.contextFirstMethod.getDescriptor(), false);
    } else {//from   w ww  .j a  v  a 2  s  . c  o  m
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "firstSimple", BytecodeEventListenerGeneration.contextSimpleFirstMethod.getDescriptor(), false);
    }
    visitor.visitInsn(DUP);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(asPair ? Type.getType(Pair.class) : Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void lastContext(LastContext lastContext, boolean asPair, Class type, int param, int event,
        MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "context", BytecodeEventListenerGeneration.eventContextMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    if (asPair) {
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "last", BytecodeEventListenerGeneration.contextLastMethod.getDescriptor(), false);
    } else {// w  w  w  . j ava2 s . c  o m
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(),
                "lastSimple", BytecodeEventListenerGeneration.contextSimpleLastMethod.getDescriptor(), false);
    }
    visitor.visitInsn(DUP);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(asPair ? Type.getType(Pair.class) : Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void get(Get get, Parameter parameter, Class eventClass, int param, int event,
        MethodVisitor visitor) {
    try {//from  ww w . j ava 2  s  . com
        String name = EventListenerWrapper.name(get, parameter, eventClass).get();
        Type eventType = Type.getType(eventClass);
        Method rMethod = eventClass.getMethod(name);
        Type method = Type.getType(rMethod);
        Type type = Type.getType(parameter.getType());
        visitor.visitVarInsn(ALOAD, event);
        if (eventClass.isInterface()) {
            visitor.visitMethodInsn(INVOKEINTERFACE, eventType.getInternalName(), name, method.getDescriptor(),
                    true);
        } else {
            visitor.visitMethodInsn(INVOKEVIRTUAL, eventType.getInternalName(), name, method.getDescriptor(),
                    false);
        }
        if (get.nonnull() && method.getReturnType().getDescriptor().startsWith("L")) {
            Label continueLabel = new Label();
            visitor.visitInsn(DUP);
            visitor.visitJumpInsn(IFNONNULL, continueLabel);
            visitor.visitInsn(RETURN);
            visitor.visitLabel(continueLabel);
            visitor.visitVarInsn(ASTORE, param);
        } else {
            if (!method.getReturnType().getDescriptor().startsWith("L")) {
                Type boxing = Type.getType(Reflections.boxingType(rMethod.getReturnType()));
                visitor.visitTypeInsn(NEW, boxing.getInternalName());
                visitor.visitInsn(DUP_X1);
                visitor.visitInsn(SWAP);
                visitor.visitMethodInsn(INVOKESPECIAL, boxing.getInternalName(), "<init>",
                        Type.getMethodDescriptor(Type.VOID_TYPE, method.getReturnType()), false);
            }
            visitor.visitVarInsn(ASTORE, param);
        }
    } catch (NoSuchMethodException e) {
        //Meh
    }
}

From source file:com.gmail.socraticphoenix.occurence.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static List<EventListenerWrapper> of(Object listener, File dir) {
    List<Class> cachedListeners = BytecodeEventListenerGeneration.listenerCache.get(listener.getClass());
    List<EventListenerWrapper> wrappers = new ArrayList<>();

    if (cachedListeners != null) {
        cachedListeners.forEach(c -> {
            try {
                wrappers.add((EventListenerWrapper) c.getConstructor(Object.class).newInstance(listener));
            } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
                    | InvocationTargetException e) {

            }//from  w w  w.j a va2 s . c  om
        });
    } else {
        try {
            Type listenerType = Type.getType(listener.getClass());
            Map<String, ClassWriter> classes = new LinkedHashMap<>();
            Map<String, String> binaryNames = new LinkedHashMap<>();

            List<Method> methods = new ArrayList<>();
            for (Method method : listener.getClass().getDeclaredMethods()) {
                if (method.isAnnotationPresent(EventListener.class)) {
                    EventListenerWrapper.verify(method, listener);
                    methods.add(method);
                }
            }

            for (Method method : methods) {
                int event = BytecodeEventListenerGeneration.isolateEvent(method);
                int eventVarIndex = event + 1;
                int[] jumpTable = BytecodeEventListenerGeneration.jumpTable(eventVarIndex,
                        method.getParameters());

                Parameter eventParam = method.getParameters()[event];
                Class eventClass = eventParam.getType();
                Type eventType = Type.getType(eventClass);

                String name = BytecodeEventListenerGeneration.name(method);
                ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
                classes.put(name, writer);

                String internalName = BytecodeEventListenerGeneration.listener(
                        method.getAnnotation(EventListener.class), eventClass, listener, method, writer);
                binaryNames.put(name, internalName.replace('/', '.'));

                if (!eventType.equals(BytecodeEventListenerGeneration.eventType)) {
                    MethodVisitor handleDelegateMethod = writer.visitMethod(ACC_PUBLIC | ACC_FINAL, "handle",
                            Type.getMethodDescriptor(Type.VOID_TYPE, BytecodeEventListenerGeneration.eventType),
                            null, null);
                    handleDelegateMethod.visitCode();
                    handleDelegateMethod.visitVarInsn(ALOAD, 0);
                    handleDelegateMethod.visitVarInsn(ALOAD, 1);
                    BytecodeEventListenerGeneration.checkCast(eventType, handleDelegateMethod);
                    handleDelegateMethod.visitMethodInsn(INVOKEVIRTUAL, internalName, "handle",
                            Type.getMethodDescriptor(Type.VOID_TYPE, eventType), false);
                    handleDelegateMethod.visitInsn(RETURN);
                    handleDelegateMethod.visitMaxs(0, 0);
                    handleDelegateMethod.visitEnd();
                }

                MethodVisitor handleMethod = writer.visitMethod(ACC_PUBLIC | ACC_FINAL, "handle",
                        Type.getMethodDescriptor(Type.VOID_TYPE, eventType), null, null);
                handleMethod.visitCode();
                if (method.isAnnotationPresent(Exclude.class)) {
                    BytecodeEventListenerGeneration.exclude(method.getAnnotation(Exclude.class), 1,
                            handleMethod);
                } else if (method.isAnnotationPresent(Include.class)) {
                    BytecodeEventListenerGeneration.include(method.getAnnotation(Include.class), 1,
                            handleMethod);
                }

                if (method.isAnnotationPresent(Cancelled.class)) {
                    BytecodeEventListenerGeneration.cancelled(method.getAnnotation(Cancelled.class), 1,
                            eventClass, handleMethod);
                }

                for (int i = 0; i < method.getParameters().length; i++) {
                    Parameter parameter = method.getParameters()[i];
                    JavaFilledGenerics generics = parameter.getParameterizedType() instanceof ParameterizedType
                            ? JavaFilledGenerics.of((ParameterizedType) parameter.getParameterizedType())
                            : JavaFilledGenerics.empty();
                    int var = i + 1;
                    Annotation annotation = BytecodeEventListenerGeneration.isolateAnnotation(parameter);
                    if (annotation instanceof AllCauses) {
                        AllCauses allCauses = (AllCauses) annotation;
                        Class clazz = generics.getFilled().isEmpty() ? Object.class
                                : generics.getFilled().get(0).type().getRepresented().orElse(Object.class);
                        BytecodeEventListenerGeneration.allCauses(allCauses, clazz, jumpTable[var], 1,
                                handleMethod);
                    } else if (annotation instanceof AllContexts) {
                        AllContexts allContexts = (AllContexts) annotation;

                        JavaNamespace.Filled target = generics.getFilled().isEmpty()
                                ? JavaNamespace.of(Object.class).fill()
                                : generics.getFilled().get(0).type();
                        Class clazz = target.getRepresented().orElse(Object.class);

                        if (!allContexts.value()) {
                            BytecodeEventListenerGeneration.allContexts(allContexts, false, parameter.getType(),
                                    jumpTable[var], 1, handleMethod);
                        } else if (clazz == Pair.class) {
                            Class a = target.getFilledGenerics().getFilled().isEmpty() ? Object.class
                                    : target.getFilledGenerics().getFilled().get(0).type().getRepresented()
                                            .orElse(Object.class);
                            Class b = target.getFilledGenerics().getFilled().size() <= 1 ? Object.class
                                    : target.getFilledGenerics().getFilled().get(1).type().getRepresented()
                                            .orElse(Object.class);
                            if (a == String.class) {
                                BytecodeEventListenerGeneration.allContexts(allContexts, true, b,
                                        jumpTable[var], 1, handleMethod);
                            } else {
                                BytecodeEventListenerGeneration.allContexts(allContexts, false,
                                        parameter.getType(), jumpTable[var], 1, handleMethod);
                            }
                        } else {
                            BytecodeEventListenerGeneration.allContexts(allContexts, false, parameter.getType(),
                                    jumpTable[var], 1, handleMethod);
                        }
                    } else if (annotation instanceof FirstCause) {
                        FirstCause firstCause = (FirstCause) annotation;
                        BytecodeEventListenerGeneration.firstCause(firstCause, parameter.getType(),
                                jumpTable[var], 1, handleMethod);
                    } else if (annotation instanceof FirstContext) {
                        FirstContext firstContext = (FirstContext) annotation;
                        if (!firstContext.value()) {
                            BytecodeEventListenerGeneration.firstContext(firstContext, false,
                                    parameter.getType(), jumpTable[var], 1, handleMethod);
                        } else if (parameter.getType() == Pair.class) {
                            Class a = generics.getFilled().isEmpty() ? Object.class
                                    : generics.getFilled().get(0).type().getRepresented().orElse(Object.class);
                            Class b = generics.getFilled().size() <= 1 ? Object.class
                                    : generics.getFilled().get(1).type().getRepresented().orElse(Object.class);
                            if (a == String.class) {
                                BytecodeEventListenerGeneration.firstContext(firstContext, true, b,
                                        jumpTable[var], 1, handleMethod);
                            } else {
                                BytecodeEventListenerGeneration.firstContext(firstContext, false,
                                        parameter.getType(), jumpTable[var], 1, handleMethod);
                            }
                        } else {
                            BytecodeEventListenerGeneration.firstContext(firstContext, false,
                                    parameter.getType(), jumpTable[var], 1, handleMethod);
                        }
                    } else if (annotation instanceof LastCause) {
                        LastCause lastCause = (LastCause) annotation;
                        BytecodeEventListenerGeneration.lastCause(lastCause, parameter.getType(),
                                jumpTable[var], 1, handleMethod);
                    } else if (annotation instanceof LastContext) {
                        LastContext lastContext = (LastContext) annotation;
                        if (!lastContext.value()) {
                            BytecodeEventListenerGeneration.lastContext(lastContext, false, parameter.getType(),
                                    jumpTable[var], 1, handleMethod);
                        } else if (parameter.getType() == Pair.class) {
                            Class a = generics.getFilled().isEmpty() ? Object.class
                                    : generics.getFilled().get(0).type().getRepresented().orElse(Object.class);
                            Class b = generics.getFilled().size() <= 1 ? Object.class
                                    : generics.getFilled().get(1).type().getRepresented().orElse(Object.class);
                            if (a == String.class) {
                                BytecodeEventListenerGeneration.lastContext(lastContext, true, b,
                                        jumpTable[var], 1, handleMethod);
                            } else {
                                BytecodeEventListenerGeneration.lastContext(lastContext, false,
                                        parameter.getType(), jumpTable[var], 1, handleMethod);
                            }
                        } else {
                            BytecodeEventListenerGeneration.lastContext(lastContext, false, parameter.getType(),
                                    jumpTable[var], 1, handleMethod);
                        }
                    } else if (annotation instanceof GetContext) {
                        GetContext getContext = (GetContext) annotation;
                        BytecodeEventListenerGeneration.getContext(getContext, parameter.getName(),
                                parameter.getType(), jumpTable[var], 1, handleMethod);
                    } else if (annotation instanceof Get) {
                        Get get = (Get) annotation;
                        BytecodeEventListenerGeneration.get(get, parameter, eventClass, jumpTable[var], 1,
                                handleMethod);
                    }
                }

                handleMethod.visitVarInsn(ALOAD, 0);
                handleMethod.visitFieldInsn(GETFIELD, internalName, "listener", listenerType.getDescriptor());
                for (int i = 0; i < method.getParameters().length; i++) {
                    handleMethod.visitVarInsn(ALOAD, jumpTable[i + 1]);
                }
                handleMethod.visitMethodInsn(INVOKEVIRTUAL, listenerType.getInternalName(), method.getName(),
                        Type.getType(method).getDescriptor(), false);
                handleMethod.visitInsn(RETURN);
                handleMethod.visitMaxs(0, 0);
                handleMethod.visitEnd();
            }

            List<Class> cache = BytecodeEventListenerGeneration.listenerCache.get(listener.getClass());
            if (cache == null) {
                cache = new ArrayList<>();
                BytecodeEventListenerGeneration.listenerCache.put(listener.getClass(), cache);
            }

            for (Map.Entry<String, ClassWriter> finished : classes.entrySet()) {
                ClassWriter visitor = finished.getValue();
                visitor.visitEnd();

                byte[] clazz = visitor.toByteArray();
                if (dir != null) {
                    File target = new File(dir, binaryNames.get(finished.getKey()) + ".class");
                    try {
                        target.getAbsoluteFile().getParentFile().mkdirs();
                        target.createNewFile();
                        FileOutputStream stream = new FileOutputStream(target);
                        stream.write(clazz);
                        stream.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                Class eventListenerClass = EventListenerClassLoader.INSTANCE
                        .defineClass(binaryNames.get(finished.getKey()), clazz);
                cache.add(eventListenerClass);
                EventListenerWrapper instance = (EventListenerWrapper) eventListenerClass
                        .getConstructor(Object.class).newInstance(listener);
                wrappers.add(instance);
            }

        } catch (NoSuchMethodException | InstantiationException | InvocationTargetException
                | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    return wrappers;
}

From source file:com.gmail.socraticphoenix.occurence.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static String listener(EventListener eventListener, Class eventClass, Object listener, Method method,
        ClassVisitor visitor) {//from www.ja  v  a 2  s .  c  o m
    Type listenerType = Type.getType(listener.getClass());
    Type eventType = Type.getType(eventClass);
    String internalImplName = listenerType.getInternalName() + "$listener$" + method.getName() + "$"
            + BytecodeEventListenerGeneration.index++;
    String simpleName = listener.getClass().getName() + "#" + method.getName() + "("
            + Stream.of(method.getParameters()).map(p -> p.getType().getSimpleName())
                    .reduce((a, b) -> a + ", " + b).orElse("")
            + ")";

    visitor.visit(V1_8, ACC_PUBLIC | ACC_SYNTHETIC | ACC_FINAL, internalImplName, null,
            BytecodeEventListenerGeneration.objectType.getInternalName(),
            new String[] { BytecodeEventListenerGeneration.eventWrapperType.getInternalName() });
    visitor.visitField(ACC_PRIVATE | ACC_FINAL, "listener", listenerType.getDescriptor(), null, null)
            .visitEnd();

    MethodVisitor constructor = visitor.visitMethod(ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class)), null, null);
    constructor.visitParameter("listener", ACC_FINAL);
    constructor.visitCode();
    constructor.visitVarInsn(ALOAD, 0);
    constructor.visitMethodInsn(INVOKESPECIAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
            "<init>", Type.getMethodDescriptor(Type.VOID_TYPE), false);
    constructor.visitVarInsn(ALOAD, 0);
    constructor.visitVarInsn(ALOAD, 1);
    constructor.visitTypeInsn(CHECKCAST, listenerType.getInternalName());
    constructor.visitFieldInsn(PUTFIELD, internalImplName, "listener", listenerType.getDescriptor());
    constructor.visitInsn(RETURN);
    constructor.visitMaxs(0, 0);
    constructor.visitEnd();

    MethodVisitor name = visitor.visitMethod(ACC_PUBLIC, "name",
            Type.getMethodDescriptor(Type.getType(String.class)), null, null);
    name.visitLdcInsn(simpleName);
    name.visitInsn(ARETURN);
    name.visitMaxs(0, 0);
    name.visitEnd();

    MethodVisitor priority = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "priority",
            BytecodeEventListenerGeneration.priorityMethod.getDescriptor(), null, null);
    priority.visitCode();
    priority.visitLdcInsn(eventListener.value());
    priority.visitInsn(IRETURN);
    priority.visitMaxs(0, 0);
    priority.visitEnd();

    MethodVisitor getListener = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "listener",
            BytecodeEventListenerGeneration.listenerMethod.getDescriptor(), null, null);
    getListener.visitVarInsn(ALOAD, 0);
    getListener.visitFieldInsn(GETFIELD, internalImplName, "listener", listenerType.getDescriptor());
    getListener.visitInsn(ARETURN);
    getListener.visitMaxs(0, 0);
    getListener.visitEnd();

    MethodVisitor event = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "mainEvent",
            BytecodeEventListenerGeneration.mainEventMethod.getDescriptor(), null, null);
    event.visitCode();
    event.visitLdcInsn(eventType);
    event.visitInsn(ARETURN);
    event.visitMaxs(0, 0);
    event.visitEnd();

    return internalImplName;
}

From source file:com.gmail.socraticphoenix.occurence.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void get(Get get, Parameter parameter, Class eventClass, int param, int event,
        MethodVisitor visitor) {
    try {//  ww  w.ja  v  a2  s  . c  om
        String name = EventListenerWrapper.name(get, parameter, eventClass).get();
        Type eventType = Type.getType(eventClass);
        Method rMethod = eventClass.getMethod(name);
        Type method = Type.getType(rMethod);
        visitor.visitVarInsn(ALOAD, event);
        if (eventClass.isInterface()) {
            visitor.visitMethodInsn(INVOKEINTERFACE, eventType.getInternalName(), name, method.getDescriptor(),
                    true);
        } else {
            visitor.visitMethodInsn(INVOKEVIRTUAL, eventType.getInternalName(), name, method.getDescriptor(),
                    false);
        }
        if (get.nonnull() && method.getReturnType().getDescriptor().startsWith("L")) {
            Label continueLabel = new Label();
            visitor.visitInsn(DUP);
            visitor.visitJumpInsn(IFNONNULL, continueLabel);
            visitor.visitInsn(RETURN);
            visitor.visitLabel(continueLabel);
            visitor.visitVarInsn(ASTORE, param);
        } else {
            if (!method.getReturnType().getDescriptor().startsWith("L")) {
                Type boxing = Type.getType(Reflections.boxingType(rMethod.getReturnType()));
                visitor.visitTypeInsn(NEW, boxing.getInternalName());
                visitor.visitInsn(DUP_X1);
                visitor.visitInsn(SWAP);
                visitor.visitMethodInsn(INVOKESPECIAL, boxing.getInternalName(), "<init>",
                        Type.getMethodDescriptor(Type.VOID_TYPE, method.getReturnType()), false);
            }
            visitor.visitVarInsn(ASTORE, param);
        }
    } catch (NoSuchMethodException e) {
        //Meh
    }
}