简体   繁体   English

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

[英]getDeclaredMethods() and hidden super class static method

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". 根据http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29,Class.getDeclaredMethods ()应该只包含“由类声明的方法” ”。 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(). 编译器抱怨B.m3()与A.m3()具有不兼容的返回类型。 This is dictated by JSL 8.4.8.3 (Page 225 v3 langspec-3.0.pdf). 这由JSL 8.4.8.3(第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. 输出中包含两个m2()方法。 This appears to contradict the claim that getDeclaredMethods() only returns methods "declared by the class", and "excludes inherited methods". 这似乎与getDeclaredMethods()仅返回“由类声明”的方法和“排除继承的方法”的说法相矛盾。 I don't see A.m2() as "declared by" B. 我没有看到A.m2()为“声明”B.

  • One m1() is included in the output. 输出中包含一个m1()。 Following the previous point, if it made sense to let getDeclaredMethods() return two m2(), why doesn't it return two m1() as well? 在前一点之后,如果让getDeclaredMethods()返回两个m2()是有意义的,为什么它也不返回两个m1()? 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? 在m1()和m2()的情况之间我没有看到任何有趣的区别,除了在m1()的情况下返回类型是相同的,但是我理解的返回类型不是方法签名的一部分?

Thanks in advance! 提前致谢!

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

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6815786 请参见http://bugs.sun.com/bugdatabase/view_bug.do?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 虽然在2009-03-11 05:44:57.0报告了1.6.0_12-b04但是Sun在2010-07-22 01:25:56.0接受了它

in brief: 简单来说:

Synopsis (reflect) Class.getDeclaredMethods() is returning inherited methods 概要 (反射)Class.getDeclaredMethods()返回继承的方法

Category java:classes_lang 类别 java:classes_lang

Reported Against 报道反对

State 3-Accepted, bug State 3-Accepted,bug

Priority: 3-Medium 优先级: 3 - 中等

Submit Date 11-MAR-2009 提交日期 2009年3月11日

Work Around N/A 解决 N / A问题

Evaluation Will investigate. 评估将调查。

Posted Date : 2010-07-22 01:25:56.0 发表日期: 2010-07-22 01:25:56.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM