getDeclaredMethods() 和隐藏超类静态方法

发布于 2024-10-01 05:35:16 字数 1321 浏览 9 评论 0原文

根据 http:// /download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29,Class.getDeclaredMethods() 应该只包含“由类声明”的方法。然而,我通过下面的测试代码得到了一些非常令人惊讶的结果:

import java.util.Arrays;

class A {
 public static A m1() { return null; }
 public static A m2() { return null; }
 public static A m3() { return null; }
}

class B extends A {
 public static A m1() { return null; }
 public static B m2() { return null; }
// public static Object m3() { return null; } won't compile
}


public class Scratch {
 public static void main(final String[] args) {
  System.out.println(Arrays.asList(B.class.getDeclaredMethods()));
 }
}

一些对我来说看起来很奇怪/令人惊讶的事情:

  • 编译器抱怨 B.m3() 的返回类型与 A.m3() 不兼容。这是由 JSL 8.4.8.3 (Page 225 v3 langspec-3.0.pdf) 规定的。但我很好奇,为什么这个限制需要应用于静态方法。我的理解是静态方法可以隐藏而不是重写,并且引用在编译时解析,那么这个限制背后的原因是什么?

  • 输出中包含两个 m2() 方法。这似乎与 getDeclaredMethods() 仅返回“由类声明的”方法和“排除继承的方法”的说法相矛盾。我没有将 A.m2() 视为“由”B 声明。

  • 输出中包含一个 m1()。继上一点之后,如果让 getDeclaredMethods() 返回两个 m2() 有意义,为什么它不返回两个 m1() 呢?毕竟它们是两种不同的方法,一种被另一种隐藏了。除了 m1() 的返回类型相同之外,我没有看到 m1() 和 m2() 的情况有任何根本区别,但据我了解,返回类型不是方法签名的一部分?< /p>

提前致谢!

According to http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29, Class.getDeclaredMethods() should only include methods "declared by the class". However, I am getting some pretty surprising results with the test code below:

import java.util.Arrays;

class A {
 public static A m1() { return null; }
 public static A m2() { return null; }
 public static A m3() { return null; }
}

class B extends A {
 public static A m1() { return null; }
 public static B m2() { return null; }
// public static Object m3() { return null; } won't compile
}


public class Scratch {
 public static void main(final String[] args) {
  System.out.println(Arrays.asList(B.class.getDeclaredMethods()));
 }
}

A few things that look quite strange/surprising to me:

  • The compiler complain about B.m3() having an incompatible return type with A.m3(). This is dictated by JSL 8.4.8.3 (Page 225 v3 langspec-3.0.pdf). But I am curious, why this restriction needs to apply to static methods. My understanding is that static methods can be hidden and not overriden, and that the reference is resolved at compile time, so what is the reasoning behind this restriction?

  • Two m2() methods are included in the output. This appears to contradict the claim that getDeclaredMethods() only returns methods "declared by the class", and "excludes inherited methods". I don't see A.m2() as "declared by" B.

  • One m1() is included in the output. Following the previous point, if it made sense to let getDeclaredMethods() return two m2(), why doesn't it return two m1() as well? They are after all two distinct methods, and one is hidden by the other. I don't see any fundemental difference between the case of m1() and m2() except the return type being the same in the case of m1(), but the return type as I understand is not part of the method signature?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2024-10-08 05:35:16

我可以确认该错误 https://bugs.java.com/bugdatabase/view_bug? bug_id=6815786 仍然存在于 Oracle Java Windows 64 位 1.6.0_35 中。

I can confirm that the bug https://bugs.java.com/bugdatabase/view_bug?bug_id=6815786 is still present in Oracle Java Windows 64-bit 1.6.0_35.

岁月蹉跎了容颜 2024-10-08 05:35:16

请参阅https://bugs.java.com/bugdatabase/view_bug?bug_id=6815786

虽然已于 2009-03-11 05:44:57.0 报告针对 1.6.0_12-b04
但Sun于2010-07-22 01:25:56.0

简单地接受了它:

概要(反映)Class.getDeclaredMethods()正在返回继承的方法

类别 java:classes_lang

<针对状态报告

3-已接受,错误

优先级: 3-中

提交日期 2009 年 3 月 11 日

解决方法 N/A

评估 将进行调查。

发布日期:2010-07-22 01:25:56.0

See https://bugs.java.com/bugdatabase/view_bug?bug_id=6815786

Though it had been reported against 1.6.0_12-b04 on 2009-03-11 05:44:57.0
but Sun accepted it on 2010-07-22 01:25:56.0

in brief:

Synopsis (reflect) Class.getDeclaredMethods() is returning inherited methods

Category java:classes_lang

Reported Against

State 3-Accepted, bug

Priority: 3-Medium

Submit Date 11-MAR-2009

Work Around N/A

Evaluation Will investigate.

Posted Date : 2010-07-22 01:25:56.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文