在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
始終可見?如果不是這樣的類加載器實際上是怎樣的?