CodingRulesAnalyzerPlugin.java revision 2601:8e638f046bf0
1/*
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package crules;
25
26import java.util.ArrayList;
27import java.util.HashMap;
28import java.util.List;
29import java.util.Map;
30
31import javax.lang.model.element.TypeElement;
32import javax.tools.JavaFileObject;
33
34import com.sun.source.tree.Tree;
35import com.sun.source.util.JavacTask;
36import com.sun.source.util.Plugin;
37import com.sun.source.util.TaskEvent;
38import com.sun.source.util.TaskEvent.Kind;
39import com.sun.source.util.TaskListener;
40import com.sun.source.util.Trees;
41import com.sun.tools.javac.api.BasicJavacTask;
42import com.sun.tools.javac.tree.JCTree;
43import com.sun.tools.javac.util.Context;
44import com.sun.tools.javac.util.DefinedBy;
45import com.sun.tools.javac.util.DefinedBy.Api;
46import com.sun.tools.javac.util.Log;
47
48public class CodingRulesAnalyzerPlugin implements Plugin {
49
50    protected Log log;
51    protected Trees trees;
52
53    @DefinedBy(Api.COMPILER_TREE)
54    public void init(JavacTask task, String... args) {
55        BasicJavacTask impl = (BasicJavacTask)task;
56        Context context = impl.getContext();
57        log = Log.instance(context);
58        trees = Trees.instance(task);
59        task.addTaskListener(new PostAnalyzeTaskListener(
60                new MutableFieldsAnalyzer(task),
61                new AssertCheckAnalyzer(task),
62                new DefinedByAnalyzer(task)
63        ));
64    }
65
66    public class PostAnalyzeTaskListener implements TaskListener {
67        private final Map<Kind, List<AbstractCodingRulesAnalyzer>> analyzers = new HashMap<>();
68
69        public PostAnalyzeTaskListener(AbstractCodingRulesAnalyzer... analyzers) {
70            for (AbstractCodingRulesAnalyzer analyzer : analyzers) {
71                List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(analyzer.eventKind);
72
73                if (currentAnalyzers == null) {
74                    this.analyzers.put(analyzer.eventKind, currentAnalyzers = new ArrayList<>());
75                }
76
77                currentAnalyzers.add(analyzer);
78            }
79        }
80
81        @Override @DefinedBy(Api.COMPILER_TREE)
82        public void started(TaskEvent taskEvent) {}
83
84        @Override @DefinedBy(Api.COMPILER_TREE)
85        public void finished(TaskEvent taskEvent) {
86            List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(taskEvent.getKind());
87
88            if (currentAnalyzers != null) {
89                TypeElement typeElem = taskEvent.getTypeElement();
90                Tree tree = trees.getTree(typeElem);
91                if (tree != null) {
92                    JavaFileObject prevSource = log.currentSourceFile();
93                    try {
94                        log.useSource(taskEvent.getCompilationUnit().getSourceFile());
95                        for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) {
96                            analyzer.treeVisitor.scan((JCTree)tree);
97                        }
98                    } finally {
99                        log.useSource(prevSource);
100                    }
101                }
102            }
103        }
104    }
105
106    @Override @DefinedBy(Api.COMPILER_TREE)
107    public String getName() {
108        return "coding_rules";
109    }
110
111}
112