use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class JavassistClassTest method getNestedClasses.
@Test
public void getNestedClasses() throws Exception {
InstrumentEngine engine = newJavassistEngine();
InstrumentContext instrumentContext = mock(InstrumentContext.class);
String testObjectName = "com.navercorp.pinpoint.test.javasssit.mock.TestObjectNestedClass";
byte[] testObjectByteCode = readByteCode(testObjectName);
InstrumentClass testObject = engine.getClass(instrumentContext, null, testObjectName, testObjectByteCode);
Assert.assertEquals(testObject.getName(), testObjectName);
// find class name condition.
final String targetClassName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObjectNestedClass$InstanceInner";
for (InstrumentClass c : testObject.getNestedClasses(ClassFilters.name(targetClassName))) {
assertEquals(targetClassName, c.getName());
}
// find enclosing method condition.
assertEquals(2, testObject.getNestedClasses(ClassFilters.enclosingMethod("annonymousInnerClass")).size());
// find interface condition.
assertEquals(2, testObject.getNestedClasses(ClassFilters.interfaze("java.util.concurrent.Callable")).size());
// find enclosing method & interface condition.
assertEquals(1, testObject.getNestedClasses(ClassFilters.chain(ClassFilters.enclosingMethod("annonymousInnerClass"), ClassFilters.interfaze("java.util.concurrent.Callable"))).size());
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class JavassistClassTest method testClassHierarchy.
@Test
public void testClassHierarchy() throws InstrumentException {
InstrumentEngine engine = newJavassistEngine();
InstrumentContext instrumentContext = mock(InstrumentContext.class);
String testObjectName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
byte[] testObjectByteCode = readByteCode(testObjectName);
InstrumentClass testObject = engine.getClass(instrumentContext, null, testObjectName, testObjectByteCode);
Assert.assertEquals(testObject.getName(), testObjectName);
String testObjectSuperClass = testObject.getSuperClass();
Assert.assertEquals("java.lang.Object", testObjectSuperClass);
String[] testObjectSuperClassInterfaces = testObject.getInterfaces();
Assert.assertEquals(testObjectSuperClassInterfaces.length, 0);
final String classHierarchyTestMockName = "com.navercorp.pinpoint.test.javasssit.mock.ClassHierarchyTestMock";
byte[] classHierarchyTestMockByteCode = readByteCode(classHierarchyTestMockName);
InstrumentClass classHierarchyObject = engine.getClass(instrumentContext, null, classHierarchyTestMockName, classHierarchyTestMockByteCode);
String hierarchySuperClass = classHierarchyObject.getSuperClass();
Assert.assertEquals("java.util.HashMap", hierarchySuperClass);
String[] hierarchyInterfaces = classHierarchyObject.getInterfaces();
Assert.assertEquals(hierarchyInterfaces.length, 2);
Assert.assertEquals(hierarchyInterfaces[0], "java.lang.Runnable");
Assert.assertEquals(hierarchyInterfaces[1], "java.lang.Comparable");
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class JavassistClassTest method testDeclaredMethod.
@Test
public void testDeclaredMethod() throws InstrumentException {
InstrumentEngine engine = newJavassistEngine();
InstrumentContext instrumentContext = mock(InstrumentContext.class);
String testObjectName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject";
byte[] testObjectByteCode = readByteCode(testObjectName);
InstrumentClass testObject = engine.getClass(instrumentContext, null, testObjectName, testObjectByteCode);
Assert.assertEquals(testObject.getName(), testObjectName);
InstrumentMethod declaredMethod = testObject.getDeclaredMethod("callA");
Assert.assertNotNull(declaredMethod);
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class AccessorInjectionTest method testAddGetter.
@Test
public void testAddGetter() throws Exception {
final TestClassLoader loader = getTestClassLoader();
final String targetClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject3";
loader.addTransformer(targetClassName, 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, className, classfileBuffer);
aClass.addGetter(StringGetter.class.getName(), "value");
aClass.addGetter(IntGetter.class.getName(), "intValue");
aClass.addGetter(IntArrayGetter.class.getName(), "intValues");
aClass.addGetter(IntegerArrayGetter.class.getName(), "integerValues");
return aClass.toBytecode();
} catch (InstrumentException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
});
Object testObject = loader.loadClass(targetClassName).newInstance();
Class<?> stringGetter = loader.loadClass(StringGetter.class.getName());
Class<?> intGetter = loader.loadClass(IntGetter.class.getName());
Class<?> intsGetter = loader.loadClass(IntArrayGetter.class.getName());
Class<?> integersGetter = loader.loadClass(IntegerArrayGetter.class.getName());
Assert.assertTrue(stringGetter.isInstance(testObject));
Assert.assertTrue(intGetter.isInstance(testObject));
Assert.assertTrue(intsGetter.isInstance(testObject));
Assert.assertTrue(integersGetter.isInstance(testObject));
String value = "hehe";
int intValue = 99;
int[] intValues = { 99, 100 };
Integer[] integerValues = { 99, 100 };
Method method = testObject.getClass().getMethod("setValue", String.class);
method.invoke(testObject, value);
Method getString = stringGetter.getMethod("_$PINPOINT$_getString");
Assert.assertEquals(value, getString.invoke(testObject));
Method setIntValue = testObject.getClass().getMethod("setIntValue", int.class);
setIntValue.invoke(testObject, intValue);
Method getInt = intGetter.getMethod("_$PINPOINT$_getInt");
Assert.assertEquals(intValue, getInt.invoke(testObject));
Method setIntValues = testObject.getClass().getMethod("setIntValues", int[].class);
setIntValues.invoke(testObject, intValues);
Method getIntValues = intsGetter.getMethod("_$PINPOINT$_getIntArray");
Assert.assertEquals(intValues, getIntValues.invoke(testObject));
Method setIntegerValues = testObject.getClass().getMethod("setIntegerValues", Integer[].class);
// wrap due to vararg expansion
Object[] wrappedIntegerValues = new Object[] { integerValues };
setIntegerValues.invoke(testObject, wrappedIntegerValues);
Method getIntegerValues = integersGetter.getMethod("_$PINPOINT$_getIntegerArray");
Assert.assertEquals(integerValues, getIntegerValues.invoke(testObject));
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass in project pinpoint by naver.
the class JavassistEngineTest method testGetClass_transform.
@Test
public void testGetClass_transform() throws Exception {
InstrumentEngine instrumentEngine = newJavassistEngine();
InstrumentContext instrumentContext = mock(InstrumentContext.class);
final byte[] transformByteCode = getTransformByteCode();
final InstrumentClass transformClass = instrumentEngine.getClass(instrumentContext, null, mock, transformByteCode);
Assert.assertNotNull(transformClass.getDeclaredMethod("test"));
Assert.assertNotNull("transform method", transformClass.getDeclaredMethod("transformMethod"));
}
Aggregations