简体   繁体   中英

Does Java guarantee that Object.getClass() == Object.getClass()?

I really do mean identity-equality here.

For example, will the following always print true ?

System.out.println("foo".getClass() == "fum".getClass());

Yes, class tokens are unique (for any given classloader, that is).

Ie you will always get a reference to the same physical object within the same classloader realm . However, a different classloader will load a different class token, in conjunction with the fact that the same class definition is deemed different when loaded by two distinct classloaders.

See this earlier answer of mine for a demonstration of this.

For two instances of class X ,

x1.getClass() == x2.getClass()

only if

x1.getClass().getClassLoader() == x2.getClass().getClassLoader()

Note: Class.getClassLoader() may return null which implies the bootstrap ClassLoader.

Yes.

The returned Class object is the object that is locked by static synchronized methods of the represented class.

If it was possible to return multiple instances, then

public static synchronized void doSomething() {..}

would not be thread-safe.

It is guaranteed per classloader, as stated in the JVM specification :

First, the Java Virtual Machine determines whether it has already recorded that L is an initiating loader of a class or interface denoted by N. If so, this creation attempt is invalid and loading throws a LinkageError.

That is, if a class loader (L) tries to bypass default Class instances caching, and make the JVM load the byte[] definition more than once for the same class name (N), a LinkageError will be thrown by the JVM.

For example, implement a class loader that calls defineClass(...) each time loadClass(...) is invoked (bypassing default caching):

public class ClassloaderTest {

    private static final byte[] CLASS_DEF = readClassBytes();

    private static byte[] readClassBytes() {
        try {
            InputStream is = ClassloaderTest.class.getResourceAsStream("ClassloaderTest.class");
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[16384];
            while ((nRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return buffer.toByteArray();
        } catch (IOException ex) {
            throw new AssertionError();
        }
    }

    private static ClassLoader createNonCachingClassloader() {
        return new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                if (name.equals("classloader.ClassloaderTest")) {
                    return defineClass(name, CLASS_DEF, 0, CLASS_DEF.length);
                } else {
                    return getParent().loadClass(name);
                }
            }
        };
    }

    public static void main(String[] args) throws Exception {
        ClassLoader cl = createNonCachingClassloader();
        Class<?> cl1 = cl.loadClass("classloader.ClassloaderTest");
        Class<?> cl2 = cl.loadClass("classloader.ClassloaderTest");
        System.out.println(cl1==cl2);
    }
}

and this is what happens:

Exception in thread "main" java.lang.LinkageError: loader (instance of  classloader/ClassloaderTest$1): attempted  duplicate class definition for name: "classloader/ClassloaderTest"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
    at classloader.ClassloaderTest$1.loadClass(ClassloaderTest.java:53)
    at classloader.ClassloaderTest.main(ClassloaderTest.java:64)

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM