RFR: 8266027: The diamond finder does not find diamond candidates in field initializers

Vicente Romero vromero at openjdk.java.net
Tue Apr 27 16:04:37 UTC 2021


On Tue, 27 Apr 2021 09:13:46 GMT, Jan Lahoda <jlahoda 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?

-------------

PR: https://git.openjdk.java.net/jdk/pull/3713


More information about the compiler-dev mailing list