Laird Nelson
Laird Nelson

Reputation: 16238

Why does directSupertypes(TypeMirror) return an empty List when given a primitive type of int?

If I invoke javax.lang.model.util.Types#directSupertypes(TypeMirror) on a TypeMirror representing int, the resulting List is empty.

The Java Language Specification says that the direct supertype of int is long. Consequently since the method is documented to return the direct supertypes of the supplied TypeMirror I would have expected a List consisting of one element (a PrimitiveType representing long).

Upvotes: 0

Views: 39

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 103637

Just like your previous question about type system pedantics, there isn't an interesting answer to this.

For the purposes of method resolution, int is a subtype of long, i.e. if you call foo(someInt) where someInt is an expression of type int, and the only method available is void foo(long x), then that will invoked. Flip the int and the long around and it wouldn't be.

The Java Language Specification says that the direct supertype of int is long.

No, the JLS says that the direct supertype of int is a long in the context of method resolution.

It doesn't actually say that. It's intended to convey this as 'that should be obvious'. As exhibit A to 'prove' that:... the behaviour of Types.directSupertypes.

The javadoc of directSupertypes directly refers to §4.10 of the JLS.

At some point 'ask on SO' is a weird move. If you find this pedantic disconnect offensive you should file bugs with the openJDK team. I've filed a bug about errors in specs before. It was fixed in the next version of the documentation.

Upvotes: 2

Related Questions