use of javassist.CtClass in project powermock by powermock.
the class XStreamClassloaderExecutorTest method createClassloader.
private MockClassLoader createClassloader() {
MockClassLoader classloader = new MockClassLoader(new String[] { MyClass.class.getName(), MyArgument.class.getName(), MyReturnValue.class.getName() });
MockTransformer mainMockTransformer = new MockTransformer() {
public CtClass transform(CtClass clazz) throws Exception {
return clazz;
}
};
LinkedList<MockTransformer> linkedList = new LinkedList<MockTransformer>();
linkedList.add(mainMockTransformer);
classloader.setMockTransformerChain(linkedList);
return classloader;
}
use of javassist.CtClass in project powermock by powermock.
the class MockClassLoader method loadMockClass.
/**
* Load a mocked version of the class.
*/
private Class<?> loadMockClass(String name, ProtectionDomain protectionDomain) {
final byte[] clazz;
ClassPool.doPruning = false;
try {
CtClass type = classPool.get(name);
for (MockTransformer transformer : mockTransformerChain) {
type = transformer.transform(type);
}
javaAssistClassMarker.mark(type);
/*
* ClassPool may cause huge memory consumption if the number of CtClass
* objects becomes amazingly large (this rarely happens since Javassist
* tries to reduce memory consumption in various ways). To avoid this
* problem, you can explicitly remove an unnecessary CtClass object from
* the ClassPool. If you call detach() on a CtClass object, then that
* CtClass object is removed from the ClassPool.
*/
type.detach();
clazz = type.toBytecode();
} catch (Exception e) {
throw new IllegalStateException("Failed to transform class with name " + name + ". Reason: " + e.getMessage(), e);
}
return defineClass(name, clazz, 0, clazz.length, protectionDomain);
}
use of javassist.CtClass in project powermock by powermock.
the class AbstractMainMockTransformer method modifyMethod.
private void modifyMethod(final CtMethod method) throws NotFoundException, CannotCompileException {
if (!shouldSkipMethod(method)) {
// Lookup the method return type
final CtClass returnTypeAsCtClass = method.getReturnType();
final String returnTypeAsString = getReturnTypeAsString(method);
if (Modifier.isNative(method.getModifiers())) {
modifyNativeMethod(method, returnTypeAsCtClass, returnTypeAsString);
} else {
modifyMethod(method, returnTypeAsCtClass, returnTypeAsString);
}
}
}
use of javassist.CtClass in project powermock by powermock.
the class PowerMockClassTransformer method transform.
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (loader == null || shouldIgnore(className)) {
return null;
}
try {
String normalizedClassName = className.replace("/", ".");
if (classesToTransform != null && classesToTransform.contains(normalizedClassName)) {
ByteArrayInputStream is = new ByteArrayInputStream(classfileBuffer);
CtClass ctClass = null;
try {
ctClass = ClassPool.getDefault().makeClass(is);
} finally {
is.close();
}
if (ctClass.isInterface()) {
ctClass = INTERFACE_MOCK_TRANSFORMER.transform(ctClass);
} else {
ctClass = CLASS_MOCK_TRANSFORMER.transform(ctClass);
}
/*
* ClassPool may cause huge memory consumption if the number of CtClass
* objects becomes amazingly large (this rarely happens since Javassist
* tries to reduce memory consumption in various ways). To avoid this
* problem, you can explicitly remove an unnecessary CtClass object from
* the ClassPool. If you call detach() on a CtClass object, then that
* CtClass object is removed from the ClassPool.
*/
ctClass.detach();
javaAgentClassRegister.registerClass(loader, normalizedClassName);
return ctClass.toBytecode();
}
return null;
} catch (Exception e) {
throw new RuntimeException("Failed to redefine class " + className, e);
}
}
use of javassist.CtClass 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());
}
Aggregations