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);
}
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);
}
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;
}
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);
}
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;
}
Aggregations