有没有一个
existing answer来确定一个方法是否被覆盖,如果没有泛型涉及.
Java将由于type erasure而完全删除泛型类型.因此字节码将是:
class FooImpl implements Foo {
FooImpl();
Code:
0: aload_0
1: invokespecial #10 // Method java/lang/Object.””:()V
4: return
public void doStuff(java.lang.String);
Code:
0: return
public void doStuff(java.lang.Object);
Code:
0: aload_0
1: aload_1
2: checkcast #21 // class java/lang/String
5: invokevirtual #23 // Method doStuff:(Ljava/lang/String;)V
8: return
}
有两个doStuff方法.它是一个bridge method.它只是做类型转换并调用void doStuff(java.lang.String),所以实际上void doStuff(java.lang.String)不被覆盖,但是void doStuff(java.lang.Object)是.当您使用多态时,如:
Foo foo = new FooImpl();
foo.doStuff(“ABC”)
它实际上调用void doStuff(java.lang.Object).所以如果你使用上面的链接来检测是否覆盖了void doStuff(java.lang.Object),那么它会报告yes.
public static void main(java.lang.String[]);
Code:
0: new #1 // class FooImpl
3: dup
4: invokespecial #22 // Method “”:()V
7: astore_1
8: aload_1
9: ldc #23 // String ABC
11: invokeinterface #25, 2 // InterfaceMethod Foo.doStuff:(Ljava/lang/Object;)V
16: return