我对Class的标识感到怀疑,Class的标识应该是The same classloader instance
+ The same class full path
。但是我做了一些测试用例,这是行不通的。
我有一个自定义的ClassLoader:
import java.io.*;
public class FileSystemClassLoader extends ClassLoader {
private String rootDir;
public FileSystemClassLoader(String rootDir) {
this.rootDir = rootDir;
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] classData = getClassData(name);
if (classData == null) {
throw new ClassNotFoundException();
} else {
return defineClass(name, classData, 0, classData.length);
}
}
private byte[] getClassData(String className) {
String path = classNameToPath(className);
try {
InputStream ins = new FileInputStream(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int bytesNumRead = 0;
while ((bytesNumRead = ins.read(buffer)) != -1) {
baos.write(buffer, 0, bytesNumRead);
}
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String classNameToPath(String className) {
return rootDir + File.separatorChar
+ className.replace('.', File.separatorChar) + ".class";
}
}
Sample
类:public class Sample {
private Sample instance;
public void setSample(Object instance) {
this.instance = (Sample) instance;
}
}
还有一个测试用例:
String classDataRootPath = "/Users/haolin/Github/jvm/target/classes";
FileSystemClassLoader fscl1 = new FileSystemClassLoader(classDataRootPath);
FileSystemClassLoader fscl2 = new FileSystemClassLoader(classDataRootPath);
String className = "me.hao0.jvm.classloader.Sample";
try {
Class<?> class1 = fscl1.loadClass(className);
Object obj1 = class1.newInstance();
Class<?> class2 = fscl2.loadClass(className);
Object obj2 = class2.newInstance();
Method setSampleMethod = class1.getMethod("setSample", Object.class);
setSampleMethod.invoke(obj1, obj2);
} catch (Exception e) {
e.printStackTrace();
}
当
ClassCastException
时应在setSampleMethod.invoke(obj1, obj2)
发生,因为obj1和obj2是不同的Class(它们的ClassLoader是不同的),但是代码运行良好,不会抛出ClassCastException
。有人可以建议吗?
最佳答案
除了一个重要的细节,您将是正确的。类加载器存在于层次结构中,每个ClassLoader
在尝试加载类本身之前都会委派给其父级。因此,如果您的类加载器的共同祖先找到了所请求的类,那么您的类加载器将都返回该类的相同实例。考虑以下示例:
public class Foo {
public void isFoo(Object obj) {
System.out.println("object is a Foo: " + (obj instanceof Foo));
Foo foo = (Foo) obj;
}
}
和测试:
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class Test {
public static void main(String[] args) throws Exception {
File path = new File(".");
URL[] urls = new URL[] { path.toURI().toURL() };
URLClassLoader cl1 = new URLClassLoader(urls);
URLClassLoader cl2 = new URLClassLoader(urls);
Class c1 = cl1.loadClass("Foo");
Class c2 = cl2.loadClass("Foo");
System.out.println("same class instance: " + (c1 == c2));
Object o1 = c1.newInstance();
Object o2 = c2.newInstance();
Method m = c1.getDeclaredMethod("isFoo", Object.class);
m.invoke(o1, o2);
}
}
输出为:
same class instance: true
object is a Foo: true
发生这种情况是因为当前目录是默认类路径的一部分,所以父类加载器查找并加载
Foo
,并且两个自定义类加载器均从其父级返回实例。现在,对测试类进行此更改并重新编译:
File path = new File("foo");
创建
foo/
目录,然后将Foo.class
移至该目录。现在的输出是:same class instance: false
object is a Foo: false
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Test.main(Test.java:21)
Caused by: java.lang.ClassCastException: Foo cannot be cast to Foo
at Foo.isFoo(Foo.java:4)
... 5 more
这就是您所期望的。系统类加载器无法再找到
Foo
,因此自定义类加载器将加载它的单独实例。 JVM将两个Class
实例视为不同的类,即使它们是相同的,并且强制转换失败。关于java - java类标识有关classloader,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36440731/