Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

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

Introduction

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

Prototype

public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.github.javalbert.bytecode.utils.AsmUtils.java

License:Apache License

public static void visitDefaultConstructor(ClassWriter cw, String classTypeDescriptor) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();// ww w.  ja  va  2s . c o  m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (!mIgnoreClass && !isClInitExists && !mStaticFinalFields.isEmpty()) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();// w  ww . jav a  2 s . c  om
        // Here init static final fields.
        for (ClassStringField field : mStaticFinalFields) {
            if (!canEncrypted(field.value)) {
                continue;
            }
            String originValue = field.value;
            String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
            mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
            mv.visitLdcInsn(encryptValue);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                    "(Ljava/lang/String;)Ljava/lang/String;", false);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name, ClassStringField.STRING_DESC);
        }
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    super.visitEnd();
}

From source file:com.github.wolf480pl.mias4j.MakeTestBuryUninitialized.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from  w  w w . jav  a  2  s  .c  o  m
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Test3", null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(I)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn("hey");
        mv.visitVarInsn(ILOAD, 1);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IADD);
        mv.visitMethodInsn(INVOKESTATIC, "Test3", "say", "(Ljava/lang/Object;I)I", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        mv.visitTypeInsn(NEW, "java/util/LinkedList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/LinkedList", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKESPECIAL, "Test3", "<init>", "(Ljava/lang/Integer;Ljava/util/List;)V", false);
        mv.visitLdcInsn("hey1");
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(NEW, "java/util/Date");
        mv.visitInsn(DUP_X2);

        mv.visitMethodInsn(INVOKESPECIAL, "java/util/Date", "<init>", "()V", false);
        mv.visitInsn(POP2);

        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKESTATIC, "Test3", "say", "(Ljava/lang/Object;I)I", false);
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Integer;Ljava/util/List;)V",
                "(Ljava/lang/Integer;Ljava/util/List<Ljava/lang/Integer;>;)V", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "Test3");
        mv.visitIntInsn(BIPUSH, 10);
        mv.visitMethodInsn(INVOKESPECIAL, "Test3", "<init>", "(I)V", false);
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("byebye");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "say", "(Ljava/lang/Object;I)I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.github.wolf480pl.mias4j.MakeTestDynamic.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*from w  ww.  j  ava2s .co m*/
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestDynamic", null, "java/lang/Object", null);

    cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
            ACC_PUBLIC + ACC_FINAL + ACC_STATIC);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "bootstrap",
                "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;",
                null, new String[] { "java/lang/NoSuchMethodException", "java/lang/IllegalAccessException" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(Type.getType("LTestDynamic;"));
        mv.visitLdcInsn("sayHi");
        mv.visitVarInsn(ALOAD, 3);
        mv.visitLdcInsn(Type.getType("LTestDynamic;"));
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findSpecial",
                "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;",
                false);
        mv.visitVarInsn(ASTORE, 4);
        mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>",
                "(Ljava/lang/invoke/MethodHandle;)V", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(5, 5);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "TestDynamic");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "TestDynamic", "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        // mv.visitMethodInsn(INVOKESPECIAL, "TestDynamic", "sayHi", "()V", false);
        mv.visitInvokeDynamicInsn("sayHi", "(LTestDynamic;)V", new Handle(H_INVOKESTATIC, "TestDynamic",
                "bootstrap",
                "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                Type.getMethodType("()V"));
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PRIVATE, "sayHi", "()V", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hi");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.github.wolf480pl.mias4j.MakeTestMH.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestMH", null, "java/lang/Object", null);

    cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
            ACC_PUBLIC + ACC_FINAL + ACC_STATIC);

    {//from   www . j av  a2  s .co m
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");
        mv.visitLabel(l0);
        /*mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false);
        mv.visitLdcInsn(Type.getType("Ljava/io/PrintStream;"));
        mv.visitLdcInsn("println");
        mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;");
        mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "methodType", "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/invoke/MethodType;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);*/
        mv.visitLdcInsn(new Handle(H_INVOKEVIRTUAL, "java/io/PrintStream", "println",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class))));
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hey");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke",
                "(Ljava/io/PrintStream;Ljava/lang/String;)V", false);
        mv.visitLabel(l1);
        Label l3 = new Label();
        mv.visitJumpInsn(GOTO, l3);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V", false);
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(RETURN);
        mv.visitMaxs(5, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.github.xemiru.luamesh.MeshTransformer.java

License:Open Source License

public static void loadVar(MethodVisitor mv, String descriptor, int lindex, boolean box) {
    switch (descriptor) {
    case "Z": // boolean
        mv.visitVarInsn(ILOAD, lindex);/*from www .j  a va  2  s .c  o  m*/
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
        break;
    case "B": // byte
        mv.visitVarInsn(ILOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
        break;
    case "C": // char
        mv.visitVarInsn(ILOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
                    false);
        break;
    case "D": // double
        mv.visitVarInsn(DLOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
        break;
    case "F": // float
        mv.visitVarInsn(FLOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
        break;
    case "I": // int
        mv.visitVarInsn(ILOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        break;
    case "J": // long
        mv.visitVarInsn(LLOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
        break;
    case "S": // short
        mv.visitVarInsn(ILOAD, lindex);
        if (box)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
        break;
    default:
        mv.visitVarInsn(ALOAD, lindex);
        break;
    }
}

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

License:Open Source License

public static List<EventListenerWrapper> of(Object listener) {
    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  .  java2s. co  m
        });
    } 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];
                    JavaSourceFilledGenerics generics = parameter
                            .getParameterizedType() instanceof ParameterizedType
                                    ? JavaSourceFilledGenerics
                                            .of((ParameterizedType) parameter.getParameterizedType())
                                    : JavaSourceFilledGenerics.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;

                        JavaSourceNamespace.Filled target = generics.getFilled().isEmpty()
                                ? JavaSourceNamespace.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();
                Class eventListenerClass = EventListenerClassLoader.INSTANCE
                        .defineClass(binaryNames.get(finished.getKey()), clazz);
                cache.add(eventListenerClass);
                EventListenerWrapper instance = (EventListenerWrapper) eventListenerClass
                        .getConstructor(listener.getClass()).newInstance(listener);
                wrappers.add(instance);
            }

        } catch (NoSuchMethodException | InstantiationException | InvocationTargetException
                | IllegalAccessException e) {

        }
    }

    return wrappers;
}

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

