Search in sources :

Example 11 with ClassPool

use of javassist.ClassPool 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 12 with ClassPool

use of javassist.ClassPool in project powermock by powermock.

the class ClassMockTransformerTest method shouldIgnoreCallToSyntheticField.

@Test
public void shouldIgnoreCallToSyntheticField() throws Throwable {
    final ClassPool classPool = new ClassPool(true);
    CtClass ctClass = prepareClassesForFieldTests(classPool);
    new ClassMockTransformer().transform(ctClass);
    runTestWithNewClassLoader(classPool, ShouldIgnoreCallToSyntheticField.class.getName());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) Test(org.junit.Test)

Example 13 with ClassPool

use of javassist.ClassPool in project powermock by powermock.

the class ClassMockTransformerTest method shouldModifyBridgeMethods.

@Test
public void shouldModifyBridgeMethods() throws Throwable {
    final ClassPool classPool = new ClassPool(true);
    addCallInterceptorToMockGateway(classPool);
    CtClass ctClass = classPool.get(SubclassWithBridgeMethod.class.getName());
    new ClassMockTransformer().transform(ctClass);
    runTestWithNewClassLoader(classPool, ShouldModifyBridgeMethods.class.getName());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) SubclassWithBridgeMethod(powermock.test.support.MainMockTransformerTestSupport.SubclassWithBridgeMethod) Test(org.junit.Test)

Example 14 with ClassPool

use of javassist.ClassPool in project powermock by powermock.

the class ClassMockTransformerTest method shouldIgnoreCallToSyntheticNonBridgeMethods.

@Test
public void shouldIgnoreCallToSyntheticNonBridgeMethods() throws Throwable {
    final ClassPool classPool = new ClassPool(true);
    CtClass ctClass = prepareClassesForTest(classPool, "powermock.test.support.MainMockTransformerTestSupport.CallSpy.registerMethodCall($1);");
    new ClassMockTransformer().transform(ctClass);
    runTestWithNewClassLoader(classPool, ShouldIgnoreCallToSyntheticNonBridgeMethods.class.getName());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) Test(org.junit.Test)

Example 15 with ClassPool

use of javassist.ClassPool in project ratpack by ratpack.

the class DescribingHandlers method describeTo.

public static void describeTo(Handler handler, StringBuilder stringBuilder) {
    Class<? extends Handler> clazz = handler.getClass();
    if (clazz.isAnonymousClass()) {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass;
        try {
            ctClass = pool.get(clazz.getName());
            CtBehavior[] behaviors = ctClass.getDeclaredBehaviors();
            List<CtBehavior> withLineNumber = Arrays.asList(behaviors).stream().filter(input -> input.getMethodInfo().getLineNumber(0) > 0).sorted((o1, o2) -> Integer.valueOf(o1.getMethodInfo().getLineNumber(0)).compareTo(o2.getMethodInfo().getLineNumber(0))).collect(Collectors.toList());
            if (!withLineNumber.isEmpty()) {
                CtBehavior method = withLineNumber.get(0);
                int lineNumber = method.getMethodInfo().getLineNumber(0);
                ClassFile classFile = ctClass.getClassFile();
                String sourceFile = classFile.getSourceFile();
                if (lineNumber != -1 && sourceFile != null) {
                    stringBuilder.append("anonymous class ").append(clazz.getName()).append(" at approximately line ").append(lineNumber).append(" of ").append(sourceFile);
                    return;
                }
            }
        } catch (NotFoundException ignore) {
        // fall through
        }
    }
    stringBuilder.append(clazz.getName());
}
Also used : CtBehavior(javassist.CtBehavior) Arrays(java.util.Arrays) List(java.util.List) ClassFile(javassist.bytecode.ClassFile) Handler(ratpack.handling.Handler) NotFoundException(javassist.NotFoundException) CtBehavior(javassist.CtBehavior) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException)

Aggregations

ClassPool (javassist.ClassPool)44 CtClass (javassist.CtClass)32 CtMethod (javassist.CtMethod)16 NotFoundException (javassist.NotFoundException)15 CannotCompileException (javassist.CannotCompileException)10 LoaderClassPath (javassist.LoaderClassPath)9 CtField (javassist.CtField)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)6 CtConstructor (javassist.CtConstructor)6 FileNotFoundException (java.io.FileNotFoundException)5 MethodInfo (javassist.bytecode.MethodInfo)4 Method (java.lang.reflect.Method)3 ConstPool (javassist.bytecode.ConstPool)3 DirectoryInput (com.android.build.api.transform.DirectoryInput)2 JarInput (com.android.build.api.transform.JarInput)2 InjectParam (com.taobao.android.builder.tools.classinject.InjectParam)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2