Interim JDK 10 RFR of 8187951: Update javax.lang.model.SourceVersion for "var" name

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Sep 28 08:35:13 UTC 2017


Not 100% sure about this. On the one hand, the concept of contextual 
keyword is, well, associated with a context. So 'open' is a keyword in a 
module-info file, and 'var' is a keyword when used in a local variable 
declaration.

But the isName method takes no such context, so it's bound to be lossy.

You tried to reformulate the semantics of isName as something similar to 
'is this a valid name for a javax.lang.model.element.Element instance'? 
I think that _almost_ works - after all elements model:

* variables/fields (VariableElement)
* methods (ExecutableElement)
* classes/interfaces/enums/annotations (TypeElement)
* typevars (TypeParameterElement)
* modules (ModuleElement)
* packages (PackageElement)

now, in the last two cases, I'm not sure the implemented logic works 
well. While 'var' is not a legal name for 
variables/methods/types/typevars, I think it is a valid name for 
packages and modules, there's no restriction there.

This unfortunately makes the definition of isName messy, because it 
doesn't really refer to _all_ elements, but just to a subset of them.

Also, I find this text:

"and {@code "var"} is used for local variable type inference inthe 
argument version."

I only got a good idea of what you meant by looking at the implementation.

One alternative would be to mark this method as @Deprecated, and add a 
new method:

public static boolean isName(CharSequence name, *ElementKind kind*, 
SourceVersion version) { ... }

Where the semantics could be:

is _name_ a valid qualified name for an element of kind _kind_ in 
version _version_ ?

While not perfect (it still cannot handle module-info keyword, as 
there's no info on which compilation unit this question refers to), I 
think it's a step forward in that it lets you discriminate between 
different kind of elements.

An even more complete variant would be:

public static boolean isName(CharSequence name, *JavaFileObject 
sourceFile, ElementKind kind*, SourceVersion version) { ... }

Which would let you express in full what you need to express, for both 
'var' and module keywords.

Maurizio

On 27/09/17 23:51, joe darcy wrote:
> Hello,
>
> Please review the in-progress work on
>
>     8187951: Update javax.lang.model.SourceVersion for "var" name
>     http://cr.openjdk.java.net/~darcy/8187951.0/
>
> I'm not 100% certain of the desired semantics here. The handling of 
> "var" doesn't align with any of the existing syntactic constructs, 
> including restricted keywords introduced in 9 with modules.
>
> The proposal changes gives the "isName" method the semantics of "can I 
> use this qualified name for a type, field, package, or module."
>
> Thanks,
>
> -Joe
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20170928/6d19cbcb/attachment.html>


More information about the compiler-dev mailing list