use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass 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.getName());
aClass.addField(IntTraceValue.class.getName());
aClass.addField(IntArrayTraceValue.class.getName());
aClass.addField(IntegerArrayTraceValue.class.getName());
aClass.addField(DatabaseInfoTraceValue.class.getName());
aClass.addField(BindValueTraceValue.class.getName());
String methodName = "callA";
aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
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.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class DefaultClassEditorBuilderTest method test.
@Test
public void test() throws Exception {
InstrumentEngine instrumentEngine = mock(InstrumentEngine.class);
TraceContext traceContext = mock(TraceContext.class);
InstrumentClass aClass = mock(InstrumentClass.class);
InstrumentMethod aMethod = mock(InstrumentMethod.class);
MethodDescriptor aDescriptor = mock(MethodDescriptor.class);
ApplicationContext applicationContext = mock(ApplicationContext.class);
InstrumentContext context = mock(InstrumentContext.class);
ClassLoader classLoader = getClass().getClassLoader();
String className = "someClass";
String methodName = "someMethod";
byte[] classFileBuffer = new byte[0];
Class<?>[] parameterTypes = new Class<?>[] { String.class };
String[] parameterTypeNames = TypeUtils.toClassNames(parameterTypes);
when(applicationContext.getInstrumentEngine()).thenReturn(instrumentEngine);
when(applicationContext.getTraceContext()).thenReturn(traceContext);
when(instrumentEngine.getClass(context, classLoader, className, classFileBuffer)).thenReturn(aClass);
when(aClass.getDeclaredMethod(methodName, parameterTypeNames)).thenReturn(aMethod);
when(aMethod.getName()).thenReturn(methodName);
when(aMethod.getParameterTypes()).thenReturn(parameterTypeNames);
when(aMethod.getDescriptor()).thenReturn(aDescriptor);
when(aClass.addInterceptor(eq(methodName), va(eq(parameterTypeNames)))).thenReturn(0);
// DefaultClassFileTransformerBuilder builder = new DefaultClassFileTransformerBuilder(context, "TargetClass");
// builder.injectField("some.accessor.Type", "java.util.HashMap");
// builder.injectGetter("some.getter.Type", "someField");
//
// MethodTransformerBuilder ib = builder.editMethod(methodName, parameterTypeNames);
// ib.injectInterceptor("com.navercorp.pinpoint.profiler.plugin.TestInterceptor", "provided");
//
// ClassFileTransformer transformer = builder.build();
//
// transformer.transform(classLoader, className, null, null, classFileBuffer);
//
// verify(aMethod).addScopedInterceptor(eq("com.navercorp.pinpoint.profiler.plugin.TestInterceptor"), eq(va("provided")), (InterceptorScope)isNull(), (ExecutionPolicy)isNull());
// verify(aClass).addField("some.accessor.Type", "new java.util.HashMap();");
// verify(aClass).addGetter("some.getter.Type", "someField");
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class TransformUtils method addInterceptor.
public static TransformCallback addInterceptor(final String interceptorClassName, final Object[] constructorArgs) {
return new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
target.addInterceptor(interceptorClassName, constructorArgs);
return target.toBytecode();
}
};
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class SpringBeansPlugin method addAbstractAutowireCapableBeanFactoryTransformer.
private void addAbstractAutowireCapableBeanFactoryTransformer(final ProfilerPluginSetupContext context) {
final ProfilerConfig config = context.getConfig();
final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
transformTemplate.transform("org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", config);
final InstrumentMethod createBeanInstance = target.getDeclaredMethod("createBeanInstance", "java.lang.String", "org.springframework.beans.factory.support.RootBeanDefinition", "java.lang.Object[]");
createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.CreateBeanInstanceInterceptor", va(beanTransformer, beanFilterFactory));
final InstrumentMethod postProcessor = target.getDeclaredMethod("applyBeanPostProcessorsBeforeInstantiation", "java.lang.Class", "java.lang.String");
postProcessor.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.PostProcessorInterceptor", va(beanTransformer, beanFilterFactory));
return target.toBytecode();
}
});
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class SpringBeansPlugin method addClassPathDefinitionScannerTransformer.
private void addClassPathDefinitionScannerTransformer(final ProfilerPluginSetupContext context) {
final ProfilerConfig config = context.getConfig();
final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
transformTemplate.transform("org.springframework.context.annotation.ClassPathBeanDefinitionScanner", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", context.getConfig());
final InstrumentMethod method = target.getDeclaredMethod("doScan", "java.lang.String[]");
method.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.ClassPathDefinitionScannerDoScanInterceptor", va(loader, beanTransformer, beanFilterFactory));
return target.toBytecode();
}
});
}
Aggregations