Search in sources :

Example 86 with NotFoundException

use of javassist.NotFoundException in project powermock by powermock.

the class JavassistMockClassLoader method loadUnmockedClass.

@Override
protected Class<?> loadUnmockedClass(String name, ProtectionDomain protectionDomain) throws ClassFormatError, ClassNotFoundException {
    byte[] bytes = null;
    try {
        /*
             * TODO This if-statement is a VERY ugly hack to avoid the
             * java.lang.ExceptionInInitializerError caused by
             * "javassist.NotFoundException:
             * net.sf.cglib.proxy.Enhancer$EnhancerKey$$KeyFactoryByCGLIB$$7fb24d72
             * ". This happens after the
             * se.jayway.examples.tests.privatefield.
             * SimplePrivateFieldServiceClassTest#testUseService(..) tests has
             * been run and all other tests will fail if this class is tried to
             * be loaded. Atm I have found no solution other than this ugly hack
             * to make it work. We really need to investigate the real cause of
             * this behavior.
             */
        if (!name.startsWith(CGLIB_ENHANCER) && !name.startsWith(CGLIB_METHOD_WRAPPER)) {
            final CtClass ctClass = classPool.get(name);
            if (ctClass.isFrozen()) {
                ctClass.defrost();
            }
            bytes = ctClass.toBytecode();
        }
    } catch (NotFoundException e) {
        return ClassLoader.getSystemClassLoader().loadClass(name);
    } catch (Exception e) {
        throw new RuntimeException("Failed to loaded class " + name, e);
    }
    return bytes == null ? null : defineClass(name, bytes, 0, bytes.length, protectionDomain);
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) NotFoundException(javassist.NotFoundException)

Example 87 with NotFoundException

use of javassist.NotFoundException in project dubbo by alibaba.

the class JValidator method generateMethodParameterClass.

/**
 * try to generate methodParameterClass.
 *
 * @param clazz interface class
 * @param method invoke method
 * @param parameterClassName generated parameterClassName
 * @return Class<?> generated methodParameterClass
 * @throws Exception
 */
private static Class<?> generateMethodParameterClass(Class<?> clazz, Method method, String parameterClassName) throws Exception {
    ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
    synchronized (parameterClassName.intern()) {
        CtClass ctClass = null;
        try {
            ctClass = pool.getCtClass(parameterClassName);
        } catch (NotFoundException ignore) {
        }
        if (null == ctClass) {
            ctClass = pool.makeClass(parameterClassName);
            ClassFile classFile = ctClass.getClassFile();
            classFile.setVersionToJava5();
            ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
            // parameter fields
            Class<?>[] parameterTypes = method.getParameterTypes();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> type = parameterTypes[i];
                Annotation[] annotations = parameterAnnotations[i];
                AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag);
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
                        javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation(classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName()));
                        Method[] members = annotation.annotationType().getMethods();
                        for (Method member : members) {
                            if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) {
                                Object value = member.invoke(annotation);
                                if (null != value) {
                                    MemberValue memberValue = createMemberValue(classFile.getConstPool(), pool.get(member.getReturnType().getName()), value);
                                    ja.addMemberValue(member.getName(), memberValue);
                                }
                            }
                        }
                        attribute.addAnnotation(ja);
                    }
                }
                String fieldName = method.getName() + "Argument" + i;
                CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName));
                ctField.getFieldInfo().addAttribute(attribute);
                ctClass.addField(ctField);
            }
            return ctClass.toClass(clazz.getClassLoader(), null);
        } else {
            return Class.forName(parameterClassName, true, clazz.getClassLoader());
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) Method(java.lang.reflect.Method) Constraint(javax.validation.Constraint) Annotation(java.lang.annotation.Annotation) CtClass(javassist.CtClass) MemberValue(javassist.bytecode.annotation.MemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) CtField(javassist.CtField) CtClass(javassist.CtClass)

Example 88 with NotFoundException

use of javassist.NotFoundException in project restfulie-java by caelum.

the class DefaultEnhancer method enhanceResource.

public <T> Class enhanceResource(Class<T> originalType) {
    ClassPool pool = ClassPool.getDefault();
    if (pool.find(DefaultEnhancer.class.getName()) == null) {
        pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
    }
    try {
        // TODO extract this enhancement to an interface and test it appart
        CtClass newType = pool.makeClass(generateNewUniqueClassName(originalType));
        newType.setSuperclass(pool.get(originalType.getName()));
        newType.addInterface(pool.get(Resource.class.getName()));
        enhanceLinks(newType);
        return newType.toClass();
    } catch (NotFoundException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    } catch (CannotCompileException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException)

Aggregations

NotFoundException (javassist.NotFoundException)88 CtClass (javassist.CtClass)64 CannotCompileException (javassist.CannotCompileException)42 CtMethod (javassist.CtMethod)36 ClassPool (javassist.ClassPool)32 IOException (java.io.IOException)19 CtField (javassist.CtField)16 FileNotFoundException (java.io.FileNotFoundException)9 CtConstructor (javassist.CtConstructor)9 File (java.io.File)7 Method (java.lang.reflect.Method)7 ClassFile (javassist.bytecode.ClassFile)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 BadBytecode (javassist.bytecode.BadBytecode)6 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 MethodUsage (com.github.javaparser.resolution.MethodUsage)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)4 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)4 Context (com.github.javaparser.symbolsolver.core.resolution.Context)4