Wed, 08 Oct 2014 14:16:40 -0700
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import com.sun.source.tree.ClassTree; |
aoqi@0 | 25 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 26 | import com.sun.source.tree.MethodTree; |
aoqi@0 | 27 | import com.sun.source.tree.NewClassTree; |
aoqi@0 | 28 | import com.sun.source.tree.Tree; |
aoqi@0 | 29 | import com.sun.source.tree.VariableTree; |
aoqi@0 | 30 | import com.sun.source.util.JavacTask; |
aoqi@0 | 31 | import com.sun.source.util.TreeScanner; |
aoqi@0 | 32 | import com.sun.source.util.Trees; |
aoqi@0 | 33 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 34 | import com.sun.tools.javac.code.Flags; |
aoqi@0 | 35 | import com.sun.tools.javac.file.JavacFileManager; |
aoqi@0 | 36 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
aoqi@0 | 37 | import java.io.File; |
aoqi@0 | 38 | import java.io.IOException; |
aoqi@0 | 39 | import java.net.URI; |
aoqi@0 | 40 | import java.net.URISyntaxException; |
aoqi@0 | 41 | import java.util.ArrayList; |
aoqi@0 | 42 | import java.util.Arrays; |
aoqi@0 | 43 | import java.util.Iterator; |
aoqi@0 | 44 | import java.util.List; |
aoqi@0 | 45 | import javax.tools.Diagnostic; |
aoqi@0 | 46 | import javax.tools.DiagnosticListener; |
aoqi@0 | 47 | import javax.tools.FileObject; |
aoqi@0 | 48 | import javax.tools.ForwardingJavaFileManager; |
aoqi@0 | 49 | import javax.tools.JavaFileManager; |
aoqi@0 | 50 | import javax.tools.JavaFileObject; |
aoqi@0 | 51 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 52 | |
aoqi@0 | 53 | /**Takes a source file, parses it once to get the warnings inside the file and |
aoqi@0 | 54 | * then for each and every declaration in the file, it tries to place |
aoqi@0 | 55 | * the @SuppressWarnings annotation on the declaration and verifies than no |
aoqi@0 | 56 | * warnings are produced inside the declaration, but all are produced outside it. |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * Currently only works with <code>unchecked,deprecation,cast</code> warnings. |
aoqi@0 | 59 | */ |
aoqi@0 | 60 | public class VerifySuppressWarnings { |
aoqi@0 | 61 | |
aoqi@0 | 62 | private static final List<String> STANDARD_PARAMS = Arrays.asList("-Xlint:unchecked,deprecation,cast", "-Xjcov"); |
aoqi@0 | 63 | |
aoqi@0 | 64 | public static void main(String... args) throws IOException, URISyntaxException { |
aoqi@0 | 65 | if (args.length != 1) throw new IllegalStateException("Must provide class name!"); |
aoqi@0 | 66 | String testContent = null; |
aoqi@0 | 67 | List<File> sourcePath = new ArrayList<>(); |
aoqi@0 | 68 | for (String sourcePaths : System.getProperty("test.src.path").split(":")) { |
aoqi@0 | 69 | sourcePath.add(new File(sourcePaths)); |
aoqi@0 | 70 | } |
aoqi@0 | 71 | JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
aoqi@0 | 72 | for (File sp : sourcePath) { |
aoqi@0 | 73 | File inp = new File(sp, args[0]); |
aoqi@0 | 74 | |
aoqi@0 | 75 | if (inp.canRead()) { |
aoqi@0 | 76 | testContent = fm.getRegularFile(inp).getCharContent(true).toString(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | } |
aoqi@0 | 79 | if (testContent == null) throw new IllegalStateException(); |
aoqi@0 | 80 | final List<Diagnostic<?>> diagnostics = new ArrayList<>(); |
aoqi@0 | 81 | DiagnosticListener<JavaFileObject> collectDiagnostics = new DiagnosticListener<JavaFileObject>() { |
aoqi@0 | 82 | @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
aoqi@0 | 83 | diagnostics.add(diagnostic); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | }; |
aoqi@0 | 86 | JavaFileObject testFile = new TestFO(new URI("mem://" + args[0]), testContent); |
aoqi@0 | 87 | JavacTask task = JavacTool.create().getTask(null, |
aoqi@0 | 88 | new TestFM(fm), |
aoqi@0 | 89 | collectDiagnostics, |
aoqi@0 | 90 | STANDARD_PARAMS, |
aoqi@0 | 91 | null, |
aoqi@0 | 92 | Arrays.asList(testFile)); |
aoqi@0 | 93 | final Trees trees = Trees.instance(task); |
aoqi@0 | 94 | final CompilationUnitTree cut = task.parse().iterator().next(); |
aoqi@0 | 95 | task.analyze(); |
aoqi@0 | 96 | |
aoqi@0 | 97 | final List<int[]> declarationSpans = new ArrayList<>(); |
aoqi@0 | 98 | |
aoqi@0 | 99 | new TreeScanner<Void, Void>() { |
aoqi@0 | 100 | @Override public Void visitClass(ClassTree node, Void p) { |
aoqi@0 | 101 | handleDeclaration(node); |
aoqi@0 | 102 | return super.visitClass(node, p); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | @Override public Void visitMethod(MethodTree node, Void p) { |
aoqi@0 | 105 | handleDeclaration(node); |
aoqi@0 | 106 | return super.visitMethod(node, p); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | @Override public Void visitVariable(VariableTree node, Void p) { |
aoqi@0 | 109 | handleDeclaration(node); |
aoqi@0 | 110 | return super.visitVariable(node, p); |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | @Override |
aoqi@0 | 114 | public Void visitNewClass(NewClassTree node, Void p) { |
aoqi@0 | 115 | if (node.getClassBody() != null) { |
aoqi@0 | 116 | scan(node.getClassBody().getMembers(), null); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | return null; |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | private void handleDeclaration(Tree node) { |
aoqi@0 | 122 | int endPos = (int) trees.getSourcePositions().getEndPosition(cut, node); |
aoqi@0 | 123 | |
aoqi@0 | 124 | if (endPos == (-1)) { |
aoqi@0 | 125 | if (node.getKind() == Tree.Kind.METHOD && (((JCMethodDecl) node).getModifiers().flags & Flags.GENERATEDCONSTR) != 0) { |
aoqi@0 | 126 | return ; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | throw new IllegalStateException(); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | declarationSpans.add(new int[] {(int) trees.getSourcePositions().getStartPosition(cut, node), endPos}); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | }.scan(cut, null); |
aoqi@0 | 134 | |
aoqi@0 | 135 | for (final int[] declarationSpan : declarationSpans) { |
aoqi@0 | 136 | final String suppressWarnings = "@SuppressWarnings({\"deprecation\", \"unchecked\", \"serial\"})"; |
aoqi@0 | 137 | final String updatedContent = testContent.substring(0, declarationSpan[0]) + suppressWarnings + testContent.substring(declarationSpan[0]); |
aoqi@0 | 138 | final List<Diagnostic<?>> foundErrors = new ArrayList<>(diagnostics); |
aoqi@0 | 139 | DiagnosticListener<JavaFileObject> verifyDiagnostics = new DiagnosticListener<JavaFileObject>() { |
aoqi@0 | 140 | @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
aoqi@0 | 141 | long adjustedPos = diagnostic.getPosition(); |
aoqi@0 | 142 | |
aoqi@0 | 143 | if (adjustedPos >= declarationSpan[0]) adjustedPos -= suppressWarnings.length(); |
aoqi@0 | 144 | |
aoqi@0 | 145 | if (declarationSpan[0] <= adjustedPos && adjustedPos <= declarationSpan[1]) { |
aoqi@0 | 146 | throw new IllegalStateException("unsuppressed: " + diagnostic.getMessage(null)); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | boolean found = false; |
aoqi@0 | 150 | |
aoqi@0 | 151 | for (Iterator<Diagnostic<?>> it = foundErrors.iterator(); it.hasNext();) { |
aoqi@0 | 152 | Diagnostic<?> d = it.next(); |
aoqi@0 | 153 | if (d.getPosition() == adjustedPos && d.getCode().equals(diagnostic.getCode())) { |
aoqi@0 | 154 | it.remove(); |
aoqi@0 | 155 | found = true; |
aoqi@0 | 156 | break; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | if (!found) { |
aoqi@0 | 161 | throw new IllegalStateException("diagnostic not originally reported: " + diagnostic.getMessage(null)); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | } |
aoqi@0 | 164 | }; |
aoqi@0 | 165 | |
aoqi@0 | 166 | JavaFileObject updatedFile = new TestFO(new URI("mem://" + args[0]), updatedContent); |
aoqi@0 | 167 | JavacTask testTask = JavacTool.create().getTask(null, |
aoqi@0 | 168 | new TestFM(fm), |
aoqi@0 | 169 | verifyDiagnostics, |
aoqi@0 | 170 | STANDARD_PARAMS, |
aoqi@0 | 171 | null, |
aoqi@0 | 172 | Arrays.asList(updatedFile)); |
aoqi@0 | 173 | |
aoqi@0 | 174 | testTask.analyze(); |
aoqi@0 | 175 | |
aoqi@0 | 176 | for (Diagnostic<?> d : foundErrors) { |
aoqi@0 | 177 | if (d.getPosition() < declarationSpan[0] || declarationSpan[1] < d.getPosition()) { |
aoqi@0 | 178 | throw new IllegalStateException("missing: " + d.getMessage(null)); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | private static final class TestFO extends SimpleJavaFileObject { |
aoqi@0 | 185 | private final String content; |
aoqi@0 | 186 | public TestFO(URI uri, String content) { |
aoqi@0 | 187 | super(uri, Kind.SOURCE); |
aoqi@0 | 188 | this.content = content; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
aoqi@0 | 192 | return content; |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | @Override public boolean isNameCompatible(String simpleName, Kind kind) { |
aoqi@0 | 196 | return true; |
aoqi@0 | 197 | } |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | private static final class TestFM extends ForwardingJavaFileManager<JavaFileManager> { |
aoqi@0 | 201 | |
aoqi@0 | 202 | public TestFM(JavaFileManager fileManager) { |
aoqi@0 | 203 | super(fileManager); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | |
aoqi@0 | 206 | @Override |
aoqi@0 | 207 | public boolean isSameFile(FileObject a, FileObject b) { |
aoqi@0 | 208 | return a.equals(b); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | } |
aoqi@0 | 212 | } |