Search in sources :

Example 31 with InstrumentMethod

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.

the class InvokeAfterCodeGeneratorTest method testGenerate_AroundInterceptor0.

@Test
public void testGenerate_AroundInterceptor0() throws Exception {
    final Class<AroundInterceptor0> aroundInterceptor3Class = AroundInterceptor0.class;
    final InterceptorDefinition interceptorDefinition = interceptorDefinitionFactory.createInterceptorDefinition(aroundInterceptor3Class);
    final InstrumentClass mockClass = mock(InstrumentClass.class);
    Mockito.when(mockClass.getName()).thenReturn("TestClass");
    final InstrumentMethod mockMethod = mock(InstrumentMethod.class);
    Mockito.when(mockMethod.getName()).thenReturn("TestMethod");
    Mockito.when(mockMethod.getParameterTypes()).thenReturn(new String[] {});
    Mockito.when(mockMethod.getReturnType()).thenReturn("java.lang.Object");
    ApiMetaDataService apiMetaDataService = mock(ApiMetaDataService.class);
    final InvokeAfterCodeGenerator invokeAfterCodeGenerator = new InvokeAfterCodeGenerator(100, interceptorDefinition, mockClass, mockMethod, apiMetaDataService, false, true);
    final String generate = invokeAfterCodeGenerator.generate();
    logger.debug("testGenerate_AroundInterceptor0:{}", generate);
    Assert.assertFalse(generate.contains("($w)$1"));
    Assert.assertFalse(generate.contains("($w)$2"));
    Assert.assertFalse(generate.contains("($w)$3"));
    Assert.assertTrue(generate.contains("$e"));
}
Also used : AroundInterceptor0(com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor0) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ApiMetaDataService(com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService) Test(org.junit.Test)

Example 32 with InstrumentMethod

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.

the class ASMMethodNodeAdapterTestMain method addInterceptor.

private void addInterceptor(final String className) throws Exception {
    final boolean trace = false;
    final boolean verify = false;
    final String classInternalName = JavaAssistUtils.jvmNameToJavaName(className);
    ClassLoader classLoader = new ClassLoader() {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (!name.startsWith("java") && !name.startsWith("sun") && super.findLoadedClass(name) == null) {
                try {
                    ClassNode classNode = ASMClassNodeLoader.get(JavaAssistUtils.javaNameToJvmName(name));
                    ObjectBinderFactory objectBinderFactory = mock(ObjectBinderFactory.class);
                    ApiMetaDataService apiMetaDataService = mock(ApiMetaDataService.class);
                    ASMClass asmClass = new ASMClass(objectBinderFactory, null, interceptorRegistryBinder, apiMetaDataService, null, classNode);
                    if (asmClass.isInterceptable()) {
                        for (InstrumentMethod method : asmClass.getDeclaredMethods()) {
                            try {
                                method.addInterceptor(interceptorId);
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                    byte[] bytes = asmClass.toBytecode();
                    if (trace) {
                        ClassReader classReader = new ClassReader(bytes);
                        ClassWriter cw = new ClassWriter(0);
                        TraceClassVisitor tcv = new TraceClassVisitor(cw, new PrintWriter(System.out));
                        classReader.accept(tcv, 0);
                    }
                    if (verify) {
                        CheckClassAdapter.verify(new ClassReader(bytes), false, new PrintWriter(System.out));
                    }
                    return super.defineClass(name, bytes, 0, bytes.length);
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    return null;
                }
            } else {
                try {
                    return super.loadClass(name);
                } catch (Throwable t) {
                    t.printStackTrace();
                    return null;
                }
            }
        }
    };
    try {
        classLoader.loadClass(classInternalName);
    } catch (ClassNotFoundException cnfe) {
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ObjectBinderFactory(com.navercorp.pinpoint.profiler.objectfactory.ObjectBinderFactory) ApiMetaDataService(com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 33 with InstrumentMethod

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.

the class ConstructorTransformer method edit.

@Override
public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
    InstrumentMethod targetConstructor = target.getConstructor(targetParameterTypes);
    if (targetConstructor == null) {
        if (ignoreIfNotExist) {
            return;
        } else {
            Exception e = new NoSuchMethodException("No such constructor: " + "(" + Arrays.deepToString(targetParameterTypes) + ")");
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), "init", targetParameterTypes, e);
                logger.info("Cannot find target constructor with parameter types (" + Arrays.deepToString(targetParameterTypes) + ") but MethodTransformerExceptionHandler handled it.");
                return;
            } else {
                throw new InstrumentException("Fail to edit constructor", e);
            }
        }
    }
    for (MethodRecipe recipe : recipes) {
        try {
            recipe.edit(classLoader, target, targetConstructor);
        } catch (Throwable t) {
            logger.info("Exception thrown while editing " + targetConstructor.getDescriptor().getApiDescriptor(), t);
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), "init", targetParameterTypes, t);
                logger.info("Exception thrown while editing" + targetConstructor.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t);
            } else {
                throw new InstrumentException("Fail to edit constructor " + targetConstructor.getDescriptor().getApiDescriptor(), t);
            }
        }
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)

Aggregations

InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)33 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)29 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)21 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)19 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)19 ProtectionDomain (java.security.ProtectionDomain)19 Test (org.junit.Test)9 ApiMetaDataService (com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService)6 AroundInterceptor3 (com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor3)4 InstrumentContext (com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext)3 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)3 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)3 InstrumentEngine (com.navercorp.pinpoint.profiler.instrument.InstrumentEngine)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 List (java.util.List)2 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 MethodDescriptor (com.navercorp.pinpoint.bootstrap.context.MethodDescriptor)1 TraceContext (com.navercorp.pinpoint.bootstrap.context.TraceContext)1 AroundInterceptor0 (com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor0)1 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)1