Search in sources :

Example 1 with InterceptorDefinition

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

the class ASMMethod method addInterceptor0.

private void addInterceptor0(Interceptor interceptor, int interceptorId) {
    if (interceptor == null) {
        throw new NullPointerException("interceptor must not be null");
    }
    final InterceptorDefinition interceptorDefinition = this.interceptorDefinitionFactory.createInterceptorDefinition(interceptor.getClass());
    final Class<?> interceptorClass = interceptorDefinition.getInterceptorClass();
    final CaptureType captureType = interceptorDefinition.getCaptureType();
    if (this.methodNode.hasInterceptor()) {
        logger.warn("Skip adding interceptor. 'already intercepted method' class={}, interceptor={}", this.declaringClass.getName(), interceptorClass.getName());
        return;
    }
    if (this.methodNode.isAbstract() || this.methodNode.isNative()) {
        logger.warn("Skip adding interceptor. 'abstract or native method' class={}, interceptor={}", this.declaringClass.getName(), interceptorClass.getName());
        return;
    }
    int apiId = -1;
    if (interceptorDefinition.getInterceptorType() == InterceptorType.API_ID_AWARE) {
        apiId = this.apiMetaDataService.cacheApi(this.descriptor);
    }
    // add before interceptor.
    if (isBeforeInterceptor(captureType) && interceptorDefinition.getBeforeMethod() != null) {
        this.methodNode.addBeforeInterceptor(interceptorId, interceptorDefinition, apiId);
        this.declaringClass.setModified(true);
    } else {
        if (isDebug) {
            logger.debug("Skip adding before interceptorDefinition because the interceptorDefinition doesn't have before method: {}", interceptorClass.getName());
        }
    }
    // add after interface.
    if (isAfterInterceptor(captureType) && interceptorDefinition.getAfterMethod() != null) {
        this.methodNode.addAfterInterceptor(interceptorId, interceptorDefinition, apiId);
        this.declaringClass.setModified(true);
    } else {
        if (isDebug) {
            logger.debug("Skip adding after interceptor because the interceptor doesn't have after method: {}", interceptorClass.getName());
        }
    }
}
Also used : CaptureType(com.navercorp.pinpoint.profiler.instrument.interceptor.CaptureType) InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition)

Example 2 with InterceptorDefinition

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

the class ASMMethodNodeAdapterTest method isVisited.

@Test
public void isVisited() throws Exception {
    // init
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(ArgsArrayInterceptor.class);
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.ArgsClass";
    final MethodNode methodNode = ASMClassNodeLoader.get(targetClassName, "arg");
    ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(JavaAssistUtils.javaNameToJvmName(targetClassName), methodNode);
    assertEquals(false, adapter.hasInterceptor());
    adapter.addBeforeInterceptor(interceptorId, interceptorDefinition, -1);
    assertEquals(true, adapter.hasInterceptor());
}
Also used : InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) MethodNode(org.objectweb.asm.tree.MethodNode) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) JointPoint(com.navercorp.pinpoint.bootstrap.instrument.aspect.JointPoint) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory) Test(org.junit.Test)

Example 3 with InterceptorDefinition

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

the class ASMMethodVariablesTest method initInterceptorLocalVariables.

@Test
public void initInterceptorLocalVariables() throws Exception {
    MethodNode methodNode = ASMClassNodeLoader.get("com.navercorp.pinpoint.profiler.instrument.mock.ConstructorChildClass", "<init>");
    ASMMethodVariables variables = new ASMMethodVariables("com/navercorp/pinpoint/profiler/instrument/mock/ConstructorChildClass", methodNode);
    assertNull(variables.getEnterInsnNode());
    assertNull(variables.getEnterInsnNode());
    InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
    int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(ArgsArrayInterceptor.class);
    InsnList instructions = new InsnList();
    boolean first = variables.initInterceptorLocalVariables(instructions, interceptorId, interceptorDefinition, -1);
    assertEquals(true, first);
    assertNotNull(variables.getEnterInsnNode());
    assertNotNull(variables.getEnterInsnNode());
}
Also used : InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) DefaultInterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder) InterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder) DefaultInterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory) Test(org.junit.Test)

Example 4 with InterceptorDefinition

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

the class ASMMethodVariablesTest method hasInterceptor.

@Test
public void hasInterceptor() throws Exception {
    InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
    int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(ArgsArrayInterceptor.class);
    final ClassNode classNode = ASMClassNodeLoader.get("com.navercorp.pinpoint.profiler.instrument.mock.ArgsClass");
    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode methodNode : methodNodes) {
        ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
        assertEquals(false, methodNodeAdapter.hasInterceptor());
        methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, -1);
        assertEquals(true, methodNodeAdapter.hasInterceptor());
    }
}
Also used : InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) DefaultInterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder) InterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder) DefaultInterceptorRegistryBinder(com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory) Test(org.junit.Test)

Example 5 with InterceptorDefinition

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

the class ASMMethodNodeAdapterAddInterceptorTest method addInterceptor.

private Class addInterceptor(final int interceptorId, final String targetClassName, final Class<?> interceptorClass) {
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(interceptorClass);
    try {
        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) {
                    if (methodNode.name.equals("<clinit>")) {
                        continue;
                    }
                    ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                    if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
                        continue;
                    }
                    methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
                    methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
                }
            }
        });
        return classLoader.loadClass(targetClassName);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InterceptorDefinition(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition) ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) InterceptorDefinitionFactory(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory)

Aggregations

InterceptorDefinition (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition)5 InterceptorDefinitionFactory (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory)4 ArgsArrayInterceptor (com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor)3 Test (org.junit.Test)3 DefaultInterceptorRegistryBinder (com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder)2 InterceptorRegistryBinder (com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 JointPoint (com.navercorp.pinpoint.bootstrap.instrument.aspect.JointPoint)1 CaptureType (com.navercorp.pinpoint.profiler.instrument.interceptor.CaptureType)1 List (java.util.List)1 ClassNode (org.objectweb.asm.tree.ClassNode)1