/** * Converts the given class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * To load the class, this method uses the context class loader * of the current thread. It is obtained by calling * <code>getClassLoader()</code>. * * <p>This behavior can be changed by subclassing the pool and changing * the <code>getClassLoader()</code> method. * If the program is running on some application * server, the context class loader might be inappropriate to load the * class. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @see #toClass(CtClass, java.lang.ClassLoader, ProtectionDomain) * @see #getClassLoader() */ public Class toClass(CtClass clazz) throws CannotCompileException { // Some subclasses of ClassPool may override toClass(CtClass,ClassLoader). // So we should call that method instead of toClass(.., ProtectionDomain). return toClass(clazz, getClassLoader()); }
return toClass(clazz, getClassLoader());
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); try { return anno.toAnnotationType(cl2, cp); } catch (ClassNotFoundException e2){ try { Class clazz = cp.get(anno.getTypeName()).toClass(); return javassist.bytecode.annotation.AnnotationImpl.make( clazz.getClassLoader(), clazz, cp, anno); } catch (Throwable e3) { throw new ClassNotFoundException(anno.getTypeName()); } } } }
loader = cp.getClassLoader();
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); try { return anno.toAnnotationType(cl2, cp); } catch (ClassNotFoundException e2){ try { Class<?> clazz = cp.get(anno.getTypeName()).toClass(); return javassist.bytecode.annotation.AnnotationImpl.make( clazz.getClassLoader(), clazz, cp, anno); } catch (Throwable e3) { throw new ClassNotFoundException(anno.getTypeName()); } } } }
loader = cp.getClassLoader();
protected static Class<?> loadClas(CtClass clazz, String annotationClassname) throws ClassNotFoundException { return clazz.getClassPool().getClassLoader().loadClass(annotationClassname); }
private void debugClassLoader(final ClassPool classPool) { if (!logger.isDebugEnabled()) { return; } logger.debug(" - classPool: {}", classPool.toString()); ClassLoader classLoader = classPool.getClassLoader(); while (classLoader != null) { logger.debug(" -- {}: {}", classLoader.getClass().getName(), classLoader.toString()); if (classLoader instanceof URLClassLoader) { logger.debug(" --- urls: {}", Arrays.deepToString(((URLClassLoader) classLoader).getURLs())); } classLoader = classLoader.getParent(); } } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } }
public static ClassLoader getClassLoader(CtClass ctClass) { ClassPool pool = ctClass.getClassPool(); ClassLoader loader = null; if (pool != null) loader = pool.getClassLoader(); if (loader == null) loader = SecurityActions.getContextClassLoader(); return loader; }
/** * Get the information for a class * * @param ctClass the class * @return the info */ protected TypeInfo get(CtClass ctClass) { if (ctClass == null) throw new IllegalArgumentException("Null class"); try { return get(ctClass, null, ctClass.getName(), ctClass.getClassPool().getClassLoader(), false); } catch(Exception e) { throw new RuntimeException(e); } }
public static ClassAdvisor getClassAdvisor(CtClass clazz, AspectManager am) { ClassAdvisor classAdvisor = getClassAdvisor(clazz.getName(), am, null); classAdvisor.setClassLoader(clazz.getClassPool().getClassLoader()); return classAdvisor; }
public MutableMethodInfo getDeclaredMethod(String methodName, String... parameters) throws ClassNotFoundException { TypeInfo[] typeParams = new TypeInfo[parameters.length]; for(int i=0; i<parameters.length;i++) { typeParams[i] = factory.getTypeInfo(parameters[i], ctClass.getClassPool().getClassLoader()); } return getDeclaredMethod(methodName, typeParams); }
public MutableConstructorInfo getDeclaredConstructor(String... parameters) throws ClassNotFoundException { TypeInfo[] typeParams = new TypeInfo[parameters.length]; for(int i=0; i<parameters.length;i++) { typeParams[i] = factory.getTypeInfo(parameters[i], ctClass.getClassPool().getClassLoader()); } return getDeclaredConstructor(typeParams); }
public JavassistTemplateBuilder(TemplateRegistry registry, ClassLoader cl) { super(registry); pool = new ClassPool(); pool.appendClassPath(new ClassClassPath(getClass())); boolean appended = false; loader = cl; if (loader == null) { loader = pool.getClassLoader(); } try { if (loader != null) { pool.appendClassPath(new LoaderClassPath(loader)); appended = true; } } catch (SecurityException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Cannot append a search path of classloader", e); } } if (!appended) { pool.appendSystemPath(); } }
import java.lang.reflect.Field; import javassist.ClassPool; import javassist.CtClass; import javassist.CtField; import javassist.bytecode.AnnotationsAttribute; import javassist.bytecode.ClassFile; import javassist.bytecode.ConstPool; import javassist.bytecode.annotation.Annotation; public class AddingAnnotationDynamically { public static void main(String[] args) throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass cc = cp.get("scala.Option"); // Without the call to "makePackage()", package information is lost cp.makePackage(cp.getClassLoader(), pkgName); ClassFile cfile = cc.getClassFile(); ConstPool cpool = cfile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation(annotationName, cpool); attr.addAnnotation(annot); cfile.addAttribute(attr); // Changes are not persisted without a call to "toClass()" Class<?> c = cc.toClass(); } }