Search in sources :

Example 36 with ClassPool

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);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod)

Example 37 with ClassPool

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());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) Test(org.junit.Test)

Example 38 with ClassPool

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());
    }
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) ClosureUtil(ratpack.groovy.internal.ClosureUtil) CtMethod(javassist.CtMethod)

Example 39 with ClassPool

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);
    }
}
Also used : ASMEngine(com.navercorp.pinpoint.profiler.instrument.ASMEngine) JavassistEngine(com.navercorp.pinpoint.profiler.instrument.JavassistEngine) ClassPool(javassist.ClassPool) InstrumentEngine(com.navercorp.pinpoint.profiler.instrument.InstrumentEngine)

Example 40 with ClassPool

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);
}
Also used : ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Aggregations

ClassPool (javassist.ClassPool)49 CtClass (javassist.CtClass)37 CtMethod (javassist.CtMethod)18 NotFoundException (javassist.NotFoundException)17 CannotCompileException (javassist.CannotCompileException)11 LoaderClassPath (javassist.LoaderClassPath)9 IOException (java.io.IOException)8 CtField (javassist.CtField)8 Test (org.junit.Test)7 File (java.io.File)6 CtConstructor (javassist.CtConstructor)6 FileNotFoundException (java.io.FileNotFoundException)5 MethodInfo (javassist.bytecode.MethodInfo)4 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 Method (java.lang.reflect.Method)2