use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class InvokeAfterCodeGeneratorTest method testGenerate_AroundInterceptor0.
@Test
public void testGenerate_AroundInterceptor0() throws Exception {
final Class<AroundInterceptor0> aroundInterceptor3Class = AroundInterceptor0.class;
final InterceptorDefinition interceptorDefinition = interceptorDefinitionFactory.createInterceptorDefinition(aroundInterceptor3Class);
final InstrumentClass mockClass = mock(InstrumentClass.class);
Mockito.when(mockClass.getName()).thenReturn("TestClass");
final InstrumentMethod mockMethod = mock(InstrumentMethod.class);
Mockito.when(mockMethod.getName()).thenReturn("TestMethod");
Mockito.when(mockMethod.getParameterTypes()).thenReturn(new String[] {});
Mockito.when(mockMethod.getReturnType()).thenReturn("java.lang.Object");
ApiMetaDataService apiMetaDataService = mock(ApiMetaDataService.class);
final InvokeAfterCodeGenerator invokeAfterCodeGenerator = new InvokeAfterCodeGenerator(100, interceptorDefinition, mockClass, mockMethod, apiMetaDataService, false, true);
final String generate = invokeAfterCodeGenerator.generate();
logger.debug("testGenerate_AroundInterceptor0:{}", generate);
Assert.assertFalse(generate.contains("($w)$1"));
Assert.assertFalse(generate.contains("($w)$2"));
Assert.assertFalse(generate.contains("($w)$3"));
Assert.assertTrue(generate.contains("$e"));
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class ASMMethodNodeAdapterTestMain method addInterceptor.
private void addInterceptor(final String className) throws Exception {
final boolean trace = false;
final boolean verify = false;
final String classInternalName = JavaAssistUtils.jvmNameToJavaName(className);
ClassLoader classLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (!name.startsWith("java") && !name.startsWith("sun") && super.findLoadedClass(name) == null) {
try {
ClassNode classNode = ASMClassNodeLoader.get(JavaAssistUtils.javaNameToJvmName(name));
ObjectBinderFactory objectBinderFactory = mock(ObjectBinderFactory.class);
ApiMetaDataService apiMetaDataService = mock(ApiMetaDataService.class);
ASMClass asmClass = new ASMClass(objectBinderFactory, null, interceptorRegistryBinder, apiMetaDataService, null, classNode);
if (asmClass.isInterceptable()) {
for (InstrumentMethod method : asmClass.getDeclaredMethods()) {
try {
method.addInterceptor(interceptorId);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
byte[] bytes = asmClass.toBytecode();
if (trace) {
ClassReader classReader = new ClassReader(bytes);
ClassWriter cw = new ClassWriter(0);
TraceClassVisitor tcv = new TraceClassVisitor(cw, new PrintWriter(System.out));
classReader.accept(tcv, 0);
}
if (verify) {
CheckClassAdapter.verify(new ClassReader(bytes), false, new PrintWriter(System.out));
}
return super.defineClass(name, bytes, 0, bytes.length);
} catch (Throwable ex) {
ex.printStackTrace();
return null;
}
} else {
try {
return super.loadClass(name);
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
}
};
try {
classLoader.loadClass(classInternalName);
} catch (ClassNotFoundException cnfe) {
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod in project pinpoint by naver.
the class ConstructorTransformer method edit.
@Override
public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
InstrumentMethod targetConstructor = target.getConstructor(targetParameterTypes);
if (targetConstructor == null) {
if (ignoreIfNotExist) {
return;
} else {
Exception e = new NoSuchMethodException("No such constructor: " + "(" + Arrays.deepToString(targetParameterTypes) + ")");
if (exceptionHandler != null) {
exceptionHandler.handle(target.getName(), "init", targetParameterTypes, e);
logger.info("Cannot find target constructor with parameter types (" + Arrays.deepToString(targetParameterTypes) + ") but MethodTransformerExceptionHandler handled it.");
return;
} else {
throw new InstrumentException("Fail to edit constructor", e);
}
}
}
for (MethodRecipe recipe : recipes) {
try {
recipe.edit(classLoader, target, targetConstructor);
} catch (Throwable t) {
logger.info("Exception thrown while editing " + targetConstructor.getDescriptor().getApiDescriptor(), t);
if (exceptionHandler != null) {
exceptionHandler.handle(target.getName(), "init", targetParameterTypes, t);
logger.info("Exception thrown while editing" + targetConstructor.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t);
} else {
throw new InstrumentException("Fail to edit constructor " + targetConstructor.getDescriptor().getApiDescriptor(), t);
}
}
}
}
Aggregations