langtools/make/tools/crules/CodingRulesAnalyzerPlugin.java
author mcimadamore
Thu, 05 Mar 2015 13:10:49 +0000
changeset 29294 376a915b4ff0
parent 26266 2d24bda701dc
child 36526 3b41f1c69604
permissions -rw-r--r--
8073645: Add lambda-based lazy eval versions of Assert.check methods Summary: Enhance Assert so that lazy string computation can occurr where needed; enhance static roding rule checkers to make sure the right version of the method is called. Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     1
/*
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     4
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     8
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    13
 * accompanied this code).
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    14
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    18
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    21
 * questions.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    22
 */
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    23
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    24
package crules;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    25
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    26
import java.util.ArrayList;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    27
import java.util.HashMap;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    28
import java.util.List;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    29
import java.util.Map;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    30
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    31
import javax.lang.model.element.TypeElement;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    33
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    34
import com.sun.source.tree.Tree;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    35
import com.sun.source.util.JavacTask;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    36
import com.sun.source.util.Plugin;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    37
import com.sun.source.util.TaskEvent;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    38
import com.sun.source.util.TaskEvent.Kind;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    39
import com.sun.source.util.TaskListener;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    40
import com.sun.source.util.Trees;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    41
import com.sun.tools.javac.api.BasicJavacTask;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    42
import com.sun.tools.javac.tree.JCTree;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    43
import com.sun.tools.javac.util.Context;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    44
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    45
import com.sun.tools.javac.util.DefinedBy.Api;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    46
import com.sun.tools.javac.util.Log;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    47
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    48
public class CodingRulesAnalyzerPlugin implements Plugin {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    49
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    50
    protected Log log;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    51
    protected Trees trees;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    52
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    53
    @DefinedBy(Api.COMPILER_TREE)
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    54
    public void init(JavacTask task, String... args) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    55
        BasicJavacTask impl = (BasicJavacTask)task;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    56
        Context context = impl.getContext();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    57
        log = Log.instance(context);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    58
        trees = Trees.instance(task);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    59
        task.addTaskListener(new PostAnalyzeTaskListener(
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    60
                new MutableFieldsAnalyzer(task),
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    61
                new AssertCheckAnalyzer(task),
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    62
                new DefinedByAnalyzer(task)
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    63
        ));
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    64
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    65
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    66
    public class PostAnalyzeTaskListener implements TaskListener {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    67
        private final Map<Kind, List<AbstractCodingRulesAnalyzer>> analyzers = new HashMap<>();
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    68
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    69
        public PostAnalyzeTaskListener(AbstractCodingRulesAnalyzer... analyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    70
            for (AbstractCodingRulesAnalyzer analyzer : analyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    71
                List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(analyzer.eventKind);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    72
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    73
                if (currentAnalyzers == null) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    74
                    this.analyzers.put(analyzer.eventKind, currentAnalyzers = new ArrayList<>());
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    75
                }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    76
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    77
                currentAnalyzers.add(analyzer);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    78
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    79
        }
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    80
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    81
        @Override @DefinedBy(Api.COMPILER_TREE)
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    82
        public void started(TaskEvent taskEvent) {}
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    83
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    84
        @Override @DefinedBy(Api.COMPILER_TREE)
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    85
        public void finished(TaskEvent taskEvent) {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    86
            List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(taskEvent.getKind());
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    87
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    88
            if (currentAnalyzers != null) {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    89
                TypeElement typeElem = taskEvent.getTypeElement();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    90
                Tree tree = trees.getTree(typeElem);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    91
                if (tree != null) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    92
                    JavaFileObject prevSource = log.currentSourceFile();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    93
                    try {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    94
                        log.useSource(taskEvent.getCompilationUnit().getSourceFile());
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    95
                        for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    96
                            analyzer.treeVisitor.scan((JCTree)tree);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    97
                        }
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    98
                    } finally {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    99
                        log.useSource(prevSource);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   100
                    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   101
                }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   102
            }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   103
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   104
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   105
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
   106
    @Override @DefinedBy(Api.COMPILER_TREE)
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
   107
    public String getName() {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
   108
        return "coding_rules";
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   109
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   110
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   111
}