RFR: 8266027: The diamond finder does not find diamond candidates in field initializers
Jan Lahoda
jlahoda at openjdk.java.net
Tue Apr 27 19:52:08 UTC 2021
On Tue, 27 Apr 2021 16:01:13 GMT, Vicente Romero <vromero at openjdk.org> wrote:
>> Consider code like:
>>
>> public class Test {
>> private List<String> f2 = new LinkedList<String>();
>> }
>>
>>
>> Running with javac with `-XDfind=diamond` will not produce any warning about a possible diamond use. The reason is that the field:
>>
>> private List<String> f2 = new LinkedList<String>();
>>
>>
>> will get wrapped by a block, and attributed as a block, but since it has the `private` modifier, an error will be produced, and the transformation will be deemed impossible.
>>
>> The proposed solution is to clear the modifiers when/if wrapping fields with blocks.
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java line 801:
>
>> 799: //would be reported, causing the whole rewrite to fail. Removing the non-final
>> 800: //modifiers from the variable here:
>> 801: ((JCVariableDecl) result).mods.flags &= Flags.FINAL;
>
> just curious: why preserving the FINAL flag anyway?
No particular strong reason, just keeping what we can keep. We can clear the flags to `0`, if you prefer. I don't think that should cause issues (with the current set of Analyzers).
-------------
PR: https://git.openjdk.java.net/jdk/pull/3713
More information about the compiler-dev
mailing list