Not like it was impossible to turn these variables into fields that can be checked with Reflection. For example, you can do
var l = new ArrayList<String>(); l.add("text"); System.out.println(l); System.out.println( new Object(){ { var x = l; } }.getClass().getDeclaredFields()[0].getGenericType() );
In the current version, it simply prints an ArrayList , so the actual generic type was not stored in the class file of the anonymous inner class, and it is unlikely that this will change, since support for this introspection is not the actual target. This is also a special case when the type is denoted as ArrayList<String> . To illustrate another case:
var acs = true? new StringBuilder(): CharBuffer.allocate(10); acs.append("text"); acs.subSequence(1, 2); System.out.println( new Object(){ { var x = acs; } }.getClass().getDeclaredFields()[0].getGenericType() );
The acs type is the intersection type of Appendable and CharSequence , as shown by invoking a method on any interface on it, but since it is not specified whether the compiler #1 extends Appendable&CharSequence or #1 extends CharSequence&Appendable , it is not specified whether the code will print java.lang.Appendable or java.lang.CharSequence .
I do not think that this is a problem for a synthetic field, but for an explicitly declared field it could be.
However, I doubt that the expert group considered such consequences in detail. Instead, a decision that does not support field declarations (and therefore, skip a long thought about the consequences) was made from the very beginning, since local variables were always the intended purpose for this function. The number of local variables is much higher than the number of field declarations, so the template reduction for local variables has the greatest impact.
Holger
source share