Search in sources :

Example 26 with ClassPool

use of javassist.ClassPool in project hibernate-orm by hibernate.

the class EnhancerImpl method buildClassPool.

private ClassPool buildClassPool(JavassistEnhancementContext enhancementContext) {
    ClassPool classPool = new ClassPool(false) {

        @Override
        public ClassLoader getClassLoader() {
            return enhancementContext.getLoadingClassLoader();
        }
    };
    ClassLoader loadingClassLoader = enhancementContext.getLoadingClassLoader();
    if (loadingClassLoader != null) {
        classPool.appendClassPath(new LoaderClassPath(loadingClassLoader));
    }
    return classPool;
}
Also used : ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Example 27 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 28 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 29 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 30 with ClassPool

use of javassist.ClassPool in project dubbo by alibaba.

the class ClassGenerator method getClassPool.

public static ClassPool getClassPool(ClassLoader loader) {
    if (loader == null)
        return ClassPool.getDefault();
    ClassPool pool = POOL_MAP.get(loader);
    if (pool == null) {
        pool = new ClassPool(true);
        pool.appendClassPath(new LoaderClassPath(loader));
        POOL_MAP.put(loader, pool);
    }
    return pool;
}
Also used : ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Aggregations

ClassPool (javassist.ClassPool)44 CtClass (javassist.CtClass)32 CtMethod (javassist.CtMethod)16 NotFoundException (javassist.NotFoundException)15 CannotCompileException (javassist.CannotCompileException)10 LoaderClassPath (javassist.LoaderClassPath)9 CtField (javassist.CtField)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)6 CtConstructor (javassist.CtConstructor)6 FileNotFoundException (java.io.FileNotFoundException)5 MethodInfo (javassist.bytecode.MethodInfo)4 Method (java.lang.reflect.Method)3 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