Example usage for org.objectweb.asm Opcodes ASM5

List of usage examples for org.objectweb.asm Opcodes ASM5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM5.

Prototype

int ASM5

To view the source code for org.objectweb.asm Opcodes ASM5.

Click Source Link

Usage

From source file:javadepchecker.Main.java

License:Open Source License

/**
 * Empty Constructor, sets ASM op code version
 */
public Main() {
    super(Opcodes.ASM5);
}

From source file:javadepchecker.Main.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    for (Type param : Type.getArgumentTypes(desc)) {
        addDep(param);/* w  w w.ja v a 2 s .c  om*/
    }

    if (exceptions != null) {
        for (String exception : exceptions) {
            addDep(exception);
        }
    }
    addDep(Type.getReturnType(desc));
    return new MethodVisitor(Opcodes.ASM5) {
        @Override
        public void visitLocalVariable(String name, String desc, String signature, Label start, Label end,
                int index) {
            addDep(Type.getType(desc));
        }

        @Override
        public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            addDep(Type.getObjectType(owner));
            addDep(Type.getType(desc));
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            addDep(Type.getObjectType(owner));
        }

        @Override
        public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
            return Main.this.visitAnnotation(desc, visible);
        }
    };
}

From source file:lombok.bytecode.AsmUtil.java

License:Open Source License

static byte[] fixJSRInlining(byte[] byteCode) {
    ClassReader reader = new ClassReader(byteCode);
    ClassWriter writer = new FixedClassWriter(reader, 0);

    ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5, writer) {
        @Override//from  w ww.  j  ava  2s.  co m
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access,
                    name, desc, signature, exceptions);
        }
    };

    reader.accept(visitor, 0);
    return writer.toByteArray();
}

From source file:lombok.bytecode.PreventNullAnalysisRemover.java

License:Open Source License

@Override
public byte[] applyTransformations(byte[] original, String fileName, DiagnosticsReceiver diagnostics) {
    if (!new ClassFileMetaData(original).usesMethod("lombok/Lombok", "preventNullAnalysis"))
        return null;

    byte[] fixedByteCode = fixJSRInlining(original);

    ClassReader reader = new ClassReader(fixedByteCode);
    ClassWriter writer = new FixedClassWriter(reader, 0);

    final AtomicBoolean changesMade = new AtomicBoolean();

    class PreventNullAnalysisVisitor extends MethodVisitor {
        PreventNullAnalysisVisitor(MethodVisitor mv) {
            super(Opcodes.ASM5, mv);
        }//  ww  w.j av  a 2s .co m

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            boolean hit = true;
            if (hit && opcode != Opcodes.INVOKESTATIC)
                hit = false;
            if (hit && !"preventNullAnalysis".equals(name))
                hit = false;
            if (hit && !"lombok/Lombok".equals(owner))
                hit = false;
            if (hit && !"(Ljava/lang/Object;)Ljava/lang/Object;".equals(desc))
                hit = false;
            if (hit) {
                changesMade.set(true);
                if (System.getProperty("lombok.debugAsmOnly", null) != null)
                    super.visitMethodInsn(opcode, owner, name, desc, itf); // DEBUG for issue 470!
            } else {
                super.visitMethodInsn(opcode, owner, name, desc, itf);
            }
        }
    }

    reader.accept(new ClassVisitor(Opcodes.ASM5, writer) {
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new PreventNullAnalysisVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }
    }, 0);
    return changesMade.get() ? writer.toByteArray() : null;
}

From source file:lombok.bytecode.SneakyThrowsRemover.java

License:Open Source License

