use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException 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.InstrumentException in project pinpoint by naver.
the class ASMClass method addGetter.
@Override
public void addGetter(final String getterTypeName, final String fieldName) throws InstrumentException {
try {
final Class<?> getterType = this.pluginContext.injectClass(this.classLoader, getterTypeName);
final GetterAnalyzer.GetterDetails getterDetails = new GetterAnalyzer().analyze(getterType);
final ASMFieldNodeAdapter fieldNode = this.classNode.getField(fieldName, null);
if (fieldNode == null) {
throw new IllegalArgumentException("Not found field. name=" + fieldName);
}
final String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(getterDetails.getFieldType().getName());
if (!fieldNode.getClassName().equals(fieldTypeName)) {
throw new IllegalArgumentException("different return type. return=" + fieldTypeName + ", field=" + fieldNode.getClassName());
}
this.classNode.addGetterMethod(getterDetails.getGetter().getName(), fieldNode);
this.classNode.addInterface(getterTypeName);
setModified(true);
} catch (Exception e) {
throw new InstrumentException("Failed to add getter: " + getterTypeName, e);
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class ASMClass method addSetter.
@Override
public void addSetter(String setterTypeName, String fieldName, boolean removeFinal) throws InstrumentException {
try {
final Class<?> setterType = this.pluginContext.injectClass(this.classLoader, setterTypeName);
final SetterAnalyzer.SetterDetails setterDetails = new SetterAnalyzer().analyze(setterType);
final ASMFieldNodeAdapter fieldNode = this.classNode.getField(fieldName, null);
if (fieldNode == null) {
throw new IllegalArgumentException("Not found field. name=" + fieldName);
}
final String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(setterDetails.getFieldType().getName());
if (!fieldNode.getClassName().equals(fieldTypeName)) {
throw new IllegalArgumentException("Argument type of the setter is different with the field type. setterMethod: " + fieldTypeName + ", fieldType: " + fieldNode.getClassName());
}
if (fieldNode.isStatic()) {
throw new IllegalArgumentException("Cannot add setter to static fields. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
}
final int original = fieldNode.getAccess();
boolean finalRemoved = false;
if (fieldNode.isFinal()) {
if (!removeFinal) {
throw new IllegalArgumentException("Cannot add setter to final field. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
} else {
final int removed = original & ~Opcodes.ACC_FINAL;
fieldNode.setAccess(removed);
finalRemoved = true;
}
}
try {
this.classNode.addSetterMethod(setterDetails.getSetter().getName(), fieldNode);
this.classNode.addInterface(setterTypeName);
setModified(true);
} catch (Exception e) {
if (finalRemoved) {
fieldNode.setAccess(original);
}
throw e;
}
} catch (Exception e) {
throw new InstrumentException("Failed to add setter: " + setterTypeName, e);
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException 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.InstrumentException 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