Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitComparable(MethodVisitor mv, boolean be, Property property, LocalVarInfo param,
        Label okay) {// w  w  w  .j  a  va2s  .  c o  m

    Type type = param.getType();
    int index = param.getIndex();

    switch (type.getSort()) {
    case Type.INT:
    case Type.SHORT:
        mv.visitVarInsn(ILOAD, index);
        break;
    case Type.LONG:
        mv.visitVarInsn(LLOAD, index);
        mv.visitInsn(LCONST_0);
        mv.visitInsn(LCMP);
        break;
    case Type.DOUBLE:
        mv.visitVarInsn(DLOAD, index);
        mv.visitInsn(DCONST_0);
        mv.visitInsn(DCMPG);
        break;
    case Type.FLOAT:
        mv.visitVarInsn(FLOAD, index);
        mv.visitInsn(FCONST_0);
        mv.visitInsn(FCMPG);
        break;
    case Type.OBJECT:
        if (type.equals(Type.getType(Integer.class))) {
            mv.visitVarInsn(ALOAD, index);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getType(Integer.class).getInternalName(), "intValue", "()I");
        } else if (type.equals(Type.getType(Short.class))) {
            mv.visitVarInsn(ALOAD, index);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getType(Short.class).getInternalName(), "intValue", "()I");
        } else if (type.equals(Type.getType(Long.class))) {
            mv.visitVarInsn(ALOAD, index);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getType(Long.class).getInternalName(), "longValue", "()J");
            mv.visitInsn(LCONST_0);
            mv.visitInsn(LCMP);
        } else if (type.equals(Type.getType(Double.class))) {
            mv.visitVarInsn(ALOAD, index);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getType(Double.class).getInternalName(), "doubleValue",
                    "()D");
            mv.visitInsn(DCONST_0);
            mv.visitInsn(DCMPG);
        } else if (type.equals(Type.getType(Float.class))) {
            mv.visitVarInsn(ALOAD, index);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getType(Float.class).getInternalName(), "floatValue", "()F");
            mv.visitInsn(FCONST_0);
            mv.visitInsn(FCMPG);
        } else {
            throw new AnnotationStateError("Unsupported type: " + type);
        }
        break;
    default:
        throw new AnnotationStateError("Unsupported type: " + type);
    }

    switch (property) {
    case NEGATIVE:
        mv.visitJumpInsn(be ? IFLT : IFGE, okay);
        break;
    case POSITIVE:
        mv.visitJumpInsn(be ? IFGT : IFLE, okay);
        break;
    case ZERO:
        mv.visitJumpInsn(be ? IFEQ : IFNE, okay);
        break;
    }
}

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitDeathStarPlans(MethodVisitor mv, boolean be, LocalVarInfo param, Label okay) {

    if (param.getType().getSort() != Type.OBJECT) {
        throw new AnnotationStateError("Unsupported type: " + param.getType());
    }/*from   www. j  a  v  a  2 s  . c  o m*/

    Label notOkay = new Label();

    // See if the param type matches a DeathStarPlans type
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn("DeathStarPlans|TheDeathStarPlans|.*[\\.$]DeathStarPlans|.*[\\.$]TheDeathStarPlans");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z");

    if (be) {
        // If the param type does not match a DeathStarPlans type, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type does not match a DeathStarPlans type, then that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // At this point, the param type matches a DeathStarPlans type.
    // So check if the param has an isStolen method.
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);

    mv.visitLabel(start);
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitLdcInsn("isStolen");
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");

    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");

    mv.visitJumpInsn(be ? TRUE : FALSE, okay);

    mv.visitLabel(end);
    mv.visitJumpInsn(GOTO, notOkay);
    mv.visitLabel(handler);
    mv.visitInsn(POP);

    mv.visitLabel(notOkay);
}

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitException(MethodVisitor mv, LocalVarInfo param, String message) {
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);

    StringBuilderVisitor sb = new StringBuilderVisitor(mv);
    sb.visitAppend(param.getName() + " = ");
    sb.visitAppend(param.getType(), param.getIndex());
    sb.visitAppend(message);/*from   ww w. j  av a  2 s  .c  o m*/
    sb.visitToString();

    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(ATHROW);
}

From source file:com.github.stokito.gag.instrument.util.StringBuilderVisitor.java

License:Apache License

public StringBuilderVisitor(MethodVisitor mv) {
    this.mv = mv;
    mv.visitTypeInsn(NEW, SB_NAME);// w  ww .  j a v  a  2 s. c  o m
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, SB_NAME, "<init>", "()V");
}

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   ww w .  ja v a 2s  .  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 ww  w  .ja  v  a  2s .  c o 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 w w  w  .  j  av a2 s .  c  o 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.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 . ja va  2  s. c  o  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) {/*  w  w  w.j  av  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++;

    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  va 2s.  c o  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);
}