Allowed names for receiver parameters

I Al Istannen java-ml at ialistannen.de
Thu Feb 1 18:56:27 UTC 2024


Hey,

we recently looked at the handling of receiver parameters in Java and 
did not quite understand some of the spec. As far as we can tell, Javac 
and the spec do not quite agree on the allowed names, which can be seen 
in the example[0].
The first comment before the constructor and method quotes JLS §8.4, the 
rest explain why we think the names should be rejected by Javac. Does 
Javac accept more than specified here or did we miss something?

IntelliJ accepts the code and models it in the PSI tree as a 
"PsiThisExpression", but ignores it completely in the documentation popup.
JDT seems to directly normalise the parameter name to "this", 
unqualifying it in the process.

Have a nice day :)
I Al Istannen

[0] Example:
```java
public class Outer {
     public class Middle {
         class Inner {
             //  In an inner class's constructor [..] and the name of the
             //  receiver parameter must be "Identifier.this" where 
"Identifier"
             //  is the simple name of the class or interface which is the
             //  immediately enclosing type declaration of the inner class;
             //  otherwise, a compile-time error occurs
             public Inner(Middle Middle.this) {}
             // here "Identifier" is "Outer.Middle", which is *not* the 
simple
             // name, but still accepted
             public Inner(Outer.Middle Outer.Middle.this, int a) {}


             // In an instance method, the type of the receiver 
parameter must
             // be the class or interface in which the method is 
declared, and
             // the name of the receiver parameter must be 'this'; 
otherwise, a
             // compile-time error occurs.
             public void inner1(Inner this) {}

             // Here the name is "Outer.Middle.Inner.this" and not 
"this", but
             // still accepted
             public void inner2(Inner Outer.Middle.Inner.this) {}
             // Here the name is "Middle.Inner.this" and not "this", but 
still
             // accepted
             public void inner3(Inner Middle.Inner.this) {}
             // Here the name is "Inner.this" and not "this", but still 
accepted
             public void inner4(Inner Inner.this) {}
         }
     }
}
```



More information about the compiler-dev mailing list