use of javassist.ClassPool in project powermock by powermock.
the class ConcreteClassGenerator method createConcreteSubClass.
public Class<?> createConcreteSubClass(Class<?> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("clazz cannot be null");
}
if (!java.lang.reflect.Modifier.isAbstract(clazz.getModifiers())) {
throw new IllegalArgumentException("clazz must be abstract");
}
ClassPool classpool = ClassPool.getDefault();
final String originalClassName = clazz.getName();
final CtClass originalClassAsCtClass;
final CtClass newClass = classpool.makeClass(generateClassName(clazz));
try {
newClass.setSuperclass(classpool.get(clazz.getName()));
} catch (Exception e1) {
throw new RuntimeException(e1);
}
try {
originalClassAsCtClass = classpool.get(originalClassName);
CtMethod[] declaredMethods = originalClassAsCtClass.getDeclaredMethods();
for (CtMethod ctMethod : declaredMethods) {
if (Modifier.isAbstract(ctMethod.getModifiers())) {
final String code = getReturnCode(ctMethod.getReturnType());
CtNewMethod.make(ctMethod.getReturnType(), ctMethod.getName(), ctMethod.getParameterTypes(), ctMethod.getExceptionTypes(), code, newClass);
}
}
if (!hasInheritableConstructor(originalClassAsCtClass)) {
return null;
}
return newClass.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javassist.ClassPool in project powermock by powermock.
the class ClassMockTransformerTest method shouldIgnoreSyntheticNonBridgeMethods.
@Test
public void shouldIgnoreSyntheticNonBridgeMethods() throws Throwable {
final ClassPool classPool = new ClassPool(true);
CtClass ctClass = prepareClassesForTest(classPool, "return;");
new ClassMockTransformer().transform(ctClass);
runTestWithNewClassLoader(classPool, ShouldIgnoreSyntheticNonBridgeMethods.class.getName());
}
use of javassist.ClassPool in project ratpack by ratpack.
the class ClosureBackedHandler method describeTo.
@Override
public void describeTo(StringBuilder stringBuilder) {
ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
if (sourceInfo == null) {
ClassPool pool = ClassPool.getDefault();
try {
CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
ClassFile classFile = ctClass.getClassFile();
String sourceFile = classFile.getSourceFile();
if (lineNumber != -1 && sourceFile != null) {
stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
} else {
stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
}
} catch (NotFoundException e) {
stringBuilder.append(invoker.getClosure().getClass().getName());
}
} else {
stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
}
}
use of javassist.ClassPool in project pinpoint by naver.
the class TestClassLoader method addTranslator.
public void addTranslator() {
final InstrumentEngine instrumentEngine = applicationContext.getInstrumentEngine();
if (instrumentEngine instanceof JavassistEngine) {
logger.info("JAVASSIST BCI engine");
ClassPool classPool = ((JavassistEngine) instrumentEngine).getClassPool(this);
this.instrumentTranslator = new JavassistTranslator(this, classPool, applicationContext.getClassFileTransformerDispatcher());
this.addTranslator(instrumentTranslator);
} else if (instrumentEngine instanceof ASMEngine) {
logger.info("ASM BCI engine");
this.instrumentTranslator = new DefaultTranslator(this, applicationContext.getClassFileTransformerDispatcher());
this.addTranslator(instrumentTranslator);
} else {
logger.info("Unknown BCI engine");
this.instrumentTranslator = new DefaultTranslator(this, applicationContext.getClassFileTransformerDispatcher());
this.addTranslator(instrumentTranslator);
}
}
use of javassist.ClassPool in project pinpoint by naver.
the class LegacyProfilerPluginClassInjector method loadFromOtherClassLoader.
private Class<?> loadFromOtherClassLoader(ClassLoader classLoader, String className) throws NotFoundException, IllegalArgumentException, IOException, CannotCompileException, IllegalAccessException, InvocationTargetException {
ClassPool pool = new ClassPool();
pool.appendClassPath(new LoaderClassPath(classLoader));
pool.appendClassPath(new LoaderClassPath(sourceClassLoader));
return loadFromOtherClassLoader(pool, classLoader, className);
}
Aggregations