src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java
author mcimadamore
Mon, 04 Dec 2017 17:54:49 +0000
changeset 48054 702043a4cdeb
parent 47957 7175a92b6fd7
child 48932 9e3f2ec326ba
permissions -rw-r--r--
8189749: Devise strategy for making source level checks more uniform Summary: Create a 'feature' enum which is responsible for handling source version checks and related diagnostic generation Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     1
/*
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 34474
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     4
 *
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    10
 *
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    16
 *
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    20
 *
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    23
 * questions.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    24
 */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    25
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    27
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    28
import java.util.ArrayList;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    29
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    30
import com.sun.source.tree.LambdaExpressionTree;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.javac.code.Source;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
    32
import com.sun.tools.javac.code.Source.Feature;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.Type;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.code.Types;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
    35
import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
    36
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCBlock;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    42
import com.sun.tools.javac.tree.JCTree.JCForLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    43
import com.sun.tools.javac.tree.JCTree.JCIf;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    44
import com.sun.tools.javac.tree.JCTree.JCLambda;
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    45
import com.sun.tools.javac.tree.JCTree.JCLambda.ParameterKind;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    46
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    47
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.JCNewClass;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCStatement;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCSwitch;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    51
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    52
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    53
import com.sun.tools.javac.tree.JCTree.JCWhileLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    54
import com.sun.tools.javac.tree.JCTree.Tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    55
import com.sun.tools.javac.tree.TreeCopier;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    56
import com.sun.tools.javac.tree.TreeInfo;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    57
import com.sun.tools.javac.tree.TreeMaker;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    58
import com.sun.tools.javac.tree.TreeScanner;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    59
import com.sun.tools.javac.util.Context;
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    60
import com.sun.tools.javac.util.DefinedBy;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    61
import com.sun.tools.javac.util.DefinedBy.Api;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    62
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    63
import com.sun.tools.javac.util.List;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    64
import com.sun.tools.javac.util.ListBuffer;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    65
import com.sun.tools.javac.util.Log;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    66
import com.sun.tools.javac.util.Options;
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
    67
import com.sun.tools.javac.util.Position;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    68
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    69
import java.util.EnumSet;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    70
import java.util.HashMap;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    71
import java.util.Map;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    72
import java.util.function.Predicate;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    73
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    74
import com.sun.source.tree.NewClassTree;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    75
import com.sun.tools.javac.code.Flags;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    76
import com.sun.tools.javac.code.Kinds.Kind;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    77
import com.sun.tools.javac.code.Symbol.ClassSymbol;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    78
import com.sun.tools.javac.tree.JCTree.JCTry;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    79
import com.sun.tools.javac.tree.JCTree.JCUnary;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    80
import com.sun.tools.javac.util.Assert;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    81
import com.sun.tools.javac.util.DiagnosticSource;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    82
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    83
import static com.sun.tools.javac.code.Flags.GENERATEDCONSTR;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    84
import static com.sun.tools.javac.code.TypeTag.CLASS;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    85
import static com.sun.tools.javac.tree.JCTree.Tag.APPLY;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    86
import static com.sun.tools.javac.tree.JCTree.Tag.FOREACHLOOP;
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    87
import static com.sun.tools.javac.tree.JCTree.Tag.LABELLED;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    88
import static com.sun.tools.javac.tree.JCTree.Tag.METHODDEF;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    89
import static com.sun.tools.javac.tree.JCTree.Tag.NEWCLASS;
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    90
import static com.sun.tools.javac.tree.JCTree.Tag.NULLCHK;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    91
import static com.sun.tools.javac.tree.JCTree.Tag.TYPEAPPLY;
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    92
import static com.sun.tools.javac.tree.JCTree.Tag.VARDEF;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    93
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    94
/**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    95
 * Helper class for defining custom code analysis, such as finding instance creation expression
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    96
 * that can benefit from diamond syntax.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    97
 */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    98
