use of com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter in project pinpoint by naver.
the class MethodInterfaceTest method addInterceptor.
@Test
public void addInterceptor() throws Exception {
final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
final String targetClassName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterfaceClass";
logger.debug("Add interceptor interface={}, class={}", targetInterfaceName, targetClassName);
final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new SimpleInterceptor());
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(SimpleInterceptor.class);
final List<String> methodNameList = Arrays.asList("currentTimeMillis", "foo");
TestClassLoader classLoader = new TestClassLoader();
classLoader.addTargetClassName(targetClassName);
classLoader.addTargetClassName(targetInterfaceName);
classLoader.setCallbackHandler(new CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
logger.debug("Handle class={}, method={}", classNode.name, methodNode.name);
if (methodNode.name.equals("<clinit>")) {
continue;
}
ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
continue;
}
if (!methodNameList.contains(methodNode.name)) {
continue;
}
methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
logger.debug("Add before interceptor in method={}", methodNode.name);
methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
logger.debug("Add after interceptor in method={}", methodNode.name);
}
}
});
// static method
Assert.assertFalse(SimpleInterceptor.before);
logger.debug("Interface static method");
Class<?> clazz = classLoader.loadClass(targetInterfaceName);
Method method = clazz.getDeclaredMethod("currentTimeMillis");
method.invoke(null);
assertTrue(SimpleInterceptor.before);
// reset
SimpleInterceptor.before = false;
// default method
Assert.assertFalse(SimpleInterceptor.before);
logger.debug("Interface default method");
clazz = classLoader.loadClass(targetClassName);
method = clazz.getDeclaredMethod("bar");
method.invoke(clazz.newInstance());
assertTrue(SimpleInterceptor.before);
}
use of com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter in project pinpoint by naver.
the class MethodInterfaceTest method addMethod.
@Test
public void addMethod() throws Exception {
final MethodNode methodNode = TestClassLoader.get("com.navercorp.test.pinpoint.jdk8.interfaces.SimpleClass", "welcome");
final ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter("com/navercorp/test/pinpoint/jdk8/interfaces/SimpleClass", methodNode);
final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
TestClassLoader classLoader = new TestClassLoader();
classLoader.addTargetClassName(targetInterfaceName);
classLoader.setTrace(false);
classLoader.setCallbackHandler(new CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
logger.debug("Add method class={}", classNode.name);
ASMClassNodeAdapter classNodeAdapter = new ASMClassNodeAdapter(pluginContext, null, null, classNode);
classNodeAdapter.copyMethod(methodNodeAdapter);
}
});
logger.debug("Interface static method");
Class<?> clazz = classLoader.loadClass(targetInterfaceName);
Method method = clazz.getDeclaredMethod("welcome");
method.invoke(null);
}
Aggregations