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);
}
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());
}
}
}
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);
}
}
Aggregations