在question I asked earlier我知道为了确保某些注释存在或不在某个类的某个位置,我需要使用可以访问注解和类的类加载器重新加载它。如何重新加载一个类,使注解变得可见?
现在我正在努力如何这样的类加载器工作。在我的设置中,我只是将注释标记为java.lang.Class
实例,并且可能使用该注释注释的类也是java.lang.Class
实例。两者都可能被一些我不知道的不同类加载器加载(类可能被远程加载,因此它们不在本地文件系统中)。
在寻找我found this JoinClassLoader
/**
* A class loader that combines multiple class loaders into one.<br>
* The classes loaded by this class loader are associated with this class loader,
* i.e. Class.getClassLoader() points to this class loader.
* <p>
* Author Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland, www.source-code.biz<br>
* License: LGPL, http://www.gnu.org/licenses/lgpl.html<br>
* Please contact the author if you need another license.
*/
public class JoinClassLoader extends ClassLoader {
private ClassLoader[] delegateClassLoaders;
public JoinClassLoader (ClassLoader parent, ClassLoader... delegateClassLoaders) {
super (parent);
this.delegateClassLoaders = delegateClassLoaders; }
protected Class<?> findClass (String name) throws ClassNotFoundException {
// It would be easier to call the loadClass() methods of the delegateClassLoaders
// here, but we have to load the class from the byte code ourselves, because we
// need it to be associated with our class loader.
String path = name.replace('.', '/') + ".class";
URL url = findResource(path);
if (url == null) {
throw new ClassNotFoundException (name); }
ByteBuffer byteCode;
try {
byteCode = loadResource(url); }
catch (IOException e) {
throw new ClassNotFoundException (name, e); }
return defineClass(name, byteCode, null); }
// some code omitted
} // end class JoinClassLoader
所以我的问题是这样的:
鉴于C
任意类可以由任意的类加载器加载注释类A
的类实例的类实例。使用C
和A
的类加载器按照该顺序实例化JoinClassLoader
作为委派类加载器。请问JoinClassLoader
在调用findClass
时重新加载类C
,以便在实际注释C
时注释A
始终可见?如果不是这样的类加载器实际上是怎样的?