use of javassist.CtClass in project pinpoint by naver.
the class TestBootstrapClass method test.
@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, CannotCompileException {
URLClassLoader classLoader = new URLClassLoader(new URL[] {});
LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader);
ClassPool cp = new ClassPool();
cp.appendClassPath(loaderClassPath);
CtClass ctClass = cp.makeClass(TEST_CLASS_NAME);
byte[] bytes = ctClass.toBytecode();
logger.debug(classLoader.getClass().getName());
Class<?> aClass = BytecodeUtils.defineClass(classLoader, TEST_CLASS_NAME, bytes);
logger.debug("{}", aClass.getName());
}
use of javassist.CtClass in project pinpoint by naver.
the class JavaAssistTest method genericTest.
@Test
public void genericTest() throws NotFoundException {
CtClass testClass = pool.get("com.navercorp.pinpoint.test.javasssit.TestClass");
// CtMethod setb = testClass.getMethod("setb");
CtMethod[] declaredMethods = testClass.getDeclaredMethods();
for (CtMethod declaredMethod : declaredMethods) {
logger.debug(declaredMethod.toString());
logger.debug(declaredMethod.getGenericSignature());
logger.debug(declaredMethod.getSignature());
logger.debug("paramTypes:{}", Arrays.toString(declaredMethod.getParameterTypes()));
logger.debug(declaredMethod.getMethodInfo2().getDescriptor());
logger.debug(declaredMethod.getMethodInfo().getDescriptor());
// logger.debug(declaredMethod.());
}
CtMethod setb = testClass.getDeclaredMethod("setA", new CtClass[] { pool.get("int") });
logger.debug(setb.toString());
CtMethod setStringArray = testClass.getDeclaredMethod("setStringArray", new CtClass[] { pool.get("java.lang.String[]") });
logger.debug(setStringArray.toString());
}
use of javassist.CtClass in project pinpoint by naver.
the class JavassistVerifyErrorTest method bug_regression_BytecodeVerifyError_Invalid_StackMapFrame.
/**
* bug id
* https://github.com/naver/pinpoint/issues/1807
* @throws Exception
*/
@Ignore("fixed Javassist 3.21.0-GA")
@Test
public void bug_regression_BytecodeVerifyError_Invalid_StackMapFrame() throws Exception {
CustomURLClassLoader classLoader = new CustomURLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader());
ClassPool classPool = new ClassPool(true);
classPool.appendClassPath(new LoaderClassPath(classLoader));
final CtClass ctClass = classPool.get(INVALID_STACK_MAP_FRAME);
final CtMethod method = ctClass.getDeclaredMethod("bytecodeVerifyError");
method.addLocalVariable("test_localVariable", CtClass.intType);
method.insertBefore("{ test_localVariable = 1; }");
final byte[] bytecode = ctClass.toBytecode();
classLoader.defineClass0(INVALID_STACK_MAP_FRAME, bytecode);
try {
Class.forName(INVALID_STACK_MAP_FRAME, true, classLoader);
Assert.fail("VerifyError");
} catch (VerifyError e) {
logger.debug("verifyError:{}", e.getMessage(), e);
}
final ASMBytecodeDisassembler bytecodeDisassembler = new ASMBytecodeDisassembler();
final String dumpBytecode = bytecodeDisassembler.dumpBytecode(bytecode);
logger.debug("dumpBytecode:{}", dumpBytecode);
// javassist bug : invalid stack map frame
// 00013 InvalidStackMapFrame ArrayList String Iterator I : : FRAME FULL [bug_regression_jdk7/javassist/InvalidStackMapFrame java/util/ArrayList [[[java/lang/Object->[Ljava/lang/String;]]] java/util/Iterator T T T I] []
final String verify = bytecodeDisassembler.dumpVerify(bytecode, classLoader);
logger.debug("dumpVerify:{}", verify);
final String dumpAsm = bytecodeDisassembler.dumpASM(bytecode);
logger.debug("dumpAsm :{}", dumpAsm);
}
use of javassist.CtClass in project pinpoint by naver.
the class JavassistMethod method addLocalVariable.
private void addLocalVariable(String name, Class<?> type) throws CannotCompileException, NotFoundException {
final String interceptorClassName = type.getName();
final CtClass interceptorCtClass = behavior.getDeclaringClass().getClassPool().get(interceptorClassName);
behavior.addLocalVariable(name, interceptorCtClass);
}
use of javassist.CtClass in project pinpoint by naver.
the class JavassistMethod method addAfterInterceptor.
private void addAfterInterceptor(InterceptorDefinition interceptorDefinition, int interceptorId, boolean localVarsInitialized, int originalCodeOffset) throws NotFoundException, CannotCompileException {
final Class<?> interceptorClass = interceptorDefinition.getInterceptorClass();
final CaptureType captureType = interceptorDefinition.getCaptureType();
if (!isAfterInterceptor(captureType)) {
return;
}
final Method interceptorMethod = interceptorDefinition.getAfterMethod();
if (interceptorMethod == null) {
if (isDebug) {
logger.debug("Skip adding after interceptor because the interceptor doesn't have after method: {}", interceptorClass.getName());
}
return;
}
InvokeAfterCodeGenerator catchGenerator = new InvokeAfterCodeGenerator(interceptorId, interceptorDefinition, declaringClass, this, apiMetaDataService, localVarsInitialized, true);
String catchCode = catchGenerator.generate();
if (isDebug) {
logger.debug("addAfterInterceptor catch behavior:{} code:{}", behavior.getLongName(), catchCode);
}
CtClass throwable = behavior.getDeclaringClass().getClassPool().get("java.lang.Throwable");
insertCatch(originalCodeOffset, catchCode, throwable, "$e");
InvokeAfterCodeGenerator afterGenerator = new InvokeAfterCodeGenerator(interceptorId, interceptorDefinition, declaringClass, this, apiMetaDataService, localVarsInitialized, false);
final String afterCode = afterGenerator.generate();
if (isDebug) {
logger.debug("addAfterInterceptor after behavior:{} code:{}", behavior.getLongName(), afterCode);
}
behavior.insertAfter(afterCode);
}
Aggregations