langtools/make/tools/crules/CodingRulesAnalyzerPlugin.java
author jlahoda
Fri, 01 Aug 2014 11:09:40 -0700
changeset 25848 3bc09f4676a9
parent 17283 langtools/make/tools/crules/AbstractCodingRulesAnalyzer.java@cddfdf252f4a
child 26266 2d24bda701dc
permissions -rw-r--r--
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks. Summary: Generalizing the crules infrastructure, adding a new analyzer to check String concatenation in Assert calls. Reviewed-by: jjg, vromero
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;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    44
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
    45
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    46
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
    47
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    48
    protected Log log;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    49
    protected Trees trees;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    50
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    51
    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
    52
        BasicJavacTask impl = (BasicJavacTask)task;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    53
        Context context = impl.getContext();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    54
        log = Log.instance(context);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    55
        trees = Trees.instance(task);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    56
        task.addTaskListener(new PostAnalyzeTaskListener(
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    57
                new MutableFieldsAnalyzer(task),
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    58
                new AssertCheckAnalyzer(task)
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    59
        ));
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    60
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    61
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    62
    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
    63
        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
    64
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    65
        public PostAnalyzeTaskListener(AbstractCodingRulesAnalyzer... analyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    66
            for (AbstractCodingRulesAnalyzer analyzer : analyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    67
                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
    68
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    69
                if (currentAnalyzers == null) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    70
                    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
    71
                }
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
                currentAnalyzers.add(analyzer);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    74
            }
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    75
        }
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    76
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    77
        @Override
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    78
        public void started(TaskEvent taskEvent) {}
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    79
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    80
        @Override
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    81
        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
    82
            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
    83
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    84
            if (currentAnalyzers != null) {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    85
                TypeElement typeElem = taskEvent.getTypeElement();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    86
                Tree tree = trees.getTree(typeElem);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    87
                if (tree != null) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    88
                    JavaFileObject prevSource = log.currentSourceFile();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    89
                    try {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    90
                        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
    91
                        for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    92
                            analyzer.treeVisitor.scan((JCTree)tree);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    93
                        }
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    94
                    } finally {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    95
                        log.useSource(prevSource);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    96
                    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    97
                }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    98
            }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    99
        }
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
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
   102
    @Override
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
   103
    public String getName() {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
   104
        return "coding_rules";
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   105
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   106
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   107
}