@Test public void testBeforeAddInterceptor() throws Exception { final TestClassLoader loader = getTestClassLoader(); final String javassistClassName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"; loader.addTransformer( javassistClassName, new TransformCallback() { @Override public byte[] doInTransform( Instrumentor instrumentContext, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException { try { logger.info("modify className:{} cl:{}", className, classLoader); InstrumentClass aClass = instrumentContext.getInstrumentClass( classLoader, javassistClassName, classfileBuffer); String methodName = "callA"; aClass .getDeclaredMethod(methodName) .addInterceptor( "com.navercorp.pinpoint.profiler.interceptor.TestBeforeInterceptor"); return aClass.toBytecode(); } catch (InstrumentException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } } }); loader.initialize(); 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); Interceptor interceptor = getInterceptor(loader, 0); assertEqualsIntField(interceptor, "call", 1); assertEqualsObjectField( interceptor, "className", "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"); assertEqualsObjectField(interceptor, "methodName", methodName); assertEqualsObjectField(interceptor, "args", null); assertEqualsObjectField(interceptor, "target", testObject); }
@Test public void addTraceValue() throws Exception { final TestClassLoader loader = getTestClassLoader(); final String javassistClassName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"; loader.addTransformer( javassistClassName, new TransformCallback() { @Override public byte[] doInTransform( Instrumentor instrumentContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException { try { logger.info("modify cl:{}", loader); InstrumentClass aClass = instrumentContext.getInstrumentClass(loader, javassistClassName, classfileBuffer); aClass.addField(ObjectTraceValue.class.getName()); aClass.addField(IntTraceValue.class.getName()); aClass.addField(DatabaseInfoTraceValue.class.getName()); aClass.addField(BindValueTraceValue.class.getName()); String methodName = "callA"; aClass .getDeclaredMethod(methodName) .addInterceptor( "com.navercorp.pinpoint.profiler.interceptor.TestBeforeInterceptor"); return aClass.toBytecode(); } catch (InstrumentException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } } }); loader.initialize(); 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<?> 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); }