Search in sources :

Example 76 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project dex2jar by pxb1988.

the class TestUtils method verify.

@SuppressWarnings("rawtypes")
public static void verify(final ClassReader cr, PrintWriter out) throws AnalyzerException, IllegalArgumentException, IllegalAccessException {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
    List methods = cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        List tryCatchBlocks = method.tryCatchBlocks;
        for (int j = 0; j < tryCatchBlocks.size(); j++) {
            TryCatchBlockNode tcn = (TryCatchBlockNode) tryCatchBlocks.get(j);
            if (tcn.start.equals(tcn.end)) {
                throw new DexException("try/catch block %d in %s has same start(%s) and end(%s)", j, method.name, tcn.start.getLabel(), tcn.end.getLabel());
            }
        }
        BasicVerifier verifier = new BasicVerifier();
        Analyzer a = new Analyzer(verifier);
        try {
            a.analyze(cn.name, method);
        } catch (Exception e) {
            out.println(cr.getClassName() + "." + method.name + method.desc);
            printAnalyzerResult(method, a, out);
            e.printStackTrace(out);
            out.flush();
            throw new DexException("method " + method.name + " " + method.desc, e);
        }
    }
}
Also used : BasicVerifier(org.objectweb.asm.tree.analysis.BasicVerifier) ClassNode(org.objectweb.asm.tree.ClassNode) DexClassNode(com.googlecode.d2j.node.DexClassNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) DexException(com.googlecode.d2j.DexException) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) List(java.util.List) ArrayList(java.util.ArrayList) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException) ParseException(com.android.dx.cf.iface.ParseException)

Example 77 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMMethodNodeTest method addInterceptor.

@Test
public void addInterceptor() throws Exception {
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    final ASMClass declaringClass = mock(ASMClass.class);
    when(declaringClass.getName()).thenReturn(targetClassName);
    final EngineComponent engineComponent = mock(EngineComponent.class);
    when(engineComponent.createInterceptorDefinition(any(Class.class))).thenAnswer(new Answer<InterceptorDefinition>() {

        @Override
        public InterceptorDefinition answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            Class<?> clazz = (Class) arguments[0];
            return interceptorDefinitionFactory.createInterceptorDefinition(clazz);
        }
    });
    ASMClassNodeLoader.TestClassLoader classLoader = ASMClassNodeLoader.getClassLoader();
    final InstrumentException[] exception = new InstrumentException[1];
    classLoader.setTrace(false);
    classLoader.setVerify(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethod method = new ASMMethod(engineComponent, pluginContext, declaringClass, methodNode);
                try {
                    method.addInterceptor(interceptorId);
                } catch (InstrumentException e) {
                    exception[0] = e;
                    e.printStackTrace();
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum", int.class);
    assertEquals(55, method.invoke(clazz.newInstance(), 10));
    assertNull(exception[0]);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) Method(java.lang.reflect.Method) InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) MethodNode(org.objectweb.asm.tree.MethodNode) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) Test(org.junit.Test)

Example 78 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMClassNodeLoader method get.

public static MethodNode get(final String classInternalName, final String methodName) throws Exception {
    ClassNode classNode = get(classInternalName);
    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode methodNode : methodNodes) {
        if (methodNode.name.equals(methodName)) {
            return methodNode;
        }
    }
    return null;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode)

Example 79 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMMethodNodeAdapterTest method hasAnnotation.

@Test
public void hasAnnotation() throws Exception {
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.AnnotationClass";
    final MethodNode methodNode = ASMClassNodeLoader.get(targetClassName, "pointCut");
    ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(JavaAssistUtils.javaNameToJvmName(targetClassName), methodNode);
    assertEquals(true, adapter.hasAnnotation(PointCut.class));
    assertEquals(false, adapter.hasAnnotation(JointPoint.class));
}
Also used : PointCut(com.navercorp.pinpoint.bootstrap.instrument.aspect.PointCut) MethodNode(org.objectweb.asm.tree.MethodNode) JointPoint(com.navercorp.pinpoint.bootstrap.instrument.aspect.JointPoint) Test(org.junit.Test)

Example 80 with MethodNode

use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.

the class ASMMethodNodeAdapterRenameTest method rename.

@Test
public void rename() throws Exception {
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    classLoader.setTrace(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                if (!adapter.isConstructor()) {
                    adapter.rename(adapter.getName() + "_rename");
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum_rename", int.class);
    method.invoke(clazz.newInstance(), 10);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17