Nic*_*olt 3 java parameters binding constructor binary-compatibility
我在一个普通的jar中有以下类:
public class Common
{
public Common(List list)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将构造函数参数从a更改List为a Collection,如下所示:
public class Common
{
public Common(Collection collection)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
重建公共jar并运行系统会NoSuchMethodError在调用构造函数时导致任何依赖类,直到我重新编译该类.
我有一些想法是什么导致这一点,沿着构造函数如何绑定在依赖类的字节码中,但我不是100%肯定.
请问有人可以了解这里发生的事情吗?
我随后做了一个快速测试并看了一下字节码:
Compiled from "Client.java"
public class Client extends java.lang.Object{
public Client();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2; //class ArrayList
3: dup
4: invokespecial #3; //Method java/util/ArrayList."<init>":()V
7: astore_1
8: new #4; //class Common
11: dup
12: aload_1
13: invokespecial #5; //Method Common."<init>":(Ljava/util/List;)V
16: pop
17: return
}
Run Code Online (Sandbox Code Playgroud)
就像Tom说的那样,正如你在第13行看到的那样,确切的构造函数在编译时被绑定.
你每天学习新的东西 :-)
| 归档时间: |
|
| 查看次数: |
603 次 |
| 最近记录: |