Example usage for org.objectweb.asm Opcodes ASM4

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

Introduction

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

Prototype

int ASM4

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

Click Source Link

Usage

From source file:com.sun.tdk.jcov.instrument.InvokeClassAdapter.java

License:Open Source License

public InvokeClassAdapter(final ClassVisitor cv, final InstrumentationParams params) {
    super(Opcodes.ASM4, cv);
    this.params = params;
}

From source file:com.sun.tdk.jcov.instrument.MethodAnnotationAdapter.java

License:Open Source License

MethodAnnotationAdapter(final MethodVisitor mv, final DataMethod method) {
    super(Opcodes.ASM4, mv);
    this.meth = method;
}

From source file:com.sun.tdk.jcov.instrument.OffsetRecordingMethodAdapter.java

License:Open Source License

public OffsetRecordingMethodAdapter(final MethodVisitor mv, final DataMethodWithBlocks method) {
    super(Opcodes.ASM4, mv);
    this.currentInstructionIndex = 0;
    this.bcis = new int[60];
    this.method = method;
}

From source file:com.sun.tdk.jcov.instrument.SavePointsMethodAdapter.java

License:Open Source License

public SavePointsMethodAdapter(final MethodVisitor mv, boolean isBegin) {
    super(Opcodes.ASM4, mv);
    this.isBegin = isBegin;
}

From source file:com.sun.tdk.jcov.instrument.StaticInvokeMethodAdapter.java

License:Open Source License

public StaticInvokeMethodAdapter(MethodVisitor mv, String className, String methName, int access,
        final InstrumentationParams params) {
    super(Opcodes.ASM4, mv);
    this.className = className;
    this.params = params;
    this.methName = methName;
}

From source file:com.taobao.android.builder.tools.asm.method.MethodReplaceClazzVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
    return new MethodReplaceMethodVisitor(Opcodes.ASM4, methodVisitor, methodStore, stringBuilder);
}

From source file:com.taobao.android.builder.tools.asm.MethodReplacer.java

License:Apache License

@Override
protected void handleClazz(JarFile jarFile, JarOutputStream jos, JarEntry ze, String pathName) {

    try {//from   w  w  w  .jav  a 2  s .  c  o m
        InputStream jarFileInputStream = jarFile.getInputStream(ze);

        ClassReader classReader = new ClassReader(jarFileInputStream);

        ClassWriter classWriter = new ClassWriter(0);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(jar.getAbsolutePath()).append(".").append(pathName);

        classReader.accept(new MethodReplaceClazzVisitor(Opcodes.ASM4, classWriter, methodStore, stringBuilder),
                ClassReader.EXPAND_FRAMES);

        ByteArrayInputStream inputStream = new ByteArrayInputStream(classWriter.toByteArray());
        copyStreamToJar(inputStream, jos, pathName, ze.getTime());
        IOUtils.closeQuietly(inputStream);

    } catch (Throwable e) {

        System.err.println("[MethodReplacer] rewrite error > " + pathName);
        justCopy(jarFile, jos, ze, pathName);
    }

}

From source file:com.taobao.asm.AsmExample.java

License:Apache License

public static void main(String[] args) throws IOException, IllegalArgumentException, SecurityException,
        IllegalAccessException, InvocationTargetException {

    ClassReader cr = new ClassReader(Foo.class.getName());
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new MethodChangeClassAdapter(cw);
    cr.accept(cv, Opcodes.ASM4);

    //  /* w w w  .  j a v  a 2s.  co m*/
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null);
    // pushes the 'out' field (of type PrintStream) of the System class  
    mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant  
    mw.visitLdcInsn("this is add method print!");
    // invokes the 'println' method (defined in the PrintStream class)  
    mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local  
    // variables  
    mw.visitMaxs(0, 0);
    mw.visitEnd();

    // gets the bytecode of the Example class, and loads it dynamically  
    byte[] code = cw.toByteArray();

    AsmExample loader = new AsmExample();
    Class<?> exampleClass = loader.defineClass(Foo.class.getName(), code, 0, code.length);

    for (Method method : exampleClass.getMethods()) {
        System.out.println(method);
    }

    System.out.println("*************");

    // uses the dynamically generated class to print 'Helloworld'  
    exampleClass.getMethods()[0].invoke(null); //changeMethodContent

    System.out.println("*************");

    exampleClass.getMethods()[1].invoke(null); //execute,??

    // gets the bytecode of the Example class, and loads it dynamically  

    FileOutputStream fos = new FileOutputStream("e:\\logs\\Example.class");
    fos.write(code);
    fos.close();
}

