当我使用自定义类加载器时出现 ClassCastException
ClassCastException while I use custom classloader
我试图理解java 类加载。我写的例子:
class XLoader extends ClassLoader {
// карта отображения имен классов на файлы .class, где хранятся их определения
HashMap<String, String> mappings;
XLoader(HashMap mappings) {
this.mappings = mappings;
}
public synchronized Class loadClass(String name) throws ClassNotFoundException {
try {
// важно!
// приоритет отдан именно загрузке с помощью встроенного загрузчика
if (!mappings.containsKey(name)) {
System.out.println("loadClass (" + name + ") with parent classloader");
return super.findSystemClass(name);
}
System.out.println("loadClass (" + name + ") with XLoader");
String fileName = mappings.get(name);
FileInputStream fin = new FileInputStream(fileName);
byte[] bbuf = new byte[(int) (new File(fileName).length())];
fin.read(bbuf);
fin.close();
return defineClass(name, bbuf, 0, bbuf.length);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException(e.getMessage(), e);
}
}
}
我还有接口:
public interface ISexyInterface {
public void makeBar ();
}
和class:
public class SexyClassForLoader implements ISexyInterface {
...
}
主要方法:
public static void main(String[] args) throws Exception {
HashMap<String, String> mappings = new HashMap();
mappings.put("sur.che.SexyClassForLoader", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\SexyClassForLoader.class");
// if comment this line you will see
//mappings.put("sur.che.ISexyInterface", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\ISexyInterface.class");
XLoader xloa = new XLoader(mappings);
Class sexy_cla = xloa.loadClass("sur.che.SexyClassForLoader");
System.out.println("class was loaded with " + sexy_cla.getClassLoader());
Object sexy_ob = sexy_cla.newInstance();
System.out.println(sexy_ob.getClass().getClassLoader());
System.out.println(ISexyInterface.class.getClassLoader());
Thread.sleep(100);
ISexyInterface local_sexy = (ISexyInterface) sexy_ob;
local_sexy.makeBar();
}
此示例运行良好并产生以下输出:
loadClass (sur.che.SexyClassForLoader) with XLoader
loadClass (sur.che.ISexyInterface) with parent classloader
loadClass (java.lang.Object) with parent classloader
class was loaded with sur.che.XLoader@7f31245a
loadClass (java.lang.System) with parent classloader
loadClass (java.io.PrintStream) with parent classloader
SexyClassForLoader$$static
SexyClassForLoader$$init
sur.che.XLoader@7f31245a
sun.misc.Launcher$AppClassLoader@232204a1
make bar
但是让我们一起玩:
//mappings.put("sur.che.ISexyInterface", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\ISexyInterface.class");
如果取消注释这一行,我会看到以下输出:
loadClass (sur.che.SexyClassForLoader) with XLoader
loadClass (sur.che.ISexyInterface) with XLoader
loadClass (java.lang.Object) with parent classloader
class was loaded with sur.che.XLoader@7f31245a
loadClass (java.lang.System) with parent classloader
loadClass (java.io.PrintStream) with parent classloader
SexyClassForLoader$$static
SexyClassForLoader$$init
sur.che.XLoader@7f31245a
sun.misc.Launcher$AppClassLoader@232204a1
Exception in thread "main" java.lang.ClassCastException: sur.che.SexyClassForLoader cannot be cast to sur.che.ISexyInterface
at sur.che.Loader.main(Loader.java:25)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
请解释这种行为。
P.S.
我知道如果相同的 class 加载了 2 个不同的加载程序。它算作 2 个不同的 classses。
If uncomment this line I see following output:......
Class sexy_cla = xloa.loadClass("sur.che.SexyClassForLoader");
sur.che.SexyClassForLoader 由 Xloader 加载,因为它有一个接口 sur.che.ISexyInterface 然后 jvm 将使用 classloader of sur.che.SexyClassForLoader加载这个界面。
但是对于这个说法:
System.out.println(ISexyInterface.class.getClassLoader());
因为包含 main 方法 的 class 是由 AppLoader 加载的所以jvm会使用AppLoader加载ISexyInterface.class.
ISexyInterface local_sexy = (ISexyInterface) sexy_ob;
而且很明显,这个ISexyInterface的classLoader是AppLoader,然而ISexyInterface的内部依赖(ISexyInterface) =22=]SexyClassForLoader 由 xLoader 加载。也就是说,jvm中有两个ISexyInterface.class一个Xloader 另一个来自 App。而 sexy_ob 实现了 ISexyInterface.class 由 Xloader 加载。您尝试将 sexy_ob 转换为 ISexyInterface 类型 (由 App 加载)。
所以 castException.
如果注释,只有一个 ISexyInterface .class 由 App 加载。所以一切正常。
我试图理解java 类加载。我写的例子:
class XLoader extends ClassLoader {
// карта отображения имен классов на файлы .class, где хранятся их определения
HashMap<String, String> mappings;
XLoader(HashMap mappings) {
this.mappings = mappings;
}
public synchronized Class loadClass(String name) throws ClassNotFoundException {
try {
// важно!
// приоритет отдан именно загрузке с помощью встроенного загрузчика
if (!mappings.containsKey(name)) {
System.out.println("loadClass (" + name + ") with parent classloader");
return super.findSystemClass(name);
}
System.out.println("loadClass (" + name + ") with XLoader");
String fileName = mappings.get(name);
FileInputStream fin = new FileInputStream(fileName);
byte[] bbuf = new byte[(int) (new File(fileName).length())];
fin.read(bbuf);
fin.close();
return defineClass(name, bbuf, 0, bbuf.length);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException(e.getMessage(), e);
}
}
}
我还有接口:
public interface ISexyInterface {
public void makeBar ();
}
和class:
public class SexyClassForLoader implements ISexyInterface {
...
}
主要方法:
public static void main(String[] args) throws Exception {
HashMap<String, String> mappings = new HashMap();
mappings.put("sur.che.SexyClassForLoader", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\SexyClassForLoader.class");
// if comment this line you will see
//mappings.put("sur.che.ISexyInterface", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\ISexyInterface.class");
XLoader xloa = new XLoader(mappings);
Class sexy_cla = xloa.loadClass("sur.che.SexyClassForLoader");
System.out.println("class was loaded with " + sexy_cla.getClassLoader());
Object sexy_ob = sexy_cla.newInstance();
System.out.println(sexy_ob.getClass().getClassLoader());
System.out.println(ISexyInterface.class.getClassLoader());
Thread.sleep(100);
ISexyInterface local_sexy = (ISexyInterface) sexy_ob;
local_sexy.makeBar();
}
此示例运行良好并产生以下输出:
loadClass (sur.che.SexyClassForLoader) with XLoader
loadClass (sur.che.ISexyInterface) with parent classloader
loadClass (java.lang.Object) with parent classloader
class was loaded with sur.che.XLoader@7f31245a
loadClass (java.lang.System) with parent classloader
loadClass (java.io.PrintStream) with parent classloader
SexyClassForLoader$$static
SexyClassForLoader$$init
sur.che.XLoader@7f31245a
sun.misc.Launcher$AppClassLoader@232204a1
make bar
但是让我们一起玩:
//mappings.put("sur.che.ISexyInterface", "D:\java_things\custom-classloader\out\production\custom-classloader1\sur\che\ISexyInterface.class");
如果取消注释这一行,我会看到以下输出:
loadClass (sur.che.SexyClassForLoader) with XLoader
loadClass (sur.che.ISexyInterface) with XLoader
loadClass (java.lang.Object) with parent classloader
class was loaded with sur.che.XLoader@7f31245a
loadClass (java.lang.System) with parent classloader
loadClass (java.io.PrintStream) with parent classloader
SexyClassForLoader$$static
SexyClassForLoader$$init
sur.che.XLoader@7f31245a
sun.misc.Launcher$AppClassLoader@232204a1
Exception in thread "main" java.lang.ClassCastException: sur.che.SexyClassForLoader cannot be cast to sur.che.ISexyInterface
at sur.che.Loader.main(Loader.java:25)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
请解释这种行为。
P.S.
我知道如果相同的 class 加载了 2 个不同的加载程序。它算作 2 个不同的 classses。
If uncomment this line I see following output:......
Class sexy_cla = xloa.loadClass("sur.che.SexyClassForLoader");
sur.che.SexyClassForLoader 由 Xloader 加载,因为它有一个接口 sur.che.ISexyInterface 然后 jvm 将使用 classloader of sur.che.SexyClassForLoader加载这个界面。
但是对于这个说法:
System.out.println(ISexyInterface.class.getClassLoader());
因为包含 main 方法 的 class 是由 AppLoader 加载的所以jvm会使用AppLoader加载ISexyInterface.class.
ISexyInterface local_sexy = (ISexyInterface) sexy_ob;
而且很明显,这个ISexyInterface的classLoader是AppLoader,然而ISexyInterface的内部依赖(ISexyInterface) =22=]SexyClassForLoader 由 xLoader 加载。也就是说,jvm中有两个ISexyInterface.class一个Xloader 另一个来自 App。而 sexy_ob 实现了 ISexyInterface.class 由 Xloader 加载。您尝试将 sexy_ob 转换为 ISexyInterface 类型 (由 App 加载)。
所以 castException.
如果注释,只有一个 ISexyInterface .class 由 App 加载。所以一切正常。