License:Open Source License

public static String listener(EventListener eventListener, Class eventClass, Object listener, Method method,
        ClassVisitor visitor) {/*from   w  w w . java2s. c  o m*/
    Type listenerType = Type.getType(listener.getClass());
    Type eventType = Type.getType(eventClass);
    String internalImplName = listenerType.getInternalName() + "$listener$" + method.getName() + "$"
            + BytecodeEventListenerGeneration.index++;

    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, listenerType), 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.visitFieldInsn(PUTFIELD, internalImplName, "listener", listenerType.getDescriptor());
    constructor.visitInsn(RETURN);
    constructor.visitMaxs(0, 0);
    constructor.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.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void exclude(Exclude exclude, int event, MethodVisitor visitor) {
    visitor.visitVarInsn(ALOAD, event);/*w  w w .  j  a  v a2 s  .  co m*/
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
            "getClass", BytecodeEventListenerGeneration.getClassMethod.getDescriptor(), false);
    Label returnLabel = new Label();
    Label continueLabel = new Label();
    for (Class clazz : exclude.value()) {
        visitor.visitInsn(DUP);
        visitor.visitLdcInsn(Type.getType(clazz));
        visitor.visitInsn(SWAP);
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
                "equals", BytecodeEventListenerGeneration.equalsMethod.getDescriptor(), false);
        visitor.visitJumpInsn(IFNE, returnLabel);
    }
    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 include(Include include, int event, MethodVisitor visitor) {
    visitor.visitVarInsn(ALOAD, event);//from   w  w w  . ja v  a2s  .  co m
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
            "getClass", BytecodeEventListenerGeneration.getClassMethod.getDescriptor(), false);
    Label returnLabel = new Label();
    Label continueLabel = new Label();
    for (Class clazz : include.value()) {
        visitor.visitInsn(DUP);
        visitor.visitLdcInsn(Type.getType(clazz));
        visitor.visitInsn(SWAP);
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
                "equals", BytecodeEventListenerGeneration.equalsMethod.getDescriptor(), false);
        visitor.visitJumpInsn(IFEQ, returnLabel);
    }
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}