From source file:com.tencent.tinker.build.immutable.ClassSimDef.java

License:Open Source License

public void init() {
    methodCount = 0;/*w w w.  ja  va  2s  . co  m*/
    fieldCount = 0;

    ClassReader cr = new ClassReader(bytes);
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM4) {
        String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            className = name;
            super.visit(version, access, name, signature, superName, interfaces);
        }

        @Override
        public MethodVisitor visitMethod(int access, String mtdName, String mtdDesc, String mtdSig,
                String[] exceptions) {

            String defMtd = className + ":" + mtdName + ":" + mtdDesc;
            if (!refMtdSet.contains(defMtd)) {
                refMtdSet.add(defMtd);
                methodCount++;
            }

            MethodVisitor mv = super.visitMethod(access, mtdName, mtdDesc, mtdSig, exceptions);
            mv = new MethodVisitor(Opcodes.ASM4, mv) {
                @Override
                public void visitFieldInsn(int opcode, String owner, String fName, String fDesc) {
                    String invokeField = owner + ":" + fName + ":" + fDesc;
                    if (!refFieldSet.contains(invokeField)) {
                        refFieldSet.add(invokeField);
                        fieldCount++;
                    }
                    super.visitFieldInsn(opcode, owner, fName, fDesc);
                }

                @Override
                public void visitMethodInsn(int opcode, String owner, String mName, String mDesc) {
                    String invokeMtd = owner + ":" + mName + ":" + mDesc;
                    if (!refMtdSet.contains(invokeMtd)) {
                        refMtdSet.add(invokeMtd);
                        methodCount++;
                    }
                    super.visitMethodInsn(opcode, owner, mName, mDesc);
                }
            };
            return mv;
        }

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            String fieldDesc = className + ":" + name + ":" + desc;
            if (!refFieldSet.contains(fieldDesc)) {
                refFieldSet.add(fieldDesc);
                fieldCount++;
            }
            return super.visitField(access, name, desc, signature, value);
        }
    };
    cr.accept(cv, 0);
}

From source file:com.test.api.checker.tests.ASMHelloWorld.java

License:Apache License

/**
 * @param args//from   w  w  w .  j a v  a2  s.co m
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    ClassVisitor cl = new ClassVisitor(Opcodes.ASM4) {

        /**
         * Called when a class is visited. This is the method called first
         */
        @Override
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            System.out.println("Visiting class: " + name);
            System.out.println("Class Major Version: " + version);
            System.out.println("Super class: " + superName);
            super.visit(version, access, name, signature, superName, interfaces);
        }

        /**
         * Invoked only when the class being visited is an inner class
         */
        @Override
        public void visitOuterClass(String owner, String name, String desc) {
            System.out.println("Outer class: " + owner);
            super.visitOuterClass(owner, name, desc);
        }

        /**
         *Invoked when a class level annotation is encountered
         */
        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            System.out.println("Annotation: " + desc);
            return super.visitAnnotation(desc, visible);
        }

        /**
         * When a class attribute is encountered
         */
        @Override
        public void visitAttribute(Attribute attr) {
            System.out.println("Class Attribute: " + attr.type);
            super.visitAttribute(attr);
        }

        /**
         *When an inner class is encountered
         */
        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            System.out.println("Inner Class: " + innerName + " defined in " + outerName);
            super.visitInnerClass(name, outerName, innerName, access);
        }

        /**
         * When a field is encountered
         */
        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            System.out.println("Field: " + name + " " + desc + " value:" + value);
            return super.visitField(access, name, desc, signature, value);
        }

        @Override
        public void visitEnd() {
            System.out.println("Method ends here");
            super.visitEnd();
        }

        /**
         * When a method is encountered
         */
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            System.out.println("Method: " + name + " " + desc);
            return super.visitMethod(access, name, desc, signature, exceptions);
        }

        /**
         * When the optional source is encountered
         */
        @Override
        public void visitSource(String source, String debug) {
            System.out.println("Source: " + source);
            super.visitSource(source, debug);
        }

    };
    InputStream in = ASMHelloWorld.class.getResourceAsStream("/java/lang/String.class");
    ClassReader classReader = new ClassReader(in);
    classReader.accept(cl, 0);

}