Example usage for org.objectweb.asm MethodVisitor MethodVisitor

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

Introduction

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

Prototype

public MethodVisitor(final int api, final MethodVisitor methodVisitor) 

Source Link

Document

Constructs a new MethodVisitor .

Usage

From source file:org.jacoco.core.internal.instr.FrameTrackerTest.java

License:Open Source License

@After
public void verify() {
    MethodRecorder actual = new MethodRecorder();
    MethodVisitor noLabels = new MethodVisitor(Opcodes.ASM4, actual.getVisitor()) {
        @Override/*from w ww  .  j a v  a2s . com*/
        public void visitLabel(Label label) {
            // Ignore labels inserted by the tracker
        }
    };
    FrameTracker tracker = new FrameTracker("Test", ACC_STATIC, "test", "()V", noLabels);
    before.accept(tracker);
    mv.instructions.accept(tracker);
    tracker.insertFrame();

    MethodRecorder expected = new MethodRecorder();
    before.accept(expected.getVisitor());
    mv.instructions.accept(expected.getVisitor());
    after.accept(expected.getVisitor());

    assertEquals(expected, actual);
}

From source file:org.javaweb.utils.ASMClassUtils.java

License:Apache License

/**
 * ASM??,??????class/*  www . jav a2  s .c  om*/
 *
 * @param classFileBuffer ?
 * @return
 */
public Map<String, List<AnnotationNode>> getClassAnnotation(byte[] classFileBuffer) {
    final Map<String, List<AnnotationNode>> annotationNodeMap = new LinkedHashMap<String, List<AnnotationNode>>();
    final List<AnnotationNode> classAnnotationNodeList = new ArrayList<AnnotationNode>();
    final List<AnnotationNode> methodAnnotationNodeList = new ArrayList<AnnotationNode>();
    final List<AnnotationNode> fieldAnnotationNodeList = new ArrayList<AnnotationNode>();
    final ClassReader cr = new ClassReader(classFileBuffer);

    if (!ClassUtils.isInterface(cr)) {

        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);

        cr.accept(new ClassVisitor(Opcodes.ASM5, cw) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                AnnotationNode an = new AnnotationNode(desc);
                classAnnotationNodeList.add(an);

                return an;
            }

            @Override
            public MethodVisitor visitMethod(int access, String methodName, String argTypeDesc,
                    String signature, String[] exceptions) {
                final MethodVisitor mv = super.visitMethod(access, methodName, argTypeDesc, signature,
                        exceptions);
                return new MethodVisitor(Opcodes.ASM5, mv) {

                    @Override
                    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                        AnnotationNode an = new AnnotationNode(desc);
                        methodAnnotationNodeList.add(an);

                        return an;
                    }
                };
            }

            @Override
            public FieldVisitor visitField(int access, String methodName, String argTypeDesc, String signature,
                    final Object value) {
                final FieldVisitor fv = super.visitField(access, methodName, argTypeDesc, signature, value);
                return new FieldVisitor(Opcodes.ASM5, fv) {
                    @Override
                    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                        AnnotationNode an = new AnnotationNode(desc);
                        fieldAnnotationNodeList.add(an);

                        return an;
                    }
                };
            }
        }, ClassReader.EXPAND_FRAMES);
    }

    annotationNodeMap.put("class", classAnnotationNodeList);
    annotationNodeMap.put("method", methodAnnotationNodeList);
    annotationNodeMap.put("field", fieldAnnotationNodeList);

    return annotationNodeMap;
}

From source file:org.kantega.notsoserial.ReadObjectClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!isInterface() && isSerializable() && name.equals("readObject") && readObjectDescription.equals(desc)) {
        hasReadObject = true;/* w  w w .  j  av a2s .co m*/
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitCode() {
                super.visitCode();
                mv.visitLdcInsn(className);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                        Type.getType(NotSoSerialClassFileTransformer.class).getInternalName(),
                        onReadObjectCallbackMethod, "(Ljava/lang/String;)V", false);
            }
        };
    } else {
        return super.visitMethod(access, name, desc, signature, exceptions);
    }
}

From source file:org.kantega.revoc.demo.HelloWorldTransformCode.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
        @Override/*from w w  w .  j av a 2s . c  om*/
        public void visitLdcInsn(Object o) {
            if (o != null && o instanceof String && o.toString().equals("Hello world")) {
                super.visitLdcInsn("Hello Norway");
            } else {
                super.visitLdcInsn(o);
            }
        }
    };
}

From source file:org.springframework.cglib.core.ClassEmitter.java

License:Apache License

public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
    if (classInfo == null)
        throw new IllegalStateException("classInfo is null! " + this);
    MethodVisitor v = cv.visitMethod(access, sig.getName(), sig.getDescriptor(), null,
            TypeUtils.toInternalNames(exceptions));
    if (sig.equals(Constants.SIG_STATIC) && !TypeUtils.isInterface(getAccess())) {
        rawStaticInit = v;//from ww w  . j  av  a  2 s . com
        MethodVisitor wrapped = new MethodVisitor(Opcodes.ASM4, v) {
            public void visitMaxs(int maxStack, int maxLocals) {
                // ignore
            }

            public void visitInsn(int insn) {
                if (insn != Constants.RETURN) {
                    super.visitInsn(insn);
                }
            }
        };
        staticInit = new CodeEmitter(this, wrapped, access, sig, exceptions);
        if (staticHook == null) {
            // force static hook creation
            getStaticHook();
        } else {
            staticInit.invoke_static_this(staticHookSig);
        }
        return staticInit;
    } else if (sig.equals(staticHookSig)) {
        return new CodeEmitter(this, v, access, sig, exceptions) {
            public boolean isStaticHook() {
                return true;
            }
        };
    } else {
        return new CodeEmitter(this, v, access, sig, exceptions);
    }
}

From source file:org.yx.asm.MethodInfoClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {
    final Type[] args = Type.getArgumentTypes(desc);

    if (!name.equals(m.getName()) || !AsmUtils.sameType(args, m.getParameterTypes())) {
        return super.visitMethod(access, name, desc, signature, exceptions);
    }/*from w  ww  .j  a  v  a  2s.  co  m*/
    MethodVisitor v = super.visitMethod(access, name, desc, signature, exceptions);
    return new MethodVisitor(Vars.ASM_VER, v) {

        @Override
        public void visitLocalVariable(String name, String desc, String signature, Label start, Label end,
                int index) {

            super.visitLocalVariable(name, desc, signature, start, end, index);
            int argSize = m.getParameterTypes().length;
            if ("this".equals(name) || argNames.size() >= argSize || argNames.contains(name)) {
                return;
            }
            int k = argNames.size();
            if (!args[k].getDescriptor().equals(desc)) {

                Log.get("SYS.20")
                        .error("current desc should be " + args[k].getDescriptor() + ",but really is " + desc);
                return;
            }
            argNames.add(name);
            signatures.add(signature);
        }

    };

}