Search in sources :

Example 1 with ASMMethodNodeAdapter

use of com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter in project pinpoint by naver.

the class MethodInterfaceTest method addInterceptor.

@Test
public void addInterceptor() throws Exception {
    final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
    final String targetClassName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterfaceClass";
    logger.debug("Add interceptor interface={}, class={}", targetInterfaceName, targetClassName);
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new SimpleInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(SimpleInterceptor.class);
    final List<String> methodNameList = Arrays.asList("currentTimeMillis", "foo");
    TestClassLoader classLoader = new TestClassLoader();
    classLoader.addTargetClassName(targetClassName);
    classLoader.addTargetClassName(targetInterfaceName);
    classLoader.setCallbackHandler(new CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                logger.debug("Handle class={}, method={}", classNode.name, methodNode.name);
                if (methodNode.name.equals("<clinit>")) {
                    continue;
                }
                ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
                    continue;
                }
                if (!methodNameList.contains(methodNode.name)) {
                    continue;
                }
                methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
                logger.debug("Add before interceptor in method={}", methodNode.name);
                methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
                logger.debug("Add after interceptor in method={}", methodNode.name);
            }
        }
    });
    // static method
    Assert.assertFalse(SimpleInterceptor.before);
    logger.debug("Interface static method");
    Class<?> clazz = classLoader.loadClass(targetInterfaceName);
    Method method = clazz.getDeclaredMethod("currentTimeMillis");
    method.invoke(null);
    assertTrue(SimpleInterceptor.before);
    // reset
    SimpleInterceptor.before = false;
    // default method
    Assert.assertFalse(SimpleInterceptor.before);
    logger.debug("Interface default method");
    clazz = classLoader.loadClass(targetClassName);
    method = clazz.getDeclaredMethod("bar");
    method.invoke(clazz.newInstance());
    assertTrue(SimpleInterceptor.before);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ASMMethodNodeAdapter(com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter) Method(java.lang.reflect.Method) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory) InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) MethodNode(org.objectweb.asm.tree.MethodNode) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 2 with ASMMethodNodeAdapter

use of com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter in project pinpoint by naver.

the class MethodInterfaceTest method addMethod.

@Test
public void addMethod() throws Exception {
    final MethodNode methodNode = TestClassLoader.get("com.navercorp.test.pinpoint.jdk8.interfaces.SimpleClass", "welcome");
    final ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter("com/navercorp/test/pinpoint/jdk8/interfaces/SimpleClass", methodNode);
    final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
    TestClassLoader classLoader = new TestClassLoader();
    classLoader.addTargetClassName(targetInterfaceName);
    classLoader.setTrace(false);
    classLoader.setCallbackHandler(new CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            logger.debug("Add method class={}", classNode.name);
            ASMClassNodeAdapter classNodeAdapter = new ASMClassNodeAdapter(pluginContext, null, null, classNode);
            classNodeAdapter.copyMethod(methodNodeAdapter);
        }
    });
    logger.debug("Interface static method");
    Class<?> clazz = classLoader.loadClass(targetInterfaceName);
    Method method = clazz.getDeclaredMethod("welcome");
    method.invoke(null);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ASMMethodNodeAdapter(com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter) MethodNode(org.objectweb.asm.tree.MethodNode) ASMClassNodeAdapter(com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ASMMethodNodeAdapter (com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter)2 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 ClassNode (org.objectweb.asm.tree.ClassNode)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 ASMClassNodeAdapter (com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter)1 InterceptorDefinition (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition)1 InterceptorDefinitionFactory (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1