简体   繁体   中英

resolveClass doesn't resolve symbolic references

JLS says that resolveClass method should verify all symbolic links

This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the Java programming language are respected, that a class or interface is completely verified and prepared before it is initialized, and that errors detected during linkage are thrown at a point in the program where some action is taken by the program that might require linkage to the class or interface involved in the error.

So I tried to create class T that references another class in the first project and created the second project with a custom class loader loads class T but not load another referenced class.

public class T {
    public static AnotherClass field = new AnotherClass();
}
public class AnonClassLoader extends ClassLoader {

    public Class findClass(String str) {
        byte[] bytes = new byte[0];
        try {
            bytes = Files.readAllBytes(Paths.get(str));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Class<?> aClass = defineClass(null, bytes, 0, bytes.length);
        return aClass;
    }

    public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException, ClassNotFoundException {
        AnonClassLoader anonClassLoader = new AnonClassLoader();
        Class<?> aClass = anonClassLoader.loadClass("/Users/root/IdeaProjects/untitled/T.class", true);
        System.out.println(aClass.getName());
    }
}

So I expect to get NoClassDefFound as soon as possible, but the actual result - no errors are thrown, class resolved successfully

So, according to Bug ID: JDK-8057777 Cleanup of old and unused VM interfaces there is no static resolving in the hotspot.

Meanwhile, in hotspot jdk 8 native function has no implementation

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