Search in sources :

Example 1 with TestClassLoader

use of com.navercorp.pinpoint.test.classloader.TestClassLoader in project pinpoint by naver.

the class JavassistClassTest method testBeforeAddInterceptor.

@Test
public void testBeforeAddInterceptor() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String javassistClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
    loader.addTransformer(javassistClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify className:{} cl:{}", className, classLoader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, javassistClassName, classfileBuffer);
                String methodName = "callA";
                InstrumentMethod callaMethod = aClass.getDeclaredMethod(methodName);
                callaMethod.addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
                byte[] bytes = aClass.toBytecode();
                return bytes;
            } catch (Throwable e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    logger.info("class cl:{}", testObjectClazz.getClassLoader());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Interceptor interceptor = getInterceptor(loader, 0);
    assertEqualsIntField(interceptor, "call", 1);
    assertEqualsObjectField(interceptor, "className", "com.navercorp.pinpoint.test.javasssit.mock.TestObject");
    assertEqualsObjectField(interceptor, "methodName", methodName);
    assertEqualsObjectField(interceptor, "args", null);
    assertEqualsObjectField(interceptor, "target", testObject);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Interceptor(com.navercorp.pinpoint.bootstrap.interceptor.Interceptor) Test(org.junit.Test)

Example 2 with TestClassLoader

use of com.navercorp.pinpoint.test.classloader.TestClassLoader in project pinpoint by naver.

the class JavassistClassTest method testAddAfterInterceptor.

@Ignore
@Test
public void testAddAfterInterceptor() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String testClassObject = "com.navercorp.pinpoint.test.javasssit.mock.TestObject2";
    loader.addTransformer(testClassObject, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", classLoader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, testClassObject, classfileBuffer);
                String methodName = "callA";
                aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.profiler.interceptor.TestAfterInterceptor");
                String methodName2 = "callB";
                aClass.getDeclaredMethod(methodName2).addInterceptor("com.navercorp.pinpoint.profiler.interceptor.TestAfterInterceptor");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(testClassObject);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    Object result = callA.invoke(testObject);
    Interceptor interceptor = getInterceptor(loader, 0);
    assertEqualsIntField(interceptor, "call", 1);
    assertEqualsObjectField(interceptor, "className", testClassObject);
    assertEqualsObjectField(interceptor, "methodName", methodName);
    assertEqualsObjectField(interceptor, "args", null);
    assertEqualsObjectField(interceptor, "target", testObject);
    assertEqualsObjectField(interceptor, "result", result);
    final String methodName2 = "callB";
    Method callBMethod = testObject.getClass().getMethod(methodName2);
    callBMethod.invoke(testObject);
    Interceptor interceptor2 = getInterceptor(loader, 1);
    assertEqualsIntField(interceptor2, "call", 1);
    assertEqualsObjectField(interceptor2, "className", testClassObject);
    assertEqualsObjectField(interceptor2, "methodName", methodName2);
    assertEqualsObjectField(interceptor2, "args", null);
    assertEqualsObjectField(interceptor2, "target", testObject);
    assertEqualsObjectField(interceptor2, "result", null);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Interceptor(com.navercorp.pinpoint.bootstrap.interceptor.Interceptor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with TestClassLoader

use of com.navercorp.pinpoint.test.classloader.TestClassLoader in project pinpoint by naver.

the class JavassistClassTest method getTestClassLoader.

private TestClassLoader getTestClassLoader() {
    PLoggerFactory.initialize(new Slf4jLoggerBinder());
    DefaultProfilerConfig profilerConfig = new DefaultProfilerConfig();
    profilerConfig.setApplicationServerType(ServiceType.TEST_STAND_ALONE.getName());
    MockApplicationContext applicationContext = MockApplicationContext.of(profilerConfig);
    TestClassLoader testClassLoader = new TestClassLoader(applicationContext);
    testClassLoader.initialize();
    return testClassLoader;
}
Also used : TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) DefaultProfilerConfig(com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig) Slf4jLoggerBinder(com.navercorp.pinpoint.profiler.logging.Slf4jLoggerBinder) MockApplicationContext(com.navercorp.pinpoint.test.MockApplicationContext)

Example 4 with TestClassLoader

use of com.navercorp.pinpoint.test.classloader.TestClassLoader in project pinpoint by naver.

the class AccessorInjectionTest method addTraceValue.

@Test
public void addTraceValue() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String javassistClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
    loader.addTransformer(javassistClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", loader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(loader, javassistClassName, classfileBuffer);
                aClass.addField(ObjectTraceValue.class);
                aClass.addField(IntTraceValue.class);
                aClass.addField(IntArrayTraceValue.class);
                aClass.addField(IntegerArrayTraceValue.class);
                aClass.addField(DatabaseInfoTraceValue.class);
                aClass.addField(BindValueTraceValue.class);
                String methodName = "callA";
                aClass.getDeclaredMethod(methodName).addInterceptor(TestBeforeInterceptor.class);
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Class<?> objectTraceValue = loader.loadClass(ObjectTraceValue.class.getName());
    Assert.assertTrue("ObjectTraceValue implements fail", objectTraceValue.isInstance(testObject));
    objectTraceValue.getMethod("_$PINPOINT$_setTraceObject", Object.class).invoke(testObject, "a");
    Object get = objectTraceValue.getMethod("_$PINPOINT$_getTraceObject").invoke(testObject);
    Assert.assertEquals("a", get);
    Class<?> intTraceValue = loader.loadClass(IntTraceValue.class.getName());
    Assert.assertTrue("IntTraceValue implements fail", intTraceValue.isInstance(testObject));
    intTraceValue.getMethod("_$PINPOINT$_setTraceInt", int.class).invoke(testObject, 1);
    int a = (Integer) intTraceValue.getMethod("_$PINPOINT$_getTraceInt").invoke(testObject);
    Assert.assertEquals(1, a);
    Class<?> intArrayTraceValue = loader.loadClass(IntArrayTraceValue.class.getName());
    Assert.assertTrue("IntArrayTraceValue implements fail", intArrayTraceValue.isInstance(testObject));
    int[] expectedInts = { 1, 2, 3 };
    intArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntArray", int[].class).invoke(testObject, expectedInts);
    int[] ints = (int[]) intArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntArray").invoke(testObject);
    Assert.assertEquals(expectedInts, ints);
    Class<?> integerArrayTraceValue = loader.loadClass(IntegerArrayTraceValue.class.getName());
    Assert.assertTrue("IntegerArrayTraceValue implements fail", integerArrayTraceValue.isInstance(testObject));
    Integer[] expectedIntegers = { 1, 2 };
    // wrap due to vararg expansion
    Object[] wrappedExpectedIntegers = new Object[] { expectedIntegers };
    integerArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntegerArray", Integer[].class).invoke(testObject, wrappedExpectedIntegers);
    Integer[] integers = (Integer[]) integerArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntegerArray").invoke(testObject);
    Assert.assertArrayEquals(expectedIntegers, integers);
    Class<?> databaseTraceValue = loader.loadClass(DatabaseInfoTraceValue.class.getName());
    Assert.assertTrue("DatabaseInfoTraceValue implements fail", databaseTraceValue.isInstance(testObject));
    databaseTraceValue.getMethod("_$PINPOINT$_setTraceDatabaseInfo", DatabaseInfo.class).invoke(testObject, UnKnownDatabaseInfo.INSTANCE);
    Object databaseInfo = databaseTraceValue.getMethod("_$PINPOINT$_getTraceDatabaseInfo").invoke(testObject);
    Assert.assertSame(UnKnownDatabaseInfo.INSTANCE, databaseInfo);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestBeforeInterceptor(com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test)

Example 5 with TestClassLoader

use of com.navercorp.pinpoint.test.classloader.TestClassLoader in project pinpoint by naver.

the class AccessorInjectionTest method getTestClassLoader.

private TestClassLoader getTestClassLoader() {
    LoggerContext context = LogManager.getContext();
    PLoggerFactory.initialize(new Log4j2Binder(context));
    DefaultProfilerConfig profilerConfig = new DefaultProfilerConfig();
    MockApplicationContextFactory factory = new MockApplicationContextFactory();
    this.applicationContext = factory.build(profilerConfig);
    this.applicationContext.start();
    TestClassLoader testClassLoader = new TestClassLoader(applicationContext);
    testClassLoader.initialize();
    return testClassLoader;
}
Also used : MockApplicationContextFactory(com.navercorp.pinpoint.test.MockApplicationContextFactory) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) DefaultProfilerConfig(com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig) Log4j2Binder(com.navercorp.pinpoint.profiler.logging.Log4j2Binder) LoggerContext(org.apache.logging.log4j.spi.LoggerContext)

Aggregations

TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)7 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)5 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)5 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)5 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)5 Method (java.lang.reflect.Method)5 ProtectionDomain (java.security.ProtectionDomain)5 Test (org.junit.Test)5 DefaultProfilerConfig (com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig)2 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)2 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)2 DatabaseInfo (com.navercorp.pinpoint.bootstrap.context.DatabaseInfo)1 UnKnownDatabaseInfo (com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo)1 Log4j2Binder (com.navercorp.pinpoint.profiler.logging.Log4j2Binder)1 Slf4jLoggerBinder (com.navercorp.pinpoint.profiler.logging.Slf4jLoggerBinder)1 MockApplicationContext (com.navercorp.pinpoint.test.MockApplicationContext)1 MockApplicationContextFactory (com.navercorp.pinpoint.test.MockApplicationContextFactory)1 TestBeforeInterceptor (com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor)1 LoggerContext (org.apache.logging.log4j.spi.LoggerContext)1 Ignore (org.junit.Ignore)1