@Override
public byte[] applyTransformations(byte[] original, String fileName, final DiagnosticsReceiver diagnostics) {
    if (!new ClassFileMetaData(original).usesMethod("lombok/Lombok", "sneakyThrow"))
        return null;

    byte[] fixedByteCode = fixJSRInlining(original);

    ClassReader reader = new ClassReader(fixedByteCode);
    ClassWriter writer = new ClassWriter(reader, 0);

    final AtomicBoolean changesMade = new AtomicBoolean();

    class SneakyThrowsRemoverVisitor extends MethodVisitor {
        SneakyThrowsRemoverVisitor(MethodVisitor mv) {
            super(Opcodes.ASM5, mv);
        }/*from w  w  w . j a  va 2 s  .  c  o  m*/

        private boolean methodInsnQueued = false;

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            if (opcode == Opcodes.INVOKESTATIC && "sneakyThrow".equals(name) && "lombok/Lombok".equals(owner)
                    && "(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;".equals(desc)) {

                if (System.getProperty("lombok.debugAsmOnly", null) != null) {
                    super.visitMethodInsn(opcode, owner, name, desc, itf); // DEBUG for issue 470!
                } else {
                    methodInsnQueued = true;
                }
            } else {
                super.visitMethodInsn(opcode, owner, name, desc, itf);
            }
        }

        private void handleQueue() {
            if (!methodInsnQueued)
                return;
            super.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/Lombok", "sneakyThrow",
                    "(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;", false);
            methodInsnQueued = false;
            diagnostics.addWarning(
                    "Proper usage is: throw lombok.Lombok.sneakyThrow(someException);. You did not 'throw' it. Because of this, the call to sneakyThrow "
                            + "remains in your classfile and you will need lombok.jar on the classpath at runtime.");
        }

        @Override
        public void visitInsn(int arg0) {
            if (methodInsnQueued && arg0 == Opcodes.ATHROW) {
                changesMade.set(true);
                // As expected, the required ATHROW. We can now safely 'eat' the previous call.
                methodInsnQueued = false;
            }
            handleQueue();
            super.visitInsn(arg0);
        }

        @Override
        public void visitFrame(int arg0, int arg1, Object[] arg2, int arg3, Object[] arg4) {
            handleQueue();
            super.visitFrame(arg0, arg1, arg2, arg3, arg4);
        }

        @Override
        public void visitIincInsn(int arg0, int arg1) {
            handleQueue();
            super.visitIincInsn(arg0, arg1);
        }

        @Override
        public void visitFieldInsn(int arg0, String arg1, String arg2, String arg3) {
            handleQueue();
            super.visitFieldInsn(arg0, arg1, arg2, arg3);
        }

        @Override
        public void visitIntInsn(int arg0, int arg1) {
            handleQueue();
            super.visitIntInsn(arg0, arg1);
        }

        @Override
        public void visitEnd() {
            handleQueue();
            super.visitEnd();
        }

        @Override
        public void visitInvokeDynamicInsn(String arg0, String arg1, Handle arg2, Object... arg3) {
            handleQueue();
            super.visitInvokeDynamicInsn(arg0, arg1, arg2, arg3);
        }

        @Override
        public void visitLabel(Label arg0) {
            handleQueue();
            super.visitLabel(arg0);
        }

        @Override
        public void visitJumpInsn(int arg0, Label arg1) {
            handleQueue();
            super.visitJumpInsn(arg0, arg1);
        }

        @Override
        public void visitLdcInsn(Object arg0) {
            handleQueue();
            super.visitLdcInsn(arg0);
        }

        @Override
        public void visitLocalVariable(String arg0, String arg1, String arg2, Label arg3, Label arg4,
                int arg5) {
            handleQueue();
            super.visitLocalVariable(arg0, arg1, arg2, arg3, arg4, arg5);
        }

        @Override
        public void visitMaxs(int arg0, int arg1) {
            handleQueue();
            super.visitMaxs(arg0, arg1);
        }

        @Override
        public void visitLookupSwitchInsn(Label arg0, int[] arg1, Label[] arg2) {
            handleQueue();
            super.visitLookupSwitchInsn(arg0, arg1, arg2);
        }

        @Override
        public void visitMultiANewArrayInsn(String arg0, int arg1) {
            handleQueue();
            super.visitMultiANewArrayInsn(arg0, arg1);
        }

        @Override
        public void visitVarInsn(int arg0, int arg1) {
            handleQueue();
            super.visitVarInsn(arg0, arg1);
        }

        @Override
        public void visitTryCatchBlock(Label arg0, Label arg1, Label arg2, String arg3) {
            handleQueue();
            super.visitTryCatchBlock(arg0, arg1, arg2, arg3);
        }

        @Override
        public void visitTableSwitchInsn(int arg0, int arg1, Label arg2, Label... arg3) {
            handleQueue();
            super.visitTableSwitchInsn(arg0, arg1, arg2, arg3);
        }

        @Override
        public void visitTypeInsn(int arg0, String arg1) {
            handleQueue();
            super.visitTypeInsn(arg0, arg1);
        }
    }

    reader.accept(new ClassVisitor(Opcodes.ASM5, writer) {
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new SneakyThrowsRemoverVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }
    }, 0);
    return changesMade.get() ? writer.toByteArray() : null;
}

