Skip to content
Advertisement

In Java 8 compiler, what is nameexpr field in JCVariableDecl for

In java compiler JCTree.java the JCVariableDecl is defined as the following:

JavaScript

However, I am not sure how nameexpr can be useful here. In what situation nameexpr field is not null? I have read the BNF of Java 8 and JavacParser.java

JavaScript

it seems that we need to construct a case to make

pn.hasTag(Tag.IDENT) && ((JCIdent)pn).name != names._this

to be false.

Advertisement

Answer

The code is checking for a receiver parameter, which looks like this:

JavaScript

In the grammar production rules of the spec, it is listed as:

JavaScript

It’s a parameter that has the name of “this”, or NameOfOuterClass.this if in an inner class constructor. this is not an identifier, so this is why it is checking for pn.hasTag(Tag.IDENT).

If you have not heard of receiver parameters before, it’s basically a “an optional syntactic device that exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated,” and “it is never bound to any value passed as an argument in a method invocation expression or qualified class instance creation expression, and it has no effect whatsoever at run time” as the language spec puts it.

Since it has almost the same structure as a variable declarator, I guess they decided to also use JCVariableDecl to represent it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement