src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java
author jlahoda
Wed, 29 Nov 2017 14:41:09 +0100
changeset 47957 7175a92b6fd7
parent 47465 bc25e62f4794
child 48054 702043a4cdeb
permissions -rw-r--r--
8191981: javac crash when detecting lambda candidates Summary: Ensuring proper positions on the testing AST nodes created by Analyzer. Reviewed-by: mcimadamore
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;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.code.Type;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.Types;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
    34
import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
    35
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.JCBlock;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.JCForLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    42
import com.sun.tools.javac.tree.JCTree.JCIf;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    43
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
    44
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
    45
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    46
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    47
import com.sun.tools.javac.tree.JCTree.JCNewClass;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.JCStatement;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCSwitch;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    51
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    52
import com.sun.tools.javac.tree.JCTree.JCWhileLoop;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    53
import com.sun.tools.javac.tree.JCTree.Tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    54
import com.sun.tools.javac.tree.TreeCopier;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    55
import com.sun.tools.javac.tree.TreeInfo;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    56
import com.sun.tools.javac.tree.TreeMaker;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    57
import com.sun.tools.javac.tree.TreeScanner;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    58
import com.sun.tools.javac.util.Context;
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    59
import com.sun.tools.javac.util.DefinedBy;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
    60
import com.sun.tools.javac.util.DefinedBy.Api;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    61
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    62
import com.sun.tools.javac.util.List;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    63
import com.sun.tools.javac.util.ListBuffer;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    64
import com.sun.tools.javac.util.Log;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    65
import com.sun.tools.javac.util.Options;
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
    66
import com.sun.tools.javac.util.Position;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    67
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    68
import java.util.EnumSet;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    69
import java.util.HashMap;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    70
import java.util.Map;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    71
import java.util.function.Predicate;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    72
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    73
import com.sun.source.tree.NewClassTree;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    74
import com.sun.tools.javac.code.Flags;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    75
import com.sun.tools.javac.code.Kinds.Kind;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    76
import com.sun.tools.javac.code.Symbol.ClassSymbol;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    77
import com.sun.tools.javac.tree.JCTree.JCTry;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    78
import com.sun.tools.javac.tree.JCTree.JCUnary;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    79
import com.sun.tools.javac.util.Assert;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    80
import com.sun.tools.javac.util.DiagnosticSource;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
    81
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    82
import static com.sun.tools.javac.code.Flags.GENERATEDCONSTR;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    83
import static com.sun.tools.javac.code.TypeTag.CLASS;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    84
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
    85
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
    86
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
    87
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
    88
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
    89
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
    90
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
    91
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
    92
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
 * 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
    95
 * that can benefit from diamond syntax.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    96
 */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    97
