use of com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory in project pinpoint by naver.
the class InstrumentEngineProvider method get.
public InstrumentEngine get() {
final String instrumentEngine = instrumentConfig.getProfileInstrumentEngine().toUpperCase();
if (InstrumentConfig.INSTRUMENT_ENGINE_ASM.equals(instrumentEngine)) {
logger.info("ASM InstrumentEngine");
// WARNING must be singleton
final InterceptorDefinitionFactory interceptorDefinitionFactory = new InterceptorDefinitionFactory();
// WARNING must be singleton
final ScopeFactory scopeFactory = new ScopeFactory();
EngineComponent engineComponent = new DefaultEngineComponent(objectBinderFactory, interceptorRegistryBinder, interceptorDefinitionFactory, apiMetaDataServiceProvider, scopeFactory);
return new ASMEngine(instrumentation, engineComponent);
} else {
logger.warn("Unknown InstrumentEngine:{}", instrumentEngine);
throw new IllegalArgumentException("Unknown InstrumentEngine:" + instrumentEngine);
}
}
use of com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory 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.interceptor.InterceptorDefinitionFactory in project pinpoint by naver.
the class ASMMethodNodeAdapterAddInterceptorTest method addInterceptor.
private Class<?> addInterceptor(final int interceptorId, final String targetClassName, final Class<?> interceptorClass) {
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(interceptorClass);
try {
classLoader.setTrace(false);
classLoader.setVerify(false);
classLoader.setTargetClassName(targetClassName);
classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
if (methodNode.name.equals("<clinit>")) {
continue;
}
ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
continue;
}
methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
}
}
});
return classLoader.loadClass(targetClassName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory in project pinpoint by naver.
the class ASMMethodVariablesTest method hasInterceptor.
@Test
public void hasInterceptor() throws Exception {
InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(ArgsArrayInterceptor.class);
final ClassNode classNode = ASMClassNodeLoader.get("com.navercorp.pinpoint.profiler.instrument.mock.ArgsClass");
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
assertEquals(false, methodNodeAdapter.hasInterceptor());
methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, -1);
assertEquals(true, methodNodeAdapter.hasInterceptor());
}
}
use of com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory in project pinpoint by naver.
the class ASMMethodVariablesTest method initInterceptorLocalVariables.
@Test
public void initInterceptorLocalVariables() throws Exception {
MethodNode methodNode = ASMClassNodeLoader.get("com.navercorp.pinpoint.profiler.instrument.mock.ConstructorChildClass", "<init>");
ASMMethodVariables variables = new ASMMethodVariables("com/navercorp/pinpoint/profiler/instrument/mock/ConstructorChildClass", methodNode);
assertNull(variables.getEnterInsnNode());
assertNull(variables.getEnterInsnNode());
InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(ArgsArrayInterceptor.class);
InsnList instructions = new InsnList();
boolean first = variables.initInterceptorLocalVariables(instructions, interceptorId, interceptorDefinition, -1);
assertEquals(true, first);
assertNotNull(variables.getEnterInsnNode());
assertNotNull(variables.getEnterInsnNode());
}
Aggregations