-1

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.

2 Answers2

0

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.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • But it has all the methods, and can answer all the same questions that Member can - I can just ask, for a specific instance of Class>, what is yours declaring class - and that will give me the result that's different from my own class (if it's the nested class) or null (if it's not nested). EDIT: By that I mean, that a Class _can_ be treated as a Member, if you squint your eyes just a bit. It passes [duck test](https://en.wikipedia.org/wiki/Duck_test) in my book. – Andriy Mykhaylyk Jun 26 '23 at 11:39
  • @AndriyMykhaylyk You are correct, but that's not a good reason to implement `Member`. Type hierarchies have to make sense semantically, implementing the same methods isn't the only requirement. Is a `Class` always a `Member`? No, so it shouldn't implement that interface. However, it might make sense to have an `InnerClass` that extends `Class` and implements `Member`. – Jorn Jun 26 '23 at 12:11
  • @AndriyMykhaylyk you should not only look at syntax but also semantics. Yes, `Class` has all the methods, but it doesn't follow the _specification_ for `getDeclaringClass`. See my answer for why not. – Rob Spoor Jun 26 '23 at 12:12
0

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.

Rob Spoor
  • 6,186
  • 1
  • 19
  • 20