为什么java.lang.Class没有实现java.lang.reflect.Member接口?

huangapple go评论63阅读模式
英文:

Why java.lang.Class doesn't implement java.lang.reflect.Member interface?

问题

我正在研究在Java中创建一个包浏览器,尝试编写一个通用方法,可以处理类的所有成员 - 基本上收集它们的字符串表示,选择图像并在树视图中显示它们。

对于所有成员(字段、方法、构造方法),它都运行得很好 - 它们都有getName()isSynthetic(),这使我能够以相同的方式对待它们,因为这就是我所需要的。

是特殊的 - 即使它具有该接口的所有方法,但从技术上讲,它并没有实现它!

为了处理它,我需要一个特殊的方法或执行instanceof检查,这并不是很干净的方法。

所以,我想,这更多是一个哲学问题 - 为什么会这样?因为类不被认为是类的“合适”成员,而是嵌套的?还是因为其他原因?

请为这个主题提供一些见解!

我尝试将类型为Class<?>的对象视为实现了接口Member

英文:

I'm working on a Package Browser in browser in Java and trying to write a generic method which can handle all members of the class - basically collecting string representation of them, choosing an image and displaying it in a tree view.

For all Members (Field, Method, Constructor) it has been working great - they all have getName() and isSynthetic() which allows me to treat them in the same way because that's all I need.

But Class is special - even though it has all the methods of that interface, it doesn't technically implement it!.

And to handle it I need a special-case-method or do an instanceof checks, which is not very clean.

So, i guess, this is more of a philosophical question - why is that? Because classes are not considered to be "proper" members of classes and instead are nested? Or because of something else?

Please shine the light on this topic!

I tried to treat objects of type Class<?> as if they implemented interface Member.

答案1

得分: 0

为什么你期望 Class 实现 Member

默认情况下,一个类是顶级类,而不是嵌套类。因此,它不是任何其他类的成员。

我想要表达的是,只有当 class B 是在某个 class A 中声明的嵌套类时,它才是一个 Member。但是这样的话,A 也实现 Member 就没有意义了。

今天我的措辞不太好,希望我能清楚地传达我的观点。

英文:

Why do you expect Class to implement Member?

A class, by default, is a top level class and not a nested one. Thus, it's not a member of any other class.

What I'm trying to say is that a class B is only a Member if it's a nested class declared in some class A. But then it doesn't make sense that A also implements Member.

I am not good with words today, I hope I got my point communicated clearly.

答案2

得分: 0

我认为答案应该在 getDeclaringClass 中找到。

Member

返回表示声明此成员或构造函数的类或接口的 Class 对象。

Class

如果此 Class 对象表示另一个类的成员或接口,则返回表示声明它的类的 Class 对象。如果此类或接口不是任何其他类的成员,则此方法返回 null。如果此 Class 对象表示数组类、原始类型或 void,则此方法返回 null。

这可能的 null 是主要的区别。Member 的实例不允许返回 null,而 Class 的实例允许。如果 Oracle 改变了 Member.getDeclaringClass 的规范以允许返回 null 值,可能会破坏现有的代码。他们可以在 JDK 自身中修复这些问题,但无法修复第三方代码。

英文:

I think the answer should be found in getDeclaringClass.

From Member:
> Returns the Class object representing the class or interface that declares the member or constructor represented by this Member.

From Class:
> If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared. This method returns null if this class or interface is not a member of any other class. If this Class object represents an array class, a primitive type, or void,then this method returns null.

That possible null is the main difference. Instances of Member are not allowed to return null, instances of Class are. If Oracle would change the specification of Member.getDeclaringClass to allow null return values, that could break existing code. They can fix those occurrences in the JDK itself, but not in third party code.

huangapple
  • 本文由 发表于 2023年6月26日 19:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定