test/tools/javac/plugin/showtype/ShowTypePlugin.java

Thu, 19 Dec 2013 11:38:45 -0500

author
emc
date
Thu, 19 Dec 2013 11:38:45 -0500
changeset 2414
17ce329d7bd0
parent 1457
064e372f273d
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
Summary: Fix NoStringToLower test to only enforce ban on String.toLowerCase on langtools classes
Reviewed-by: vromero, jfranck
Contributed-by: paul.govereau@oracle.com

jjg@1416 1 /*
jjg@1416 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1416 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1416 4 *
jjg@1416 5 * This code is free software; you can redistribute it and/or modify it
jjg@1416 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1416 7 * published by the Free Software Foundation.
jjg@1416 8 *
jjg@1416 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1416 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1416 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1416 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1416 13 * accompanied this code).
jjg@1416 14 *
jjg@1416 15 * You should have received a copy of the GNU General Public License version
jjg@1416 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1416 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1416 18 *
jjg@1416 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1416 20 * or visit www.oracle.com if you need additional information or have any
jjg@1416 21 * questions.
jjg@1416 22 */
jjg@1416 23
jjg@1416 24 import com.sun.source.tree.CompilationUnitTree;
jjg@1416 25 import com.sun.source.tree.IdentifierTree;
jjg@1416 26 import com.sun.source.tree.MemberSelectTree;
jjg@1416 27 import com.sun.source.tree.Tree;
jjg@1416 28 import com.sun.source.util.JavacTask;
jjg@1416 29 import com.sun.source.util.Plugin;
jjg@1416 30 import com.sun.source.util.TaskEvent;
jjg@1416 31 import com.sun.source.util.TaskListener;
jjg@1416 32 import com.sun.source.util.TreePathScanner;
jjg@1416 33 import com.sun.source.util.Trees;
jjg@1416 34 import java.util.regex.Pattern;
jjg@1416 35 import javax.lang.model.type.TypeMirror;
jjg@1416 36 import javax.tools.Diagnostic.Kind;
jjg@1416 37
jjg@1416 38 public class ShowTypePlugin implements Plugin {
jjg@1416 39
jjg@1416 40 public String getName() {
jjg@1416 41 return "showtype";
jjg@1416 42 }
jjg@1416 43
jjg@1457 44 public void init(JavacTask task, String... args) {
jjg@1416 45 Pattern pattern = null;
jjg@1416 46 if (args.length == 1)
jjg@1416 47 pattern = Pattern.compile(args[0]);
jjg@1416 48 task.addTaskListener(new PostAnalyzeTaskListener(task, pattern));
jjg@1416 49 }
jjg@1416 50
jjg@1416 51 private static class PostAnalyzeTaskListener implements TaskListener {
jjg@1416 52 private final ShowTypeTreeVisitor visitor;
jjg@1416 53
jjg@1416 54 PostAnalyzeTaskListener(JavacTask task, Pattern pattern) {
jjg@1416 55 visitor = new ShowTypeTreeVisitor(task, pattern);
jjg@1416 56 }
jjg@1416 57
jjg@1416 58 @Override
jjg@1416 59 public void started(TaskEvent taskEvent) { }
jjg@1416 60
jjg@1416 61 @Override
jjg@1416 62 public void finished(TaskEvent taskEvent) {
jjg@1416 63 if (taskEvent.getKind().equals(TaskEvent.Kind.ANALYZE)) {
jjg@1416 64 CompilationUnitTree compilationUnit = taskEvent.getCompilationUnit();
jjg@1416 65 visitor.scan(compilationUnit, null);
jjg@1416 66 }
jjg@1416 67 }
jjg@1416 68 }
jjg@1416 69
jjg@1416 70 private static class ShowTypeTreeVisitor extends TreePathScanner<Void, Void> {
jjg@1416 71 private final Trees trees;
jjg@1416 72 private final Pattern pattern;
jjg@1416 73 private CompilationUnitTree currCompUnit;
jjg@1416 74
jjg@1416 75 ShowTypeTreeVisitor(JavacTask task, Pattern pattern) {
jjg@1416 76 trees = Trees.instance(task);
jjg@1416 77 this.pattern = pattern;
jjg@1416 78 }
jjg@1416 79
jjg@1416 80 @Override
jjg@1416 81 public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) {
jjg@1416 82 currCompUnit = tree;
jjg@1416 83 return super.visitCompilationUnit(tree, ignore);
jjg@1416 84 }
jjg@1416 85
jjg@1416 86 @Override
jjg@1416 87 public Void visitIdentifier(IdentifierTree tree, Void ignore) {
jjg@1416 88 show(tree, tree.getName());
jjg@1416 89 return super.visitIdentifier(tree, ignore);
jjg@1416 90 }
jjg@1416 91
jjg@1416 92 @Override
jjg@1416 93 public Void visitMemberSelect(MemberSelectTree tree, Void ignore) {
jjg@1416 94 show(tree, tree.getIdentifier());
jjg@1416 95 return super.visitMemberSelect(tree, ignore);
jjg@1416 96 }
jjg@1416 97
jjg@1416 98 void show(Tree tree, CharSequence name) {
jjg@1416 99 if (pattern == null || pattern.matcher(name).matches()) {
jjg@1416 100 TypeMirror type = trees.getTypeMirror(getCurrentPath());
jjg@1416 101 trees.printMessage(Kind.NOTE, "type is " + type, tree, currCompUnit);
jjg@1416 102 }
jjg@1416 103 }
jjg@1416 104 }
jjg@1416 105
jjg@1416 106 }

mercurial