JVM spec: grammar for generic signatures requires lookahead

Viewed 150

In the spec for generic signatures, ClassSignature has the form

ClassSignature:
  [TypeParameters] SuperclassSignature {SuperinterfaceSignature}
TypeParameters:
  < TypeParameter {TypeParameter} >
TypeParameter:
  Identifier ClassBound {InterfaceBound}
ClassBound:
  : [ReferenceTypeSignature]
InterfaceBound:
  : ReferenceTypeSignature

So the superclass bound of a type parameter can be omitted (some examples here).

If I have a class declaration public class A<T, LFooBar>, the Java compiler generates the signature <T:Ljava/lang/Object;LFooBar:Ljava/lang/Object;>Ljava/lang/Object;.

IUC, the class bound could be omitted, in which case the signature would be <T:LFooBar:>Ljava/lang/Object;.

Parsing that short signature requires looking ahead to the second : in order to know that T:LFooBar: are two type parameters, and not one type parameter T with class bound FooBar.

Maybe in practice, leaving away the class bound is only done if there's an interface bound? For public class A<T extends Comparable<? super T>>, javac produces the signature <T::Ljava/lang/Comparable<-TT;>;>Ljava/lang/Object;. But I guess i cannot rely on this assumption.

Did I misunderstand something?

1 Answers
Related