Search in sources :

Example 6 with CtMethod

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

use of javassist.CtMethod in project powermock by powermock.

the class TestClassTransformer method forTestClass.

public static ForTestClass forTestClass(final Class<?> testClass) {
    return new ForTestClass() {

        @Override
        public RemovesTestMethodAnnotation removesTestMethodAnnotation(final Class<? extends Annotation> testMethodAnnotation) {
            return new RemovesTestMethodAnnotation() {

                @Override
                public TestClassTransformer fromMethods(final Collection<Method> testMethodsThatRunOnOtherClassLoaders) {
                    return new TestClassTransformer(testClass, testMethodAnnotation) {

                        /**
                             * Is lazily initilized because of
                             * AbstractTestSuiteChunkerImpl#chunkClass(Class)
                             */
                        Collection<String> methodsThatRunOnOtherClassLoaders;

                        @Override
                        boolean mustHaveTestAnnotationRemoved(CtMethod method) throws NotFoundException {
                            if (null == methodsThatRunOnOtherClassLoaders) {
                                /* This lazy initialization is necessary - see above */
                                methodsThatRunOnOtherClassLoaders = new HashSet<String>();
                                for (Method m : testMethodsThatRunOnOtherClassLoaders) {
                                    methodsThatRunOnOtherClassLoaders.add(signatureOf(m));
                                }
                                testMethodsThatRunOnOtherClassLoaders.clear();
                            }
                            return methodsThatRunOnOtherClassLoaders.contains(signatureOf(method));
                        }
                    };
                }

                @Override
                public TestClassTransformer fromAllMethodsExcept(Method singleMethodToRunOnTargetClassLoader) {
                    final String targetMethodSignature = signatureOf(singleMethodToRunOnTargetClassLoader);
                    return new TestClassTransformer(testClass, testMethodAnnotation) {

                        @Override
                        boolean mustHaveTestAnnotationRemoved(CtMethod method) throws Exception {
                            return !signatureOf(method).equals(targetMethodSignature);
                        }
                    };
                }
            };
        }
    };
}
Also used : Collection(java.util.Collection) CtClass(javassist.CtClass) IndicateReloadClass(org.powermock.core.IndicateReloadClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) CtMethod(javassist.CtMethod)

Example 8 with CtMethod

use of javassist.CtMethod in project powermock by powermock.

the class ClassMockTransformerTest method addSyntheticMethod.

private void addSyntheticMethod(ClassPool classPool, CtClass ctClass, String body) throws NotFoundException, CannotCompileException {
    CtMethod ctMethod = CtNewMethod.make(AccessFlag.SYNTHETIC, CtClass.voidType, SYNTHETIC_METHOD_NAME, new CtClass[] { classPool.get(String.class.getName()) }, null, body, ctClass);
    ctClass.addMethod(ctMethod);
    for (CtMethod method : ctClass.getDeclaredMethods()) {
        if (!method.getName().equals(SYNTHETIC_METHOD_NAME)) {
            method.insertBefore("$synth(\"" + method.getLongName() + "\");");
        }
    }
}
Also used : CtMethod(javassist.CtMethod)

Example 9 with CtMethod

use of javassist.CtMethod in project powermock by powermock.

the class AbstractMainMockTransformer method ensureJvmMethodSizeLimit.

/**
     * According to JVM specification method size must be lower than 65536 bytes.
     * When that limit is exceeded class loader will fail to load the class.
     * Since instrumentation can increase method size significantly it must be
     * ensured that JVM limit is not exceeded.
     * <p/>
     * When the limit is exceeded method's body is replaced by exception throw.
     * Method is then instrumented again to allow mocking and suppression.
     *
     * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3">JVM specification</a>
     */
protected CtClass ensureJvmMethodSizeLimit(CtClass clazz) throws CannotCompileException, NotFoundException {
    for (CtMethod method : clazz.getDeclaredMethods()) {
        if (isMethodSizeExceeded(method)) {
            String code = "{throw new IllegalAccessException(\"" + "Method was too large and after instrumentation exceeded JVM limit. " + "PowerMock modified the method to allow JVM to load the class. " + "You can use PowerMock API to suppress or mock this method behaviour." + "\");}";
            method.setBody(code);
            modifyMethod(method);
        }
    }
    return clazz;
}
Also used : CtMethod(javassist.CtMethod)

Example 10 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class AfterBurnerTest method assertHasFooMethodWithReturnValue.

private void assertHasFooMethodWithReturnValue(CtClass clazz, boolean returnValue) throws Exception {
    CtMethod fooMethod = clazz.getDeclaredMethod("foo");
    assertNotNull(fooMethod);
    // we also need to check if code has been copied
    Method realFooMethod = targetInstance.getClass().getMethod("foo");
    assertEquals(returnValue, realFooMethod.invoke(targetInstance));
}
Also used : CtMethod(javassist.CtMethod) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) CtNewMethod(javassist.CtNewMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) Method(java.lang.reflect.Method) CtMethod(javassist.CtMethod)

Aggregations

CtMethod (javassist.CtMethod)70 CtClass (javassist.CtClass)42 CannotCompileException (javassist.CannotCompileException)20 NotFoundException (javassist.NotFoundException)20 ClassPool (javassist.ClassPool)16 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2