gpt4 book ai didi

java - 为什么在未使用的代码中包含对缺失接口(interface)的方法调用的类会导致 Java 类加载错误?

转载 作者:太空狗 更新时间:2023-10-29 22:56:56 26 4
gpt4 key购买 nike

我看到一些类加载行为似乎与 JVM 规范不一致,我想知道这是否是一个错误。如果没有,希望有人能解释原因。

下面的示例代码只是从其 main 方法中打印 hello。它有一个未使用的方法,其中包含对方法的方法调用,该方法声明它采用“C”(接口(interface))作为参数。

当 main 执行时(类路径中没有 A、B 和 C) 为接口(interface) C 抛出 ClassNotFound 错误。(注意 C 在运行时实际上从来不需要,因为它只是在从不执行的方法中引用)。

这似乎违反了 JVM 规范

Java VM 规范第 2 版第 2.17.1 节说:

The only requirement regarding when resolution is performed is that any errors detected during resolution must be thrown at a point in the program where some action is taken by the program that might, directly or indirectly, require linkage to the class or interface involved in the error

Java VM 规范第 2 版第 2.17.3 节说:

The Java programming language allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the 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.

注意:如果我将定义中的参数类型更改为类而不是接口(interface),那么代码将正确加载和执行。

/**
* This version fails, the method call in neverCalled() is to a method whose
* parameter definition is for an Interface
*/
public class Main {

public void neverCalled(){
A a = new A();
B b = new B(); // B implements C

//method takeInter is declared to take paramters of type Interface C
//This code is causes a ClassNotFound error do be thrown when Main
//is loaded if A, B, and C is not in the class path
a.takeInter(b);
}

public static void main(String[] args) {
System.out.println("Hello...");
}
}


/**
* This version runs, the method call in neverCalled() is to a method whose
* parameter definition is for a Class
*/
public class Main {

public void neverCalled(){
A a = new A();
B b = new B(); // B implements C

//method takeInter is declared to take paramters of type Interface C
//This code is causes a ClassNotFound error do be thrown when Main
//is loaded if A, B, and C is not in the class path
a.takeClass(b);
}

public static void main(String[] args) {
System.out.println("Hello...");
}
}


public class A {
public void takeClass(B in){};
public void takeInter(C in){}
}

public class B implements C {}

public interface C {}

埃德,

我并不是故意要断章取义,我只是抽出了我认为相关的部分。感谢您帮助我理解这一点。

无论如何,规范对我来说似乎很清楚。它说错误必须在一个点而不是一个点抛出。诚然,我在阅读了 Inside The Java Virtual Machine 第 8 章中的以下内容后阅读了 VM 规范,所以这可能影响了我的解释。

来自,http://www.artima.com/insidejvm/ed2/linkmod.html

As described in Chapter 7, "The Lifetime of a Class," different implementations of the Java virtual machine are permitted to perform resolution at different times during the execution of a program. An implementation may choose to link everything up front by following all symbolic references from the initial class, then all symbolic references from subsequent classes, until every symbolic reference has been resolved. In this case, the application would be completely linked before its main() method was ever invoked. This approach is called early resolution. Alternatively, an implementation may choose to wait until the very last minute to resolve each symbolic reference. In this case, the Java virtual machine would resolve a symbolic reference only when it is first used by the running program. This approach is called late resolution. Implementations may also use a resolution strategy in-between these two extremes.

Although a Java virtual machine implementation has some freedom in choosing when to resolve symbolic references, every Java virtual machine must give the outward impression that it uses late resolution. No matter when a particular Java virtual machine performs its resolution, it will always throw any error that results from attempting to resolve a symbolic reference at the point in the execution of the program where the symbolic reference was actually used for the first time. In this way, it will always appear to the user as if the resolution were late. If a Java virtual machine does early resolution, and during early resolution discovers that a class file is missing, it won't report the class file missing by throwing the appropriate error until later in the program when something in that class file is actually used. If the class is never used by the program, the error will never be thrown.

最佳答案

这是一个同样失败的更简单的示例。

public class Main {
public void neverCalled() {
A a = new A();
B b = new B();
a.takeInter(b);
}

public static void main(String[] args) {
System.out.println("Hello...");
}
}

class A {
public void takeInter(A in) {
}
}

class B extends A {
}

class C {
}

在字节码中

public void neverCalled();
Code:
0: new #2 // class A
3: dup
4: invokespecial #3 // Method A."<init>":()V
7: astore_1
8: new #4 // class B
11: dup
12: invokespecial #5 // Method B."<init>":()V
15: astore_2
16: aload_1
17: aload_2
18: invokevirtual #6 // Method A.takeInter:(LA;)V
21: return

b 被隐式转换为 A,它似乎需要对此​​进行检查。

如果您关闭所有验证,则不会发生错误。

$ rm A.class B.class C.class 
$ java -Xverify:none -cp . Main
Hello...
$ java -cp . Main
Exception in thread "main" java.lang.NoClassDefFoundError: A

关于java - 为什么在未使用的代码中包含对缺失接口(interface)的方法调用的类会导致 Java 类加载错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8185543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com