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);
}
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);
}
};
}
};
}
};
}
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() + "\");");
}
}
}
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;
}
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));
}
Aggregations