From source file:name.martingeisse.minimal.simulation.MCodeMain.java

License:Open Source License

/**
 * The main method.//from w  w  w  .ja  va2s  .c  o  m
 * @param args command-line arguments
 * @throws Exception ...
 */
public static void main(final String[] args) throws Exception {

    // compile
    ImmutableList<MCodeEntry> code = null;
    {
        final ClassNode node = new ClassNode();
        new ClassReader(Experiment1.class.getName()).accept(node, Opcodes.ASM5);
        for (final Object untypedMethodNode : node.methods) {
            final MethodNode methodNode = (MethodNode) untypedMethodNode;
            if (methodNode.name.equals("main")) {
                final Compiler compiler = new Compiler();
                code = compiler.compile(methodNode);
                break;
            }
        }
        if (code == null) {
            throw new RuntimeException("could not find main method to compile");
        }
    }

    // run
    final LwjglWindow window = new LwjglWindow(800, 600);
    final Subject subject = new FullySimulatedSubject(window);
    final DirectMCodeEngine engine = new DirectMCodeEngine(ImmutableList.copyOf(code),
            new SubjectNativeCallHandler(subject));
    engine.run();

}

From source file:net.caseif.iinjector.IInjectorClassVisitor.java

License:BSD License

public IInjectorClassVisitor(ClassWriter classWriter, Set<String> interfaces) {
    super(Opcodes.ASM5, classWriter);
    this.writer = classWriter;
    this.interfaces = interfaces;
}

From source file:net.enilink.composition.asm.ExtendedClassNode.java

License:Open Source License

/**
 * Creates an {@link ExtendedClassNode}.
 * //  w ww.ja  v a 2  s  . co  m
 * @param type
 *            Java type that gets defined by this class node.
 * @param parentClass
 *            The direct super class or the primary interface for this class
 *            node.
 * @param parentClassInfo
 *            Optional meta information for <code>parentClass</code>.
 */
public ExtendedClassNode(Type type, Class<?> parentClass, ClassInfo parentClassInfo) {
    super(Opcodes.ASM5);
    String[] interfaces = new String[parentClass.getInterfaces().length];
    int i = 0;
    for (Class<?> face : parentClass.getInterfaces()) {
        interfaces[i++] = Type.getInternalName(face);
    }

    Class<?> superClass = parentClass.isInterface() ? Object.class : parentClass;
    visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, type.getInternalName(), null, Type.getInternalName(superClass),
            interfaces);

    if (parentClassInfo != null) {
        parentClassInfo.copyAnnotations(this);
    }

    this.type = type;
    this.parentClass = parentClass;
    parentType = Type.getType(parentClass);
}

From source file:net.enilink.composition.asm.ExtendedMethod.java

License:Open Source License

public ExtendedMethod(ExtendedClassNode owner, int access, String name, String desc, String signature,
        String[] exceptions) {/* w  w w  . j a  v  a2 s  .  co m*/
    super(Opcodes.ASM5, access, name, desc, signature, exceptions);
    this.owner = owner;
}

From source file:net.enilink.composition.asm.ExtendedMethod.java

License:Open Source License

public ExtendedMethod(ExtendedClassNode owner, Method method) {
    super(Opcodes.ASM5, method.getModifiers(), method.getName(), Type.getMethodDescriptor(method), null, null);
    this.owner = owner;
    this.method = method;
}