Search in sources :

Example 51 with InstrumentClass

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());
}
Also used : InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentEngine(com.navercorp.pinpoint.profiler.instrument.InstrumentEngine) Test(org.junit.Test)

Example 52 with InstrumentClass

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");
}
Also used : InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentEngine(com.navercorp.pinpoint.profiler.instrument.InstrumentEngine) Test(org.junit.Test)

Example 53 with InstrumentClass

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);
}
Also used : InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) InstrumentEngine(com.navercorp.pinpoint.profiler.instrument.InstrumentEngine) Test(org.junit.Test)

Example 54 with InstrumentClass

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));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 55 with InstrumentClass

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"));
}
Also used : InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) Test(org.junit.Test)

Aggregations

InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)60 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)44 ProtectionDomain (java.security.ProtectionDomain)44 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)42 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)30 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)29 Test (org.junit.Test)18 InstrumentContext (com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext)8 InstrumentEngine (com.navercorp.pinpoint.profiler.instrument.InstrumentEngine)6 ApiMetaDataService (com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService)5 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 Method (java.lang.reflect.Method)5 AroundInterceptor3 (com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor3)4 JavassistClassTest (com.navercorp.pinpoint.test.javasssit.JavassistClassTest)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)2 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)2 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)2 PreparedStatementBindingMethodFilter (com.navercorp.pinpoint.bootstrap.plugin.jdbc.PreparedStatementBindingMethodFilter)2 List (java.util.List)2