public class Analyzer {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
    98
    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
    99
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   100
    final Types types;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   101
    final Log log;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   102
    final Attr attr;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   103
    final DeferredAttr deferredAttr;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
   104
    final ArgumentAttr argumentAttr;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   105
    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
   106
    final AnalyzerCopier copier;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 28146
diff changeset
   107
    private final boolean allowDiamondWithAnonymousClassCreation;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   108
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   109
    final EnumSet<AnalyzerMode> analyzerModes;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   110
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   111
    public static Analyzer instance(Context context) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   112
        Analyzer instance = context.get(analyzerKey);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   113
        if (instance == null)
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   114
            instance = new Analyzer(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   115
        return instance;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   116
    }
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
    protected Analyzer(Context context) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   119
        context.put(analyzerKey, this);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   120
        types = Types.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   121
        log = Log.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   122
        attr = Attr.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   123
        deferredAttr = DeferredAttr.instance(context);
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 30404
diff changeset
   124
        argumentAttr = ArgumentAttr.instance(context);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   125
        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
   126
        copier = new AnalyzerCopier();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   127
        Options options = Options.instance(context);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   128
        String findOpt = options.get("find");
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   129
        //parse modes
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   130
        Source source = Source.instance(context);
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 28146
diff changeset
   131
        allowDiamondWithAnonymousClassCreation = source.allowDiamondWithAnonymousClassCreation();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   132
        analyzerModes = AnalyzerMode.getAnalyzerModes(findOpt, source);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   133
    }
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
     * 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
   137
     * the {@code -XDfind} option.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   138
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   139
    enum AnalyzerMode {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   140
        DIAMOND("diamond", Source::allowDiamond),
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   141
        LAMBDA("lambda", Source::allowLambda),
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   142
        METHOD("method", Source::allowGraphInference),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   143
        LOCAL("local", Source::allowLocalVariableTypeInference);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   144
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   145
        final String opt;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   146
        final Predicate<Source> sourceFilter;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   147
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   148
        AnalyzerMode(String opt, Predicate<Source> sourceFilter) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   149
            this.opt = opt;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   150
            this.sourceFilter = sourceFilter;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   151
        }
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
         * 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
   155
         * 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
   156
         * 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
   157
         * add all modes to the resulting enum.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   158
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   159
        static EnumSet<AnalyzerMode> getAnalyzerModes(String opt, Source source) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   160
            if (opt == null) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   161
                return EnumSet.noneOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   162
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   163
            List<String> modes = List.from(opt.split(","));
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   164
            EnumSet<AnalyzerMode> res = EnumSet.noneOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   165
            if (modes.contains("all")) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   166
                res = EnumSet.allOf(AnalyzerMode.class);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   167
            }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   168
            for (AnalyzerMode mode : values()) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   169
                if (modes.contains(mode.opt)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   170
                    res.add(mode);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   171
                } else if (modes.contains("-" + mode.opt) || !mode.sourceFilter.test(source)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   172
                    res.remove(mode);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   173
                }
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
            return res;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   176
        }
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
     * 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
   181
     * 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
   182
     * messages in case the analysis has been successful.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   183
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   184
    abstract class StatementAnalyzer<S extends JCTree, T extends JCTree> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   185
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   186
        AnalyzerMode mode;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   187
        JCTree.Tag tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   188
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   189
        StatementAnalyzer(AnalyzerMode mode, Tag tag) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   190
            this.mode = mode;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   191
            this.tag = tag;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   192
        }
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
         * Is this analyzer allowed to run?
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   196
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   197
        boolean isEnabled() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   198
            return analyzerModes.contains(mode);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   199
        }
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
         * Should this analyzer be rewriting the given tree?
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   203
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   204
        abstract boolean match(S tree);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   205
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   206
        /**
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   207
         * 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
   208
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   209
        abstract List<T> rewrite(S oldTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   210
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
         * Entry-point for comparing results and generating diagnostics.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   213
         */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   214
        abstract void process(S oldTree, T newTree, boolean hasErrors);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   215
    }
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
     * 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
   219
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   220
    class DiamondInitializer extends StatementAnalyzer<JCNewClass, JCNewClass> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   221
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   222
        DiamondInitializer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   223
            super(AnalyzerMode.DIAMOND, NEWCLASS);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   224
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   227
        boolean match(JCNewClass tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   228
            return tree.clazz.hasTag(TYPEAPPLY) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   229
                    !TreeInfo.isDiamond(tree) &&
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 28146
diff changeset
   230
                    (tree.def == null || allowDiamondWithAnonymousClassCreation);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   231
        }
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
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   234
        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
   235
            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
   236
                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
   237
                ((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
   238
                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
   239
            } else {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   240
                return List.of(oldTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   241
            }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   245
        void process(JCNewClass oldTree, JCNewClass newTree, boolean hasErrors) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   246
            if (!hasErrors) {
30404
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   247
                List<Type> inferredArgs, explicitArgs;
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   248
                if (oldTree.def != null) {
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   249
                    inferredArgs = newTree.def.implementing.nonEmpty()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   250
                                      ? 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
   251
                                      : newTree.def.extending.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   252
                    explicitArgs = oldTree.def.implementing.nonEmpty()
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   253
                                      ? 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
   254
                                      : oldTree.def.extending.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   255
                } else {
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   256
                    inferredArgs = newTree.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   257
                    explicitArgs = oldTree.type.getTypeArguments();
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29776
diff changeset
   258
                }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   259
                for (Type t : inferredArgs) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   260
                    if (!types.isSameType(t, explicitArgs.head)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   261
                        return;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   262
                    }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   263
                    explicitArgs = explicitArgs.tail;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   264
                }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   265
                //exact match
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   266
                log.warning(oldTree.clazz, Warnings.DiamondRedundantArgs);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   267
            }
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
     * 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
   273
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   274
    class LambdaAnalyzer extends StatementAnalyzer<JCNewClass, JCLambda> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   275
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   276
        LambdaAnalyzer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   277
            super(AnalyzerMode.LAMBDA, NEWCLASS);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   278
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   281
        boolean match (JCNewClass tree){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   282
            Type clazztype = tree.clazz.type;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   283
            return tree.def != null &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   284
                    clazztype.hasTag(CLASS) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   285
                    types.isFunctionalInterface(clazztype.tsym) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   286
                    decls(tree.def).length() == 1;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   287
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   288
        //where
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   289
            private List<JCTree> decls(JCClassDecl decl) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   290
                ListBuffer<JCTree> decls = new ListBuffer<>();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   291
                for (JCTree t : decl.defs) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   292
                    if (t.hasTag(METHODDEF)) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   293
                        JCMethodDecl md = (JCMethodDecl)t;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   294
                        if ((md.getModifiers().flags & GENERATEDCONSTR) == 0) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   295
                            decls.add(md);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   296
                        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   297
                    } else {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   298
                        decls.add(t);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   299
                    }
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
                return decls.toList();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   302
            }
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
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   305
        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
   306
            JCMethodDecl md = (JCMethodDecl)decls(oldTree.def).head;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   307
            List<JCVariableDecl> params = md.params;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   308
            JCBlock body = md.body;
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   309
            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
   310
            return List.of(newTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   311
        }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   312
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   313
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   314
        void process (JCNewClass oldTree, JCLambda newTree, boolean hasErrors){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   315
            if (!hasErrors) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   316
                log.warning(oldTree.def, Warnings.PotentialLambdaFound);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   317
            }
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
     * 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
   323
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   324
    class RedundantTypeArgAnalyzer extends StatementAnalyzer<JCMethodInvocation, JCMethodInvocation> {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   325
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   326
        RedundantTypeArgAnalyzer() {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   327
            super(AnalyzerMode.METHOD, APPLY);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   328
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   331
        boolean match (JCMethodInvocation tree){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   332
            return tree.typeargs != null &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   333
                    tree.typeargs.nonEmpty();
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   334
        }
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   335
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   336
        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
   337
            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
   338
            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
   339
            return List.of(app);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   340
        }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   341
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   342
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   343
        void process (JCMethodInvocation oldTree, JCMethodInvocation newTree, boolean hasErrors){
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   344
            if (!hasErrors) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   345
                //exact match
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   346
                log.warning(oldTree, Warnings.MethodRedundantTypeargs);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   347
            }
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
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   351
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   352
     * Base class for local variable inference analyzers.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   353
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   354
    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
   355
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   356
        RedundantLocalVarTypeAnalyzerBase(JCTree.Tag tag) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   357
            super(AnalyzerMode.LOCAL, tag);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   358
        }
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
         * 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
   362
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   363
        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
   364
            JCVariableDecl newTree = copier.copy(oldTree);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   365
            newTree.vartype = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   366
            return newTree;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   367
        }
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
         * Analyze results of local variable inference.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   371
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   372
        void processVar(JCVariableDecl oldTree, JCVariableDecl newTree, boolean hasErrors) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   373
            if (!hasErrors) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   374
                if (types.isSameType(oldTree.type, newTree.type)) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   375
                    log.warning(oldTree, Warnings.LocalRedundantType);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   376
                }
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
     * 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
   383
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   384
    class RedundantLocalVarTypeAnalyzer extends RedundantLocalVarTypeAnalyzerBase<JCVariableDecl> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   385
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   386
        RedundantLocalVarTypeAnalyzer() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   387
            super(VARDEF);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   388
        }
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
        boolean match(JCVariableDecl tree){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   391
            return tree.sym.owner.kind == Kind.MTH &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   392
                    tree.init != null && !tree.isImplicitlyTyped() &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   393
                    attr.canInferLocalVarType(tree) == null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   394
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   395
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   396
        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
   397
            return List.of(rewriteVarType(oldTree));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   398
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   399
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   400
        void process(JCVariableDecl oldTree, JCVariableDecl newTree, boolean hasErrors){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   401
            processVar(oldTree, newTree, hasErrors);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   402
        }
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
     * 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
   407
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   408
    class RedundantLocalVarTypeAnalyzerForEach extends RedundantLocalVarTypeAnalyzerBase<JCEnhancedForLoop> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   409
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   410
        RedundantLocalVarTypeAnalyzerForEach() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   411
            super(FOREACHLOOP);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   412
        }
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
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   415
        boolean match(JCEnhancedForLoop tree){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   416
            return !tree.var.isImplicitlyTyped();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   417
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   418
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   419
        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
   420
            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
   421
            newTree.var = rewriteVarType(oldTree.var);
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   422
            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
   423
            return List.of(newTree);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   424
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   425
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   426
        void process(JCEnhancedForLoop oldTree, JCEnhancedForLoop newTree, boolean hasErrors){
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   427
            processVar(oldTree.var, newTree.var, hasErrors);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   428
        }
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
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   431
    @SuppressWarnings({"unchecked", "rawtypes"})
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   432
    StatementAnalyzer<JCTree, JCTree>[] analyzers = new StatementAnalyzer[] {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   433
            new DiamondInitializer(),
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   434
            new LambdaAnalyzer(),
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   435
            new RedundantTypeArgAnalyzer(),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   436
            new RedundantLocalVarTypeAnalyzer(),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   437
            new RedundantLocalVarTypeAnalyzerForEach()
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   438
    };
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
    /**
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   441
     * Create a copy of Env if needed.
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   442
     */
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   443
    Env<AttrContext> copyEnvIfNeeded(JCTree tree, Env<AttrContext> env) {
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   444
        if (!analyzerModes.isEmpty() &&
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   445
                !env.info.isSpeculative &&
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   446
                TreeInfo.isStatement(tree) &&
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   447
                !tree.hasTag(LABELLED)) {
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   448
            Env<AttrContext> analyzeEnv =
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   449
                    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
   450
            analyzeEnv.info.returnResult = analyzeEnv.info.returnResult != null ?
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   451
                    attr.new ResultInfo(analyzeEnv.info.returnResult.pkind,
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   452
                                        analyzeEnv.info.returnResult.pt) : null;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   453
            return analyzeEnv;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   454
        } else {
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   455
            return null;
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   456
        }
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
    /**
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   460
     * Analyze an AST node if needed.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   461
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   462
    void analyzeIfNeeded(JCTree tree, Env<AttrContext> env) {
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   463
        if (env != null) {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   464
            JCStatement stmt = (JCStatement)tree;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   465
            analyze(stmt, env);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   466
        }
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
     * 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
   471
     * 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
   472
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   473
    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
   474
        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
   475
        statementScanner.scan();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   476
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   477
        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
   478
            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
   479
                deferredAnalysisHelper.queue(rewriting);
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   480
            }
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   481
        }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   482
    }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   483
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   484
    /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   485
     * Helper interface to handle deferral of analysis tasks.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   486
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   487
    interface DeferredAnalysisHelper {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   488
        /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   489
         * Add a new analysis task to the queue.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   490
         */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   491
        void queue(RewritingContext rewriting);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   492
        /**
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   493
         * Flush queue with given attribution env.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   494
         */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   495
        void flush(Env<AttrContext> flushEnv);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   496
    }
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
     * Dummy deferral handler.
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   500
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   501
    DeferredAnalysisHelper flushDeferredHelper = new DeferredAnalysisHelper() {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   502
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   503
        public void queue(RewritingContext rewriting) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   504
            //do nothing
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   505
        }
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
        @Override
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   508
        public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   509
            //do nothing
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   510
        }
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
     * 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
   515
     * 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
   516
     */
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   517
    DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   518
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   519
        Map<ClassSymbol, ArrayList<RewritingContext>> Q = new HashMap<>();
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   520
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   521
        @Override
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   522
        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
   523
            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
   524
            s.add(rewriting);
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   525
        }
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
        @Override
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   528
        public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   529
            if (!Q.isEmpty()) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   530
                DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   531
                try {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   532
                    deferredAnalysisHelper = flushDeferredHelper;
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   533
                    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
   534
                    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
   535
                        doAnalysis(rewritings.remove(0));
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   536
                    }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   537
                } finally {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   538
                    deferredAnalysisHelper = prevHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   539
                }
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
    DeferredAnalysisHelper deferredAnalysisHelper = queueDeferredHelper;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   545
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   546
    void doAnalysis(RewritingContext rewriting) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   547
        DiagnosticSource prevSource = log.currentSource();
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   548
        LocalCacheContext localCacheContext = argumentAttr.withLocalCacheContext();
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   549
        try {
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   550
            log.useSource(rewriting.env.toplevel.getSourceFile());
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   551
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   552
            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
   553
            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
   554
                //add a block to hoist potential dangling variable declarations
47957
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   555
                treeToAnalyze = make.at(Position.NOPOS)
7175a92b6fd7 8191981: javac crash when detecting lambda candidates
jlahoda
parents: 47465
diff changeset
   556
                                    .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
   557
            }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   558
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   559
            //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
   560
            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
   561
                    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
   562
            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
   563
        } 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
   564
            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
   565
        } finally {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   566
            log.useSource(prevSource.getFile());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   567
            localCacheContext.leave();
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   568
        }
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
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   571
    public void flush(Env<AttrContext> flushEnv) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   572
        deferredAnalysisHelper.flush(flushEnv);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   573
    }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   574
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   575
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   576
     * 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
   577
     * statement boundaries.
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   578
     */
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   579
    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
   580
        /** 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
   581
        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
   582
        JCTree originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   583
        Env<AttrContext> env;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   584
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   585
        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
   586
            this.originalTree = originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   587
            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
   588
        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   589
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   590
        public void scan() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   591
            scan(originalTree);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   592
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   595
        @SuppressWarnings("unchecked")
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   596
        public void scan(JCTree tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   597
            if (tree != null) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   598
                for (StatementAnalyzer<JCTree, JCTree> analyzer : analyzers) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   599
                    if (analyzer.isEnabled() &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   600
                            tree.hasTag(analyzer.tag) &&
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   601
                            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
   602
                        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
   603
                            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
   604
                        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   605
                        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
   606
                    }
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
            super.scan(tree);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   610
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   613
        public void visitClassDef(JCClassDecl tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   614
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   615
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   618
        public void visitMethodDef(JCMethodDecl tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   619
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   620
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   623
        public void visitBlock(JCBlock tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   624
            //do nothing (prevents seeing same stuff twice)
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   625
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   628
        public void visitSwitch(JCSwitch tree) {
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   629
            scan(tree.getExpression());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   630
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   633
        public void visitForLoop(JCForLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   634
            //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
   635
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   636
            scan(tree.getUpdate());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   637
        }
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
        @Override
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   640
        public void visitTry(JCTry tree) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   641
            //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
   642
            scan(tree.getBlock());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   643
            scan(tree.getCatches());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   644
            scan(tree.getFinallyBlock());
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   645
        }
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
        @Override
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   648
        public void visitForeachLoop(JCEnhancedForLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   649
            //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
   650
            scan(tree.getExpression());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   651
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   654
        public void visitWhileLoop(JCWhileLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   655
            //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
   656
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   657
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   660
        public void visitDoLoop(JCDoWhileLoop tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   661
            //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
   662
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   663
        }
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
        @Override
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   666
        public void visitIf(JCIf tree) {
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   667
            //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
   668
            scan(tree.getCondition());
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   669
        }
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
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   672
    class RewritingContext {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   673
        // 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
   674
        JCTree originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   675
        // 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
   676
        JCTree oldTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   677
        // 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
   678
        JCTree replacement;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   679
        // 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
   680
        boolean erroneous;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   681
        // the env
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   682
        Env<AttrContext> env;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   683
        // the corresponding analyzer
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   684
        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
   685
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   686
        RewritingContext(
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   687
                JCTree originalTree,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   688
                JCTree oldTree,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   689
                JCTree replacement,
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   690
                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
   691
                Env<AttrContext> env) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   692
            this.originalTree = originalTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   693
            this.oldTree = oldTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   694
            this.replacement = replacement;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   695
            this.analyzer = analyzer;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   696
            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
   697
            /*  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
   698
             *  clone operation
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   699
             */
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   700
            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
   701
                // avoid redefinition clashes
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   702
                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
   703
            }
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
         * 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
   708
         */
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   709
        Log.DeferredDiagnosticHandler diagHandler() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   710
            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
   711
                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
   712
                    erroneous = true;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   713
                }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   714
                return true;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   715
            });
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
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   719
    /**
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   720
     * 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
   721
     */
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   722
    class AnalyzerCopier extends TreeCopier<Void> {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   723
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   724
        public AnalyzerCopier() {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   725
            super(make);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   726
        }
28146
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   727
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   728
        @Override @DefinedBy(Api.COMPILER_TREE)
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   729
        public JCTree visitLambdaExpression(LambdaExpressionTree node, Void _unused) {
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   730
            JCLambda oldLambda = (JCLambda)node;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   731
            JCLambda newLambda = (JCLambda)super.visitLambdaExpression(node, _unused);
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   732
            if (oldLambda.paramKind == ParameterKind.IMPLICIT) {
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   733
                //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
   734
                newLambda.paramKind = ParameterKind.IMPLICIT;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   735
                newLambda.params.forEach(p -> p.vartype = null);
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   736
            }
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   737
            return newLambda;
adf3a039cb7d 8067792: Javac crashes in finder mode with nested implicit lambdas
mcimadamore
parents: 28142
diff changeset
   738
        }
45862
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   739
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   740
        @Override @DefinedBy(Api.COMPILER_TREE)
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   741
        public JCTree visitNewClass(NewClassTree node, Void aVoid) {
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   742
            JCNewClass oldNewClazz = (JCNewClass)node;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   743
            JCNewClass newNewClazz = (JCNewClass)super.visitNewClass(node, aVoid);
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   744
            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
   745
                //workaround to Attr generating trees
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   746
                newNewClazz.encl = ((JCUnary)newNewClazz.args.head).arg;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   747
                newNewClazz.args = newNewClazz.args.tail;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   748
            }
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   749
            return newNewClazz;
c6827bac317d 8183126: port improvements to analyzers from lvti repo to jdk10
vromero
parents: 45605
diff changeset
   750
        }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   751
    }
47465
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   752
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   753
   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
   754
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   755
        RewritingContext rewriting;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   756
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   757
        TreeRewriter(RewritingContext rewriting) {
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   758
            this.rewriting = rewriting;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   759
        }
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
        @Override
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   762
        @SuppressWarnings("unchecked")
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   763
        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
   764
            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
   765
            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
   766
                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
   767
                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
   768
            }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   769
            return newTree;
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   770
        }
bc25e62f4794 8183896: improve analyzers to allow for the generation of multiple mutations of the same tree
vromero
parents: 47268
diff changeset
   771
    }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents:
diff changeset
   772
}