加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用反射和ClassLoader创建类的实例时的ClassCastExcep

发布时间:2020-12-14 19:11:21 所属栏目:Java 来源:网络整理
导读:首先,这是Java 1.4(项目限制). 我正在尝试创建一个应用程序管理器. 它使用自己的自定义类加载器实例加载每个应用程序的主类. 之后,它使用反射创建主类的实例. 每个应用程序都实现一个公共接口,因此在创建实例后,它会运行应用程序的预定义方法. 但是,我在CRAS

首先,这是Java 1.4(项目限制).
我正在尝试创建一个应用程序管理器.
它使用自己的自定义类加载器实例加载每个应用程序的主类.
之后,它使用反射创建主类的实例.
每个应用程序都实现一个公共接口,因此在创建实例后,它会运行应用程序的预定义方法.

但是,我在CRASH POINT 1遇到了一些麻烦(见代码).该类不被视为其界面的一个实现.
如果我为这个代码块做好准备,我会在CRASH POINT 2中得到ClassCastException.

我认为这两个错误都与同一个问题有关(当然).

谁能帮我?
代码的相关部分如下(导入被删除)……

非常感谢.

马库斯

// AppManager.java

public class AppManager {
    public ThreadGroup threadGroup;
    private Class appClass;
    private AppInstance appInst;
    public AppContextImpl context;

    private AppManager(CustomClassLoader cl,String mainClass) throws ClassNotFoundException {
        final String className = mainClass;
        final CustomClassLoader finalLoader = cl;

        appClass = cl.loadClass(mainClass);

        // DEBUG CODE:
        Class[] k1 = AppInstance.class.getInterfaces();
        System.out.println(k1.length + " interfaces for AppInstance.class:");
        for (int ii = 0; ii < k1.length; ii++) {
            System.out.println("   " + ii + " - " + k1[ii].getName() + " (" + k1[ii].getClassLoader() + ")");
        }

        Class[] k2 = appClass.getInterfaces();
        System.out.println(k2.length + " interfaces for appClass instance:");
        for (int ii = 0; ii < k2.length; ii++) {
            System.out.println("   " + ii + " - " + k2[ii].getName() + " (" + k2[ii].getClassLoader() + ")");
        }

        // CRASH POINT 1
        if (!(AppInstance.class.isAssignableFrom(appClass))) {
            throw new IllegalArgumentException("Attempt to run a non-AppInstance class: " + appClass);
        }

        context = new AppContextImpl(mainClass,this);
        cl.setAppManager(this);
        Constructor m;
        try {
            m = appClass.getConstructor(new Class[0]);
           // CRASH POINT 2
            appInst = (AppInstance) m.newInstance(new Object[0]);
            appInst.init(context);
        } catch (Exception e) {
            System.out.println("Got ClassCastException here!n");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        App app1;

        String path1 = "/home/user/workspace/MultiTaskTest/bin/";
        String app1Name = "App1";

        Vector v1 = new Vector();
        try {
            v1.add(new URL(path1));
        } catch (MalformedURLException e1) {
            final File file1 = new File(path1);
            try {
                URL path1aux = (URL) AccessController.doPrivileged(
                    new PrivilegedExceptionAction() {
                        public Object run() throws IOException {
                            if (!file1.exists()) {
                                System.out.println("Warning: "" + file1.getPath() + "" not found");
                                return null;
                            }
                        return file1.toURI().toURL();
                        }
                    });

                if (path1aux != null) {
                    v1.add(path1aux);
                }
            } catch (PrivilegedActionException e) {
                e.getException().printStackTrace();
            }
    }

        final URL[] array1 = (URL[]) v1.toArray(new URL[v1.size()]);
        CustomClassLoader cl1 = (CustomClassLoader) AccessController.doPrivileged(
            new PrivilegedAction() { public Object run() {
                return new CustomClassLoader(array1);
            }});
        System.out.println("ClassLoader 1 created: " + cl1);
        try {
            app1 = new App(cl1,app1Name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("Cannot find class App1.");
        }
    }
}

// AppInstance.java

public interface AppInstance {
    public void init(ContextImpl context);
}

// App1.java

public class App1 implements AppInstance {
    private AppContextImpl contextObj;

    public void init(AppContextImpl context) {
        this.contextObj = context;
        System.out.println("Running App1...");
    }
}

// AppContextImpl.java

public class AppContextImpl {
    public String mainClass;
    public AppManager app;

    public AppContextImpl(String mainClass,AppManager app) {
        this.mainClass = mainClass;
        this.app = app;
    }
}

// CustomClassLoader.java

public class CustomClassLoader extends URLClassLoader {
    AppManager appInst;

    public CustomClassLoader(URL[] paths) { super(paths,null); }
    public void setAppManager(AppManager app) { this.appInst = app; }
}

AppManager.java文件中Debug代码的输出是:

0 interfaces for AppInstance.class:
1 interfaces for appClass instance:
   0 - AppInstance (CustomClassLoader@480457)
最佳答案
您的AppInstance类可能由每个自定义类加载器单独加载.由于类对象依赖于类加载器上的实际类AND,因此它们实际上是不同的类.
因此,类加载器1中的AppInstance与类加载器2中的AppInstance不同.

您需要做的是使用标准的类加载器层次结构:为您的应用程序使用根类加载器,并确保AppInstance可由类加载器加载.然后从根目录中创建自定义类加载器子项.每当他们需要访问AppInstance类时,他们将使用从根加载的内容.

所以,而不是这个:

public CustomClassLoader(URL[] paths) { super(paths,null); }

您需要为您的CustomClassLoader提供父级

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读