多个静态变量实例 [英] Multiple instances of static variables

查看:190
本文介绍了多个静态变量实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的类加载器来加载特定的类,并查看该类中的静态变量是否可以具有不同的实例。



基本上,我正在尝试编写Stephen C在这个答案



这是我的课程:



CustomClassLoader.java

  class CustomClassLoader扩展ClassLoader 
{
public class loadClass(String classname)throws ClassNotFoundException {
return super.loadClass(classname,true);
}
}

Test.java(包含驱动程序)

  class Test {
public static void main(String [] args)throws Exception {
CustomClassLoader c1 = new CustomClassLoader();
CustomClassLoader c2 = new CustomClassLoader();
等级m1,m2;

m1 = c1.loadClass(A);
m2 = c2.loadClass(A);

m1.getField(b)。set(null,10);

System.out.println(m1.getField(b)。get(null));
System.out.println(m2.getField(b)。get(null));
}

}

A.java(包含静态变量)

  A类{
public static int b = 5;
}

当我运行Test类时,我得到以下输出:

  $ java测试
10
10

我预计输出为10和5.如何让代码创建我的静态变量的两个实例?



<注意:我这样做只是为了实验和学习 - 但我有兴趣知道是否有任何真实世界的应用。

解决方案

看起来类是A正在由父类加载器加载,而不是你的CustomClassLoader(因为你调用了super.loadClass)。



以下未经测试的修正应允许您使用自己的类加载器定义A类(同时将其他所有内容委托给父加载器)。



为可怕的小屋道歉,我假设单个inputStream.read()会读取所有内容!但你可以希望看到我的意思。

  public class loadClass(String classname)抛出ClassNotFoundException {
if(classname) .equals(A)){
InputStream is = getResourceAsStream(A.class);
byte [] bodge = new byte [8192]; //应该读到EOF
试试{
int len = is.read(bodge);
返回defineClass(A,bodge,0,len);
} catch(IOException e){
e.printStackTrace();
}
}

返回super.loadClass(classname,true);
}

您最终可能会使用ClasscastExceptions或类似的东西...


I'm experimenting with using different classloaders to load a particular class, and see if the static variables in that class can have different instances.

Basically, I'm trying to write code for what Stephen C has mentioned in this answer.

Here are my classes:

CustomClassLoader.java

class CustomClassLoader extends ClassLoader
{
    public Class loadClass(String classname)  throws ClassNotFoundException {
        return super.loadClass(classname, true);
    }
}

Test.java (which contains the driver)

class Test {
        public static void main(String[] args) throws Exception {
                CustomClassLoader c1 = new CustomClassLoader();
                CustomClassLoader  c2 = new CustomClassLoader();
                Class m1, m2;

                m1 = c1.loadClass("A");
                m2 = c2.loadClass("A");

                m1.getField("b").set(null, 10);

                System.out.println(m1.getField("b").get(null));
                System.out.println(m2.getField("b").get(null));
        }

}

A.java (which contains the static variable)

class A {
        public static int b = 5;
}

When I run the Test class, I get the following output:

$ java Test
10
10

I expected the output to be 10 and 5. How can I make the code create two instances of my static variable?

Note: I'm doing this only for experimentation and learning - but I'd be interested to know if there could be any real world application of this.

解决方案

It looks as though the class "A" is being loaded by the parent class loader, rather than your CustomClassLoader (because you call super.loadClass).

The following untested amendment should allow you to define the "A" class using your own class loader (while delegating everything else to the parent loader).

Apologies for the horrible bodge where I assume the single inputStream.read() will read everything! But you can hopefully see what I mean.

    public Class loadClass(String classname)  throws ClassNotFoundException {
    if (classname.equals("A")) {
        InputStream is = getResourceAsStream("A.class");
        byte[] bodge = new byte[8192];  // Should read until EOF
        try {
            int len = is.read(bodge);
            return defineClass("A", bodge, 0, len);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return super.loadClass(classname, true);
}

You'll probably then end up with ClasscastExceptions or something similar...

这篇关于多个静态变量实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