public class Analyzer {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    99
    protected static final Context.Key<Analyzer> analyzerKey = new Context.Key<>();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   100
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   101
    final Types types;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   102
    final Log log;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   103
    final Attr attr;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   104
    final DeferredAttr deferredAttr;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
   105
    final ArgumentAttr argumentAttr;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   106
    final TreeMaker make;
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   107
    final AnalyzerCopier copier;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 28146
diff changeset
   108
    private final boolean allowDiamondWithAnonymousClassCreation;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   109
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   110
    final EnumSet<AnalyzerMode> analyzerModes;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   111
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   112
    public static Analyzer instance(Context context) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   113
        Analyzer instance = context.get(analyzerKey);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   114
        if (instance == null)
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   115
            instance = new Analyzer(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   116
        return instance;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   117
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   118
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   119
    protected Analyzer(Context context) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   120
        context.put(analyzerKey, this);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   121
        types = Types.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   122
        log = Log.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   123
        attr = Attr.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   124
        deferredAttr = DeferredAttr.instance(context);
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
   125
        argumentAttr = ArgumentAttr.instance(context);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   126
        make = TreeMaker.instance(context);
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   127
        copier = new AnalyzerCopier();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   128
        Options options = Options.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   129
        String findOpt = options.get("find");
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   130
        //parse modes
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   131
        Source source = Source.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   132
        allowDiamondWithAnonymousClassCreation = Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   133
        analyzerModes = AnalyzerMode.getAnalyzerModes(findOpt, source);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   134
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   135
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   136
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   137
     * This enum defines supported analyzer modes, as well as defining the logic for decoding
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   138
     * the {@code -XDfind} option.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   139
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   140
    enum AnalyzerMode {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   141
        DIAMOND("diamond", Feature.DIAMOND),
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   142
        LAMBDA("lambda", Feature.LAMBDA),
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   143
        METHOD("method", Feature.GRAPH_INFERENCE),
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   144
        LOCAL("local", Feature.LOCAL_VARIABLE_TYPE_INFERENCE);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   145
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   146
        final String opt;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   147
        final Feature feature;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   148
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   149
        AnalyzerMode(String opt, Feature feature) {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   150
            this.opt = opt;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   151
            this.feature = feature;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   152
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   153
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   154
        /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   155
         * This method is used to parse the {@code find} option.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   156
         * Possible modes are separated by colon; a mode can be excluded by
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   157
         * prepending '-' to its name. Finally, the special mode 'all' can be used to
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   158
         * add all modes to the resulting enum.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   159
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   160
        static EnumSet<AnalyzerMode> getAnalyzerModes(String opt, Source source) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   161
            if (opt == null) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   162
                return EnumSet.noneOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   163
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   164
            List<String> modes = List.from(opt.split(","));
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   165
            EnumSet<AnalyzerMode> res = EnumSet.noneOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   166
            if (modes.contains("all")) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   167
                res = EnumSet.allOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   168
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   169
            for (AnalyzerMode mode : values()) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   170
                if (modes.contains(mode.opt)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   171
                    res.add(mode);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47957
diff changeset
   172
                } else if (modes.contains("-" + mode.opt) || !mode.feature.allowedInSource(source)) {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   173
                    res.remove(mode);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   174
                }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   175
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   176
            return res;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   177
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   178
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   179
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   180
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   181
     * A statement analyzer is a work-unit that matches certain AST nodes (of given type {@code S}),
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   182
     * rewrites them to different AST nodes (of type {@code T}) and then generates some meaningful
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   183
     * messages in case the analysis has been successful.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   184
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   185
    abstract class StatementAnalyzer<S extends JCTree, T extends JCTree> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   186
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   187
        AnalyzerMode mode;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   188
        JCTree.Tag tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   189
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   190
        StatementAnalyzer(AnalyzerMode mode, Tag tag) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   191
            this.mode = mode;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   192
            this.tag = tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   193
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   194
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   195
        /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   196
         * Is this analyzer allowed to run?
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   197
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   198
        boolean isEnabled() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   199
            return analyzerModes.contains(mode);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   200
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   201
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   202
        /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   203
         * Should this analyzer be rewriting the given tree?
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   204
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   205
        abstract boolean match(S tree);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   206
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   207
        /**
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   208
         * Rewrite a given AST node into a new one(s)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   209
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   210
        abstract List<T> rewrite(S oldTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   211
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   212
        /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   213
         * Entry-point for comparing results and generating diagnostics.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   214
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   215
        abstract void process(S oldTree, T newTree, boolean hasErrors);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   216
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   217
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   218
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   219
     * This analyzer checks if generic instance creation expression can use diamond syntax.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   220
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   221
    class DiamondInitializer extends StatementAnalyzer<JCNewClass, JCNewClass> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   222
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   223
        DiamondInitializer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   224
            super(AnalyzerMode.DIAMOND, NEWCLASS);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   225
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   226
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   227
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   228
        boolean match(JCNewClass tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   229
            return tree.clazz.hasTag(TYPEAPPLY) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   230
                    !TreeInfo.isDiamond(tree) &&
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 28146
diff changeset
   231
                    (tree.def == null || allowDiamondWithAnonymousClassCreation);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   232
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   233
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   234
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   235
        List<JCNewClass> rewrite(JCNewClass oldTree) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   236
            if (oldTree.clazz.hasTag(TYPEAPPLY)) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   237
                JCNewClass nc = copier.copy(oldTree);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   238
                ((JCTypeApply)nc.clazz).arguments = List.nil();
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   239
                return List.of(nc);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   240
            } else {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   241
                return List.of(oldTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   242
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   243
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   244
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   245
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   246
        void process(JCNewClass oldTree, JCNewClass newTree, boolean hasErrors) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   247
            if (!hasErrors) {
30404
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   248
                List<Type> inferredArgs, explicitArgs;
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   249
                if (oldTree.def != null) {
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   250
                    inferredArgs = newTree.def.implementing.nonEmpty()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   251
                                      ? newTree.def.implementing.get(0).type.getTypeArguments()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   252
                                      : newTree.def.extending.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   253
                    explicitArgs = oldTree.def.implementing.nonEmpty()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   254
                                      ? oldTree.def.implementing.get(0).type.getTypeArguments()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   255
                                      : oldTree.def.extending.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   256
                } else {
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   257
                    inferredArgs = newTree.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   258
                    explicitArgs = oldTree.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   259
                }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   260
                for (Type t : inferredArgs) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   261
                    if (!types.isSameType(t, explicitArgs.head)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   262
                        return;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   263
                    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   264
                    explicitArgs = explicitArgs.tail;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   265
                }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   266
                //exact match
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   267
                log.warning(oldTree.clazz, Warnings.DiamondRedundantArgs);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   268
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   269
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   270
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   271
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   272
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   273
     * This analyzer checks if anonymous instance creation expression can replaced by lambda.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   274
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   275
    class LambdaAnalyzer extends StatementAnalyzer<JCNewClass, JCLambda> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   276
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   277
        LambdaAnalyzer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   278
            super(AnalyzerMode.LAMBDA, NEWCLASS);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   279
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   280
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   281
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   282
        boolean match (JCNewClass tree){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   283
            Type clazztype = tree.clazz.type;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   284
            return tree.def != null &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   285
                    clazztype.hasTag(CLASS) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   286
                    types.isFunctionalInterface(clazztype.tsym) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   287
                    decls(tree.def).length() == 1;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   288
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   289
        //where
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   290
            private List<JCTree> decls(JCClassDecl decl) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   291
                ListBuffer<JCTree> decls = new ListBuffer<>();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   292
                for (JCTree t : decl.defs) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   293
                    if (t.hasTag(METHODDEF)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   294
                        JCMethodDecl md = (JCMethodDecl)t;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   295
                        if ((md.getModifiers().flags & GENERATEDCONSTR) == 0) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   296
                            decls.add(md);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   297
                        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   298
                    } else {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   299
                        decls.add(t);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   300
                    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   301
                }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   302
                return decls.toList();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   303
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   304
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   305
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   306
        List<JCLambda> rewrite(JCNewClass oldTree){
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   307
            JCMethodDecl md = (JCMethodDecl)decls(oldTree.def).head;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   308
            List<JCVariableDecl> params = md.params;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   309
            JCBlock body = md.body;
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   310
            JCLambda newTree = make.at(oldTree).Lambda(params, body);
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   311
            return List.of(newTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   312
        }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   313
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   314
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   315
        void process (JCNewClass oldTree, JCLambda newTree, boolean hasErrors){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   316
            if (!hasErrors) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   317
                log.warning(oldTree.def, Warnings.PotentialLambdaFound);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   318
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   319
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   320
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   321
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   322
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   323
     * This analyzer checks if generic method call has redundant type arguments.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   324
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   325
    class RedundantTypeArgAnalyzer extends StatementAnalyzer<JCMethodInvocation, JCMethodInvocation> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   326
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   327
        RedundantTypeArgAnalyzer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   328
            super(AnalyzerMode.METHOD, APPLY);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   329
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   330
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   331
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   332
        boolean match (JCMethodInvocation tree){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   333
            return tree.typeargs != null &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   334
                    tree.typeargs.nonEmpty();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   335
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   336
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   337
        List<JCMethodInvocation> rewrite(JCMethodInvocation oldTree){
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   338
            JCMethodInvocation app = copier.copy(oldTree);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   339
            app.typeargs = List.nil();
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   340
            return List.of(app);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   341
        }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   342
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   343
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   344
        void process (JCMethodInvocation oldTree, JCMethodInvocation newTree, boolean hasErrors){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   345
            if (!hasErrors) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   346
                //exact match
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   347
                log.warning(oldTree, Warnings.MethodRedundantTypeargs);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   348
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   349
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   350
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   351
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   352
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   353
     * Base class for local variable inference analyzers.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   354
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   355
    abstract class RedundantLocalVarTypeAnalyzerBase<X extends JCStatement> extends StatementAnalyzer<X, X> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   356
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   357
        RedundantLocalVarTypeAnalyzerBase(JCTree.Tag tag) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   358
            super(AnalyzerMode.LOCAL, tag);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   359
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   360
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   361
        /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   362
         * Map a variable tree into a new declaration using implicit type.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   363
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   364
        JCVariableDecl rewriteVarType(JCVariableDecl oldTree) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   365
            JCVariableDecl newTree = copier.copy(oldTree);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   366
            newTree.vartype = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   367
            return newTree;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   368
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   369
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   370
        /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   371
         * Analyze results of local variable inference.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   372
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   373
        void processVar(JCVariableDecl oldTree, JCVariableDecl newTree, boolean hasErrors) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   374
            if (!hasErrors) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   375
                if (types.isSameType(oldTree.type, newTree.type)) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   376
                    log.warning(oldTree, Warnings.LocalRedundantType);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   377
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   378
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   379
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   380
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   381
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   382
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   383
     * This analyzer checks if a local variable declaration has redundant type.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   384
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   385
    class RedundantLocalVarTypeAnalyzer extends RedundantLocalVarTypeAnalyzerBase<JCVariableDecl> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   386
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   387
        RedundantLocalVarTypeAnalyzer() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   388
            super(VARDEF);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   389
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   390
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   391
        boolean match(JCVariableDecl tree){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   392
            return tree.sym.owner.kind == Kind.MTH &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   393
                    tree.init != null && !tree.isImplicitlyTyped() &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   394
                    attr.canInferLocalVarType(tree) == null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   395
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   396
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   397
        List<JCVariableDecl> rewrite(JCVariableDecl oldTree) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   398
            return List.of(rewriteVarType(oldTree));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   399
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   400
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   401
        void process(JCVariableDecl oldTree, JCVariableDecl newTree, boolean hasErrors){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   402
            processVar(oldTree, newTree, hasErrors);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   403
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   404
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   405
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   406
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   407
     * This analyzer checks if a for each variable declaration has redundant type.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   408
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   409
    class RedundantLocalVarTypeAnalyzerForEach extends RedundantLocalVarTypeAnalyzerBase<JCEnhancedForLoop> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   410
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   411
        RedundantLocalVarTypeAnalyzerForEach() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   412
            super(FOREACHLOOP);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   413
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   414
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   415
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   416
        boolean match(JCEnhancedForLoop tree){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   417
            return !tree.var.isImplicitlyTyped();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   418
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   419
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   420
        List<JCEnhancedForLoop> rewrite(JCEnhancedForLoop oldTree) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   421
            JCEnhancedForLoop newTree = copier.copy(oldTree);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   422
            newTree.var = rewriteVarType(oldTree.var);
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   423
            newTree.body = make.at(oldTree.body).Block(0, List.nil());
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   424
            return List.of(newTree);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   425
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   426
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   427
        void process(JCEnhancedForLoop oldTree, JCEnhancedForLoop newTree, boolean hasErrors){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   428
            processVar(oldTree.var, newTree.var, hasErrors);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   429
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   430
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   431
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   432
    @SuppressWarnings({"unchecked", "rawtypes"})
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   433
    StatementAnalyzer<JCTree, JCTree>[] analyzers = new StatementAnalyzer[] {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   434
            new DiamondInitializer(),
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   435
            new LambdaAnalyzer(),
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   436
            new RedundantTypeArgAnalyzer(),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   437
            new RedundantLocalVarTypeAnalyzer(),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   438
            new RedundantLocalVarTypeAnalyzerForEach()
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   439
    };
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   440
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   441
    /**
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   442
     * Create a copy of Env if needed.
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   443
     */
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   444
    Env<AttrContext> copyEnvIfNeeded(JCTree tree, Env<AttrContext> env) {
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   445
        if (!analyzerModes.isEmpty() &&
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   446
                !env.info.isSpeculative &&
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   447
                TreeInfo.isStatement(tree) &&
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   448
                !tree.hasTag(LABELLED)) {
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   449
            Env<AttrContext> analyzeEnv =
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   450
                    env.dup(env.tree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   451
            analyzeEnv.info.returnResult = analyzeEnv.info.returnResult != null ?
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   452
                    attr.new ResultInfo(analyzeEnv.info.returnResult.pkind,
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   453
                                        analyzeEnv.info.returnResult.pt) : null;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   454
            return analyzeEnv;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   455
        } else {
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   456
            return null;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   457
        }
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   458
    }
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   459
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   460
    /**
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   461
     * Analyze an AST node if needed.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   462
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   463
    void analyzeIfNeeded(JCTree tree, Env<AttrContext> env) {
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   464
        if (env != null) {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   465
            JCStatement stmt = (JCStatement)tree;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   466
            analyze(stmt, env);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   467
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   468
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   469
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   470
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   471
     * Analyze an AST node; this involves collecting a list of all the nodes that needs rewriting,
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   472
     * and speculatively type-check the rewritten code to compare results against previously attributed code.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   473
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   474
    void analyze(JCStatement statement, Env<AttrContext> env) {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   475
        StatementScanner statementScanner = new StatementScanner(statement, env);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   476
        statementScanner.scan();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   477
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   478
        if (!statementScanner.rewritings.isEmpty()) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   479
            for (RewritingContext rewriting : statementScanner.rewritings) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   480
                deferredAnalysisHelper.queue(rewriting);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   481
            }
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   482
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   483
    }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   484
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   485
    /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   486
     * Helper interface to handle deferral of analysis tasks.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   487
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   488
    interface DeferredAnalysisHelper {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   489
        /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   490
         * Add a new analysis task to the queue.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   491
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   492
        void queue(RewritingContext rewriting);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   493
        /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   494
         * Flush queue with given attribution env.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   495
         */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   496
        void flush(Env<AttrContext> flushEnv);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   497
    }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   498
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   499
    /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   500
     * Dummy deferral handler.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   501
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   502
    DeferredAnalysisHelper flushDeferredHelper = new DeferredAnalysisHelper() {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   503
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   504
        public void queue(RewritingContext rewriting) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   505
            //do nothing
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   506
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   507
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   508
        @Override
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   509
        public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   510
            //do nothing
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   511
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   512
    };
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   513
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   514
    /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   515
     * Simple deferral handler. All tasks belonging to the same outermost class are added to
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   516
     * the same queue. The queue is flushed after flow analysis (only if no error occurred).
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   517
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   518
    DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   519
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   520
        Map<ClassSymbol, ArrayList<RewritingContext>> Q = new HashMap<>();
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   521
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   522
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   523
        public void queue(RewritingContext rewriting) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   524
            ArrayList<RewritingContext> s = Q.computeIfAbsent(rewriting.env.enclClass.sym.outermostClass(), k -> new ArrayList<>());
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   525
            s.add(rewriting);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   526
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   527
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   528
        @Override
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   529
        public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   530
            if (!Q.isEmpty()) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   531
                DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   532
                try {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   533
                    deferredAnalysisHelper = flushDeferredHelper;
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   534
                    ArrayList<RewritingContext> rewritings = Q.get(flushEnv.enclClass.sym.outermostClass());
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   535
                    while (rewritings != null && !rewritings.isEmpty()) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   536
                        doAnalysis(rewritings.remove(0));
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   537
                    }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   538
                } finally {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   539
                    deferredAnalysisHelper = prevHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   540
                }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   541
            }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   542
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   543
    };
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   544
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   545
    DeferredAnalysisHelper deferredAnalysisHelper = queueDeferredHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   546
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   547
    void doAnalysis(RewritingContext rewriting) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   548
        DiagnosticSource prevSource = log.currentSource();
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   549
        LocalCacheContext localCacheContext = argumentAttr.withLocalCacheContext();
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   550
        try {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   551
            log.useSource(rewriting.env.toplevel.getSourceFile());
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   552
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   553
            JCStatement treeToAnalyze = (JCStatement)rewriting.originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   554
            if (rewriting.env.info.scope.owner.kind == Kind.TYP) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   555
                //add a block to hoist potential dangling variable declarations
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   556
                treeToAnalyze = make.at(Position.NOPOS)
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   557
                                    .Block(Flags.SYNTHETIC, List.of((JCStatement)rewriting.originalTree));
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   558
            }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   559
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   560
            //TODO: to further refine the analysis, try all rewriting combinations
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   561
            deferredAttr.attribSpeculative(treeToAnalyze, rewriting.env, attr.statInfo, new TreeRewriter(rewriting),
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   562
                    t -> rewriting.diagHandler(), argumentAttr.withLocalCacheContext());
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   563
            rewriting.analyzer.process(rewriting.oldTree, rewriting.replacement, rewriting.erroneous);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   564
        } catch (Throwable ex) {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   565
            Assert.error("Analyzer error when processing: " + rewriting.originalTree);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   566
        } finally {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   567
            log.useSource(prevSource.getFile());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   568
            localCacheContext.leave();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   569
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   570
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   571
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   572
    public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   573
        deferredAnalysisHelper.flush(flushEnv);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   574
    }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   575
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   576
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   577
     * Subclass of {@link com.sun.tools.javac.tree.TreeScanner} which visit AST-nodes w/o crossing
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   578
     * statement boundaries.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   579
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   580
    class StatementScanner extends TreeScanner {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   581
        /** Tree rewritings (generated by analyzers). */
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   582
        ListBuffer<RewritingContext> rewritings = new ListBuffer<>();
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   583
        JCTree originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   584
        Env<AttrContext> env;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   585
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   586
        StatementScanner(JCTree originalTree, Env<AttrContext> env) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   587
            this.originalTree = originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   588
            this.env = attr.copyEnv(env);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   589
        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   590
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   591
        public void scan() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   592
            scan(originalTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   593
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   594
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   595
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   596
        @SuppressWarnings("unchecked")
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   597
        public void scan(JCTree tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   598
            if (tree != null) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   599
                for (StatementAnalyzer<JCTree, JCTree> analyzer : analyzers) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   600
                    if (analyzer.isEnabled() &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   601
                            tree.hasTag(analyzer.tag) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   602
                            analyzer.match(tree)) {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   603
                        for (JCTree t : analyzer.rewrite(tree)) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   604
                            rewritings.add(new RewritingContext(originalTree, tree, t, analyzer, env));
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   605
                        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   606
                        break; //TODO: cover cases where multiple matching analyzers are found
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   607
                    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   608
                }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   609
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   610
            super.scan(tree);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   611
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   612
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   613
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   614
        public void visitClassDef(JCClassDecl tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   615
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   616
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   617
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   618
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   619
        public void visitMethodDef(JCMethodDecl tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   620
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   621
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   622
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   623
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   624
        public void visitBlock(JCBlock tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   625
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   626
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   627
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   628
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   629
        public void visitSwitch(JCSwitch tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   630
            scan(tree.getExpression());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   631
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   632
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   633
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   634
        public void visitForLoop(JCForLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   635
            //skip body and var decl (to prevents same statements to be analyzed twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   636
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   637
            scan(tree.getUpdate());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   638
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   639
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   640
        @Override
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   641
        public void visitTry(JCTry tree) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   642
            //skip resources (to prevents same statements to be analyzed twice)
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   643
            scan(tree.getBlock());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   644
            scan(tree.getCatches());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   645
            scan(tree.getFinallyBlock());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   646
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   647
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   648
        @Override
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   649
        public void visitForeachLoop(JCEnhancedForLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   650
            //skip body (to prevents same statements to be analyzed twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   651
            scan(tree.getExpression());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   652
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   653
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   654
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   655
        public void visitWhileLoop(JCWhileLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   656
            //skip body (to prevents same statements to be analyzed twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   657
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   658
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   659
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   660
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   661
        public void visitDoLoop(JCDoWhileLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   662
            //skip body (to prevents same statements to be analyzed twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   663
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   664
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   665
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   666
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   667
        public void visitIf(JCIf tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   668
            //skip body (to prevents same statements to be analyzed twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   669
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   670
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   671
    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   672
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   673
    class RewritingContext {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   674
        // the whole tree being analyzed
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   675
        JCTree originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   676
        // a subtree, old tree, that will be rewritten
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   677
        JCTree oldTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   678
        // the replacement for the old tree
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   679
        JCTree replacement;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   680
        // did the compiler find any error
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   681
        boolean erroneous;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   682
        // the env
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   683
        Env<AttrContext> env;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   684
        // the corresponding analyzer
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   685
        StatementAnalyzer<JCTree, JCTree> analyzer;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   686
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   687
        RewritingContext(
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   688
                JCTree originalTree,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   689
                JCTree oldTree,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   690
                JCTree replacement,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   691
                StatementAnalyzer<JCTree, JCTree> analyzer,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   692
                Env<AttrContext> env) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   693
            this.originalTree = originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   694
            this.oldTree = oldTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   695
            this.replacement = replacement;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   696
            this.analyzer = analyzer;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   697
            this.env = attr.copyEnv(env);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   698
            /*  this is a temporary workaround that should be removed once we have a truly independent
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   699
             *  clone operation
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   700
             */
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   701
            if (originalTree.hasTag(VARDEF)) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   702
                // avoid redefinition clashes
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   703
                this.env.info.scope.remove(((JCVariableDecl)originalTree).sym);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   704
            }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   705
        }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   706
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   707
        /**
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   708
         * Simple deferred diagnostic handler which filters out all messages and keep track of errors.
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   709
         */
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   710
        Log.DeferredDiagnosticHandler diagHandler() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   711
            return new Log.DeferredDiagnosticHandler(log, d -> {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   712
                if (d.getType() == DiagnosticType.ERROR) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   713
                    erroneous = true;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   714
                }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   715
                return true;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   716
            });
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   717
        }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   718
    }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   719
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   720
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   721
     * Subclass of TreeCopier that maps nodes matched by analyzers onto new AST nodes.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   722
     */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   723
    class AnalyzerCopier extends TreeCopier<Void> {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   724
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   725
        public AnalyzerCopier() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   726
            super(make);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   727
        }
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   728
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   729
        @Override @DefinedBy(Api.COMPILER_TREE)
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   730
        public JCTree visitLambdaExpression(LambdaExpressionTree node, Void _unused) {
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   731
            JCLambda oldLambda = (JCLambda)node;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   732
            JCLambda newLambda = (JCLambda)super.visitLambdaExpression(node, _unused);
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   733
            if (oldLambda.paramKind == ParameterKind.IMPLICIT) {
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   734
                //reset implicit lambda parameters (whose type might have been set during attr)
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   735
                newLambda.paramKind = ParameterKind.IMPLICIT;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   736
                newLambda.params.forEach(p -> p.vartype = null);
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   737
            }
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   738
            return newLambda;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   739
        }
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   740
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   741
        @Override @DefinedBy(Api.COMPILER_TREE)
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   742
        public JCTree visitNewClass(NewClassTree node, Void aVoid) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   743
            JCNewClass oldNewClazz = (JCNewClass)node;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   744
            JCNewClass newNewClazz = (JCNewClass)super.visitNewClass(node, aVoid);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   745
            if (!oldNewClazz.args.isEmpty() && oldNewClazz.args.head.hasTag(NULLCHK)) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   746
                //workaround to Attr generating trees
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   747
                newNewClazz.encl = ((JCUnary)newNewClazz.args.head).arg;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   748
                newNewClazz.args = newNewClazz.args.tail;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   749
            }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   750
            return newNewClazz;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   751
        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   752
    }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   753
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   754
   class TreeRewriter extends AnalyzerCopier {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   755
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   756
        RewritingContext rewriting;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   757
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   758
        TreeRewriter(RewritingContext rewriting) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   759
            this.rewriting = rewriting;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   760
        }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   761
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   762
        @Override
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   763
        @SuppressWarnings("unchecked")
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   764
        public <Z extends JCTree> Z copy(Z tree, Void _unused) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   765
            Z newTree = super.copy(tree, null);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   766
            if (tree != null && tree == rewriting.oldTree) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   767
                Assert.checkNonNull(rewriting.replacement);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   768
                newTree = (Z)rewriting.replacement;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   769
            }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   770
            return newTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   771
        }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   772
    }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   773
}