@Test public void testClassHierarchy() throws InstrumentException { JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null); String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"; // final CallLoader loader = null; // systemClassLoader // final ClassLoader loader = ClassLoader.getSystemClassLoader(); InstrumentClass testObject = pool.getClass(null, testObjectName, null); Assert.assertEquals(testObject.getName(), testObjectName); String testObjectSuperClass = testObject.getSuperClass(); Assert.assertEquals("java.lang.Object", testObjectSuperClass); String[] testObjectSuperClassInterfaces = testObject.getInterfaces(); Assert.assertEquals(testObjectSuperClassInterfaces.length, 0); InstrumentClass classHierarchyObject = pool.getClass( null, "com.navercorp.pinpoint.profiler.interceptor.bci.ClassHierarchyTestMock", null); 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"); }
@Test public void testDeclaredMethod() throws InstrumentException { JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null); String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"; InstrumentClass testObject = pool.getClass(null, testObjectName, null); Assert.assertEquals(testObject.getName(), testObjectName); InstrumentMethod declaredMethod = testObject.getDeclaredMethod("callA", null); Assert.assertNotNull(declaredMethod); }
@Test public void getNestedClasses() throws Exception { JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null); String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObjectNestedClass"; InstrumentClass testObject = pool.getClass(null, testObjectName, null); 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()); }
@Test public void hasEnclodingMethod() throws Exception { JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null); String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObjectNestedClass"; InstrumentClass testObject = pool.getClass(null, testObjectName, null); Assert.assertEquals(testObject.getName(), testObjectName); assertEquals( 1, testObject .getNestedClasses( ClassFilters.enclosingMethod("enclosingMethod", "java.lang.String", "int")) .size()); assertEquals( 0, testObject.getNestedClasses(ClassFilters.enclosingMethod("enclosingMethod", "int")).size()); }
@Test public void testDeclaredMethods() throws InstrumentException { JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null); String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject"; InstrumentClass testObject = pool.getClass(null, testObjectName, null); Assert.assertEquals(testObject.getName(), testObjectName); int findMethodCount = 0; for (InstrumentMethod methodInfo : testObject.getDeclaredMethods()) { if (!methodInfo.getName().equals("callA")) { continue; } String[] parameterTypes = methodInfo.getParameterTypes(); if (parameterTypes == null || parameterTypes.length == 0) { findMethodCount++; } } Assert.assertEquals(findMethodCount, 1); }
@Override public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable { for (InstrumentMethod targetMethod : target.getDeclaredMethods(filter)) { for (MethodRecipe recipe : recipes) { try { recipe.edit(classLoader, target, targetMethod); } catch (Throwable t) { if (exceptionHandler != null) { exceptionHandler.handle( target.getName(), targetMethod.getName(), targetMethod.getParameterTypes(), t); logger.info( "Exception thrown while editing" + targetMethod.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t); } else { throw new InstrumentException( "Fail to edit method " + targetMethod.getDescriptor().getApiDescriptor(), t); } } } } }