Search in sources :

Example 36 with CtClass

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());
}
Also used : CtClass(javassist.CtClass) URLClassLoader(java.net.URLClassLoader) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) Test(org.junit.Test)

Example 37 with CtClass

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());
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 38 with CtClass

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);
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) ASMBytecodeDisassembler(com.navercorp.pinpoint.profiler.instrument.ASMBytecodeDisassembler) LoaderClassPath(javassist.LoaderClassPath) CtMethod(javassist.CtMethod) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with CtClass

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);
}
Also used : CtClass(javassist.CtClass)

Example 40 with CtClass

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);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method)

Aggregations

CtClass (javassist.CtClass)121 CtMethod (javassist.CtMethod)46 ClassPool (javassist.ClassPool)37 NotFoundException (javassist.NotFoundException)35 Test (org.junit.Test)32 CannotCompileException (javassist.CannotCompileException)23 CtField (javassist.CtField)20 IOException (java.io.IOException)11 CtConstructor (javassist.CtConstructor)8 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 CtMethodJavaWriter (com.github.stephanenicolas.afterburner.inserts.CtMethodJavaWriter)4 FileNotFoundException (java.io.FileNotFoundException)4 LoaderClassPath (javassist.LoaderClassPath)4 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)4 CodeAttribute (javassist.bytecode.CodeAttribute)4 ConstPool (javassist.bytecode.ConstPool)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 NamedClassPool (com.navercorp.pinpoint.profiler.instrument.classpool.NamedClassPool)3