Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
ksrini@1149 | 1 | /* |
ksrini@1249 | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
ksrini@1149 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ksrini@1149 | 4 | * |
ksrini@1149 | 5 | * This code is free software; you can redistribute it and/or modify it |
ksrini@1149 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ksrini@1149 | 7 | * published by the Free Software Foundation. |
ksrini@1149 | 8 | * |
ksrini@1149 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ksrini@1149 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ksrini@1149 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ksrini@1149 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ksrini@1149 | 13 | * accompanied this code). |
ksrini@1149 | 14 | * |
ksrini@1149 | 15 | * You should have received a copy of the GNU General Public License version |
ksrini@1149 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ksrini@1149 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ksrini@1149 | 18 | * |
ksrini@1149 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ksrini@1149 | 20 | * or visit www.oracle.com if you need additional information or have any |
ksrini@1149 | 21 | * questions. |
ksrini@1149 | 22 | */ |
ksrini@1149 | 23 | |
ksrini@1149 | 24 | /* |
ksrini@1149 | 25 | * @test |
ksrini@1277 | 26 | * @bug 7073631 7159445 7156633 |
ksrini@1149 | 27 | * @summary tests error and diagnostics positions |
ksrini@1149 | 28 | * @author Jan Lahoda |
ksrini@1149 | 29 | */ |
ksrini@1149 | 30 | |
ksrini@1149 | 31 | import com.sun.source.tree.BinaryTree; |
ksrini@1149 | 32 | import com.sun.source.tree.BlockTree; |
ksrini@1149 | 33 | import com.sun.source.tree.ClassTree; |
ksrini@1149 | 34 | import com.sun.source.tree.CompilationUnitTree; |
ksrini@1149 | 35 | import com.sun.source.tree.ErroneousTree; |
ksrini@1149 | 36 | import com.sun.source.tree.ExpressionStatementTree; |
ksrini@1149 | 37 | import com.sun.source.tree.ExpressionTree; |
ksrini@1149 | 38 | import com.sun.source.tree.MethodInvocationTree; |
ksrini@1149 | 39 | import com.sun.source.tree.MethodTree; |
ksrini@1149 | 40 | import com.sun.source.tree.ModifiersTree; |
ksrini@1149 | 41 | import com.sun.source.tree.StatementTree; |
ksrini@1149 | 42 | import com.sun.source.tree.Tree; |
ksrini@1149 | 43 | import com.sun.source.tree.Tree.Kind; |
ksrini@1149 | 44 | import com.sun.source.tree.VariableTree; |
ksrini@1149 | 45 | import com.sun.source.tree.WhileLoopTree; |
ksrini@1149 | 46 | import com.sun.source.util.SourcePositions; |
ksrini@1149 | 47 | import com.sun.source.util.TreeScanner; |
ksrini@1149 | 48 | import com.sun.source.util.Trees; |
ksrini@1149 | 49 | import com.sun.tools.javac.api.JavacTaskImpl; |
ksrini@1149 | 50 | import com.sun.tools.javac.tree.JCTree; |
ksrini@1149 | 51 | import java.io.IOException; |
ksrini@1277 | 52 | import java.lang.annotation.ElementType; |
ksrini@1277 | 53 | import java.lang.annotation.Retention; |
ksrini@1277 | 54 | import java.lang.annotation.RetentionPolicy; |
ksrini@1277 | 55 | import java.lang.annotation.Target; |
ksrini@1277 | 56 | import java.lang.reflect.Method; |
ksrini@1149 | 57 | import java.net.URI; |
ksrini@1149 | 58 | import java.util.ArrayList; |
ksrini@1149 | 59 | import java.util.Arrays; |
ksrini@1149 | 60 | import java.util.LinkedList; |
ksrini@1149 | 61 | import java.util.List; |
ksrini@1277 | 62 | import java.util.regex.Pattern; |
ksrini@1149 | 63 | import javax.tools.Diagnostic; |
ksrini@1149 | 64 | import javax.tools.DiagnosticCollector; |
ksrini@1149 | 65 | import javax.tools.DiagnosticListener; |
ksrini@1149 | 66 | import javax.tools.JavaCompiler; |
ksrini@1149 | 67 | import javax.tools.JavaFileObject; |
ksrini@1149 | 68 | import javax.tools.SimpleJavaFileObject; |
ksrini@1149 | 69 | import javax.tools.ToolProvider; |
ksrini@1149 | 70 | |
ksrini@1149 | 71 | public class JavacParserTest extends TestCase { |
ksrini@1277 | 72 | static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
ksrini@1277 | 73 | |
ksrini@1277 | 74 | private JavacParserTest(){} |
ksrini@1277 | 75 | |
ksrini@1277 | 76 | public static void main(String... args) throws Exception { |
ksrini@1277 | 77 | new JavacParserTest().run(args); |
ksrini@1149 | 78 | } |
ksrini@1149 | 79 | |
ksrini@1277 | 80 | class MyFileObject extends SimpleJavaFileObject { |
ksrini@1149 | 81 | |
ksrini@1149 | 82 | private String text; |
ksrini@1149 | 83 | |
ksrini@1149 | 84 | public MyFileObject(String text) { |
ksrini@1149 | 85 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
ksrini@1149 | 86 | this.text = text; |
ksrini@1149 | 87 | } |
ksrini@1149 | 88 | |
ksrini@1149 | 89 | @Override |
ksrini@1149 | 90 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
ksrini@1149 | 91 | return text; |
ksrini@1149 | 92 | } |
ksrini@1149 | 93 | } |
ksrini@1149 | 94 | /* |
ksrini@1149 | 95 | * converts Windows to Unix style LFs for comparing strings |
ksrini@1149 | 96 | */ |
ksrini@1277 | 97 | String normalize(String in) { |
ksrini@1149 | 98 | return in.replace(System.getProperty("line.separator"), "\n"); |
ksrini@1149 | 99 | } |
ksrini@1149 | 100 | |
ksrini@1277 | 101 | CompilationUnitTree getCompilationUnitTree(String code) throws IOException { |
ksrini@1149 | 102 | |
ksrini@1149 | 103 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1149 | 104 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 105 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 106 | return cut; |
ksrini@1149 | 107 | } |
ksrini@1149 | 108 | |
ksrini@1277 | 109 | List<String> getErroneousTreeValues(ErroneousTree node) { |
ksrini@1149 | 110 | |
ksrini@1149 | 111 | List<String> values = new ArrayList<>(); |
ksrini@1149 | 112 | if (node.getErrorTrees() != null) { |
ksrini@1149 | 113 | for (Tree t : node.getErrorTrees()) { |
ksrini@1149 | 114 | values.add(t.toString()); |
ksrini@1149 | 115 | } |
ksrini@1149 | 116 | } else { |
ksrini@1149 | 117 | throw new RuntimeException("ERROR: No Erroneous tree " |
ksrini@1149 | 118 | + "has been created."); |
ksrini@1149 | 119 | } |
ksrini@1149 | 120 | return values; |
ksrini@1149 | 121 | } |
ksrini@1149 | 122 | |
ksrini@1277 | 123 | @Test |
ksrini@1277 | 124 | void testPositionForSuperConstructorCalls() throws IOException { |
ksrini@1149 | 125 | assert tool != null; |
ksrini@1149 | 126 | |
ksrini@1149 | 127 | String code = "package test; public class Test {public Test() {super();}}"; |
ksrini@1149 | 128 | |
ksrini@1149 | 129 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1149 | 130 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 131 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 132 | SourcePositions pos = Trees.instance(ct).getSourcePositions(); |
ksrini@1149 | 133 | |
ksrini@1149 | 134 | MethodTree method = |
ksrini@1149 | 135 | (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
ksrini@1149 | 136 | ExpressionStatementTree es = |
ksrini@1149 | 137 | (ExpressionStatementTree) method.getBody().getStatements().get(0); |
ksrini@1149 | 138 | |
ksrini@1149 | 139 | final int esStartPos = code.indexOf(es.toString()); |
ksrini@1149 | 140 | final int esEndPos = esStartPos + es.toString().length(); |
ksrini@1149 | 141 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 142 | esStartPos, pos.getStartPosition(cut, es)); |
ksrini@1149 | 143 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 144 | esEndPos, pos.getEndPosition(cut, es)); |
ksrini@1149 | 145 | |
ksrini@1149 | 146 | MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); |
ksrini@1149 | 147 | |
ksrini@1149 | 148 | final int mitStartPos = code.indexOf(mit.toString()); |
ksrini@1149 | 149 | final int mitEndPos = mitStartPos + mit.toString().length(); |
ksrini@1149 | 150 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 151 | mitStartPos, pos.getStartPosition(cut, mit)); |
ksrini@1149 | 152 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 153 | mitEndPos, pos.getEndPosition(cut, mit)); |
ksrini@1149 | 154 | |
ksrini@1149 | 155 | final int methodStartPos = mitStartPos; |
ksrini@1149 | 156 | final int methodEndPos = methodStartPos + mit.getMethodSelect().toString().length(); |
ksrini@1149 | 157 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 158 | methodStartPos, pos.getStartPosition(cut, mit.getMethodSelect())); |
ksrini@1149 | 159 | assertEquals("testPositionForSuperConstructorCalls", |
ksrini@1149 | 160 | methodEndPos, pos.getEndPosition(cut, mit.getMethodSelect())); |
ksrini@1149 | 161 | } |
ksrini@1149 | 162 | |
ksrini@1277 | 163 | @Test |
ksrini@1277 | 164 | void testPositionForEnumModifiers() throws IOException { |
ksrini@1277 | 165 | final String theString = "public"; |
ksrini@1277 | 166 | String code = "package test; " + theString + " enum Test {A;}"; |
ksrini@1149 | 167 | |
ksrini@1149 | 168 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1149 | 169 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 170 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 171 | SourcePositions pos = Trees.instance(ct).getSourcePositions(); |
ksrini@1149 | 172 | |
ksrini@1149 | 173 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 174 | ModifiersTree mt = clazz.getModifiers(); |
ksrini@1277 | 175 | int spos = code.indexOf(theString); |
ksrini@1277 | 176 | int epos = spos + theString.length(); |
ksrini@1149 | 177 | assertEquals("testPositionForEnumModifiers", |
ksrini@1277 | 178 | spos, pos.getStartPosition(cut, mt)); |
ksrini@1149 | 179 | assertEquals("testPositionForEnumModifiers", |
ksrini@1277 | 180 | epos, pos.getEndPosition(cut, mt)); |
ksrini@1149 | 181 | } |
ksrini@1149 | 182 | |
ksrini@1277 | 183 | @Test |
ksrini@1277 | 184 | void testNewClassWithEnclosing() throws IOException { |
ksrini@1149 | 185 | |
ksrini@1277 | 186 | final String theString = "Test.this.new d()"; |
ksrini@1149 | 187 | String code = "package test; class Test { " + |
ksrini@1149 | 188 | "class d {} private void method() { " + |
ksrini@1277 | 189 | "Object o = " + theString + "; } }"; |
ksrini@1149 | 190 | |
ksrini@1149 | 191 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1149 | 192 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 193 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 194 | SourcePositions pos = Trees.instance(ct).getSourcePositions(); |
ksrini@1149 | 195 | |
ksrini@1149 | 196 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 197 | ExpressionTree est = |
ksrini@1149 | 198 | ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); |
ksrini@1149 | 199 | |
ksrini@1277 | 200 | final int spos = code.indexOf(theString); |
ksrini@1277 | 201 | final int epos = spos + theString.length(); |
ksrini@1149 | 202 | assertEquals("testNewClassWithEnclosing", |
ksrini@1277 | 203 | spos, pos.getStartPosition(cut, est)); |
ksrini@1149 | 204 | assertEquals("testNewClassWithEnclosing", |
ksrini@1277 | 205 | epos, pos.getEndPosition(cut, est)); |
ksrini@1149 | 206 | } |
ksrini@1149 | 207 | |
ksrini@1277 | 208 | @Test |
ksrini@1277 | 209 | void testPreferredPositionForBinaryOp() throws IOException { |
ksrini@1149 | 210 | |
ksrini@1149 | 211 | String code = "package test; public class Test {" |
ksrini@1149 | 212 | + "private void test() {" |
ksrini@1149 | 213 | + "Object o = null; boolean b = o != null && o instanceof String;" |
ksrini@1149 | 214 | + "} private Test() {}}"; |
ksrini@1149 | 215 | |
ksrini@1149 | 216 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1149 | 217 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 218 | MethodTree method = (MethodTree) clazz.getMembers().get(0); |
ksrini@1149 | 219 | VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); |
ksrini@1149 | 220 | BinaryTree cond = (BinaryTree) condSt.getInitializer(); |
ksrini@1149 | 221 | |
ksrini@1149 | 222 | JCTree condJC = (JCTree) cond; |
ksrini@1149 | 223 | int condStartPos = code.indexOf("&&"); |
ksrini@1149 | 224 | assertEquals("testPreferredPositionForBinaryOp", |
ksrini@1149 | 225 | condStartPos, condJC.pos); |
ksrini@1149 | 226 | } |
ksrini@1149 | 227 | |
ksrini@1277 | 228 | @Test |
ksrini@1277 | 229 | void testErrorRecoveryForEnhancedForLoop142381() throws IOException { |
ksrini@1277 | 230 | |
ksrini@1277 | 231 | String code = "package test; class Test { " + |
ksrini@1277 | 232 | "private void method() { " + |
ksrini@1277 | 233 | "java.util.Set<String> s = null; for (a : s) {} } }"; |
ksrini@1277 | 234 | |
ksrini@1277 | 235 | final List<Diagnostic<? extends JavaFileObject>> errors = |
ksrini@1277 | 236 | new LinkedList<Diagnostic<? extends JavaFileObject>>(); |
ksrini@1277 | 237 | |
ksrini@1277 | 238 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, |
ksrini@1277 | 239 | new DiagnosticListener<JavaFileObject>() { |
ksrini@1277 | 240 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
ksrini@1277 | 241 | errors.add(diagnostic); |
ksrini@1277 | 242 | } |
ksrini@1277 | 243 | }, null, null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 244 | |
ksrini@1277 | 245 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1277 | 246 | |
ksrini@1277 | 247 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1277 | 248 | StatementTree forStatement = |
ksrini@1277 | 249 | ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); |
ksrini@1277 | 250 | |
ksrini@1277 | 251 | assertEquals("testErrorRecoveryForEnhancedForLoop142381", |
ksrini@1277 | 252 | Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); |
ksrini@1277 | 253 | assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); |
ksrini@1277 | 254 | } |
ksrini@1277 | 255 | |
ksrini@1277 | 256 | @Test |
ksrini@1277 | 257 | void testPositionAnnotationNoPackage187551() throws IOException { |
ksrini@1277 | 258 | |
ksrini@1277 | 259 | String code = "\n@interface Test {}"; |
ksrini@1277 | 260 | |
ksrini@1277 | 261 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1277 | 262 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 263 | |
ksrini@1277 | 264 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1277 | 265 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1277 | 266 | Trees t = Trees.instance(ct); |
ksrini@1277 | 267 | |
ksrini@1277 | 268 | assertEquals("testPositionAnnotationNoPackage187551", |
ksrini@1277 | 269 | 1, t.getSourcePositions().getStartPosition(cut, clazz)); |
ksrini@1277 | 270 | } |
ksrini@1277 | 271 | |
ksrini@1277 | 272 | @Test |
ksrini@1277 | 273 | void testPositionsSane1() throws IOException { |
ksrini@1277 | 274 | performPositionsSanityTest("package test; class Test { " + |
ksrini@1277 | 275 | "private void method() { " + |
ksrini@1277 | 276 | "java.util.List<? extends java.util.List<? extends String>> l; " + |
ksrini@1277 | 277 | "} }"); |
ksrini@1277 | 278 | } |
ksrini@1277 | 279 | |
ksrini@1277 | 280 | @Test |
ksrini@1277 | 281 | void testPositionsSane2() throws IOException { |
ksrini@1277 | 282 | performPositionsSanityTest("package test; class Test { " + |
ksrini@1277 | 283 | "private void method() { " + |
ksrini@1277 | 284 | "java.util.List<? super java.util.List<? super String>> l; " + |
ksrini@1277 | 285 | "} }"); |
ksrini@1277 | 286 | } |
ksrini@1277 | 287 | |
ksrini@1277 | 288 | @Test |
ksrini@1277 | 289 | void testPositionsSane3() throws IOException { |
ksrini@1277 | 290 | performPositionsSanityTest("package test; class Test { " + |
ksrini@1277 | 291 | "private void method() { " + |
ksrini@1277 | 292 | "java.util.List<? super java.util.List<?>> l; } }"); |
ksrini@1277 | 293 | } |
ksrini@1277 | 294 | |
ksrini@1277 | 295 | private void performPositionsSanityTest(String code) throws IOException { |
ksrini@1277 | 296 | |
ksrini@1277 | 297 | final List<Diagnostic<? extends JavaFileObject>> errors = |
ksrini@1277 | 298 | new LinkedList<Diagnostic<? extends JavaFileObject>>(); |
ksrini@1277 | 299 | |
ksrini@1277 | 300 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, |
ksrini@1277 | 301 | new DiagnosticListener<JavaFileObject>() { |
ksrini@1277 | 302 | |
ksrini@1277 | 303 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
ksrini@1277 | 304 | errors.add(diagnostic); |
ksrini@1277 | 305 | } |
ksrini@1277 | 306 | }, null, null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 307 | |
ksrini@1277 | 308 | final CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1277 | 309 | final Trees trees = Trees.instance(ct); |
ksrini@1277 | 310 | |
ksrini@1277 | 311 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 312 | |
ksrini@1277 | 313 | private long parentStart = 0; |
ksrini@1277 | 314 | private long parentEnd = Integer.MAX_VALUE; |
ksrini@1277 | 315 | |
ksrini@1277 | 316 | @Override |
ksrini@1277 | 317 | public Void scan(Tree node, Void p) { |
ksrini@1277 | 318 | if (node == null) { |
ksrini@1277 | 319 | return null; |
ksrini@1277 | 320 | } |
ksrini@1277 | 321 | |
ksrini@1277 | 322 | long start = trees.getSourcePositions().getStartPosition(cut, node); |
ksrini@1277 | 323 | |
ksrini@1277 | 324 | if (start == (-1)) { |
ksrini@1277 | 325 | return null; // synthetic tree |
ksrini@1277 | 326 | } |
ksrini@1277 | 327 | assertTrue(node.toString() + ":" + start + "/" + parentStart, |
ksrini@1277 | 328 | parentStart <= start); |
ksrini@1277 | 329 | |
ksrini@1277 | 330 | long prevParentStart = parentStart; |
ksrini@1277 | 331 | |
ksrini@1277 | 332 | parentStart = start; |
ksrini@1277 | 333 | |
ksrini@1277 | 334 | long end = trees.getSourcePositions().getEndPosition(cut, node); |
ksrini@1277 | 335 | |
ksrini@1277 | 336 | assertTrue(node.toString() + ":" + end + "/" + parentEnd, |
ksrini@1277 | 337 | end <= parentEnd); |
ksrini@1277 | 338 | |
ksrini@1277 | 339 | long prevParentEnd = parentEnd; |
ksrini@1277 | 340 | |
ksrini@1277 | 341 | parentEnd = end; |
ksrini@1277 | 342 | |
ksrini@1277 | 343 | super.scan(node, p); |
ksrini@1277 | 344 | |
ksrini@1277 | 345 | parentStart = prevParentStart; |
ksrini@1277 | 346 | parentEnd = prevParentEnd; |
ksrini@1277 | 347 | |
ksrini@1277 | 348 | return null; |
ksrini@1277 | 349 | } |
ksrini@1277 | 350 | |
ksrini@1277 | 351 | private void assertTrue(String message, boolean b) { |
ksrini@1277 | 352 | if (!b) fail(message); |
ksrini@1277 | 353 | } |
ksrini@1277 | 354 | }.scan(cut, null); |
ksrini@1277 | 355 | } |
ksrini@1277 | 356 | |
ksrini@1277 | 357 | @Test |
ksrini@1277 | 358 | void testCorrectWilcardPositions1() throws IOException { |
ksrini@1277 | 359 | performWildcardPositionsTest("package test; import java.util.List; " + |
ksrini@1277 | 360 | "class Test { private void method() { List<? extends List<? extends String>> l; } }", |
ksrini@1277 | 361 | |
ksrini@1277 | 362 | Arrays.asList("List<? extends List<? extends String>> l;", |
ksrini@1277 | 363 | "List<? extends List<? extends String>>", |
ksrini@1277 | 364 | "List", |
ksrini@1277 | 365 | "? extends List<? extends String>", |
ksrini@1277 | 366 | "List<? extends String>", |
ksrini@1277 | 367 | "List", |
ksrini@1277 | 368 | "? extends String", |
ksrini@1277 | 369 | "String")); |
ksrini@1277 | 370 | } |
ksrini@1277 | 371 | |
ksrini@1277 | 372 | @Test |
ksrini@1277 | 373 | void testCorrectWilcardPositions2() throws IOException { |
ksrini@1277 | 374 | performWildcardPositionsTest("package test; import java.util.List; " |
ksrini@1277 | 375 | + "class Test { private void method() { List<? super List<? super String>> l; } }", |
ksrini@1277 | 376 | Arrays.asList("List<? super List<? super String>> l;", |
ksrini@1277 | 377 | "List<? super List<? super String>>", |
ksrini@1277 | 378 | "List", |
ksrini@1277 | 379 | "? super List<? super String>", |
ksrini@1277 | 380 | "List<? super String>", |
ksrini@1277 | 381 | "List", |
ksrini@1277 | 382 | "? super String", |
ksrini@1277 | 383 | "String")); |
ksrini@1277 | 384 | } |
ksrini@1277 | 385 | |
ksrini@1277 | 386 | @Test |
ksrini@1277 | 387 | void testCorrectWilcardPositions3() throws IOException { |
ksrini@1277 | 388 | performWildcardPositionsTest("package test; import java.util.List; " + |
ksrini@1277 | 389 | "class Test { private void method() { List<? super List<?>> l; } }", |
ksrini@1277 | 390 | |
ksrini@1277 | 391 | Arrays.asList("List<? super List<?>> l;", |
ksrini@1277 | 392 | "List<? super List<?>>", |
ksrini@1277 | 393 | "List", |
ksrini@1277 | 394 | "? super List<?>", |
ksrini@1277 | 395 | "List<?>", |
ksrini@1277 | 396 | "List", |
ksrini@1277 | 397 | "?")); |
ksrini@1277 | 398 | } |
ksrini@1277 | 399 | |
ksrini@1277 | 400 | @Test |
ksrini@1277 | 401 | void testCorrectWilcardPositions4() throws IOException { |
ksrini@1277 | 402 | performWildcardPositionsTest("package test; import java.util.List; " + |
ksrini@1277 | 403 | "class Test { private void method() { " + |
ksrini@1277 | 404 | "List<? extends List<? extends List<? extends String>>> l; } }", |
ksrini@1277 | 405 | |
ksrini@1277 | 406 | Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", |
ksrini@1277 | 407 | "List<? extends List<? extends List<? extends String>>>", |
ksrini@1277 | 408 | "List", |
ksrini@1277 | 409 | "? extends List<? extends List<? extends String>>", |
ksrini@1277 | 410 | "List<? extends List<? extends String>>", |
ksrini@1277 | 411 | "List", |
ksrini@1277 | 412 | "? extends List<? extends String>", |
ksrini@1277 | 413 | "List<? extends String>", |
ksrini@1277 | 414 | "List", |
ksrini@1277 | 415 | "? extends String", |
ksrini@1277 | 416 | "String")); |
ksrini@1277 | 417 | } |
ksrini@1277 | 418 | |
ksrini@1277 | 419 | @Test |
ksrini@1277 | 420 | void testCorrectWilcardPositions5() throws IOException { |
ksrini@1277 | 421 | performWildcardPositionsTest("package test; import java.util.List; " + |
ksrini@1277 | 422 | "class Test { private void method() { " + |
ksrini@1277 | 423 | "List<? extends List<? extends List<? extends String >>> l; } }", |
ksrini@1277 | 424 | Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", |
ksrini@1277 | 425 | "List<? extends List<? extends List<? extends String >>>", |
ksrini@1277 | 426 | "List", |
ksrini@1277 | 427 | "? extends List<? extends List<? extends String >>", |
ksrini@1277 | 428 | "List<? extends List<? extends String >>", |
ksrini@1277 | 429 | "List", |
ksrini@1277 | 430 | "? extends List<? extends String >", |
ksrini@1277 | 431 | "List<? extends String >", |
ksrini@1277 | 432 | "List", |
ksrini@1277 | 433 | "? extends String", |
ksrini@1277 | 434 | "String")); |
ksrini@1277 | 435 | } |
ksrini@1277 | 436 | |
ksrini@1277 | 437 | void performWildcardPositionsTest(final String code, |
ksrini@1277 | 438 | List<String> golden) throws IOException { |
ksrini@1277 | 439 | |
ksrini@1277 | 440 | final List<Diagnostic<? extends JavaFileObject>> errors = |
ksrini@1277 | 441 | new LinkedList<Diagnostic<? extends JavaFileObject>>(); |
ksrini@1277 | 442 | |
ksrini@1277 | 443 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, |
ksrini@1277 | 444 | new DiagnosticListener<JavaFileObject>() { |
ksrini@1277 | 445 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
ksrini@1277 | 446 | errors.add(diagnostic); |
ksrini@1277 | 447 | } |
ksrini@1277 | 448 | }, null, null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 449 | |
ksrini@1277 | 450 | final CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1277 | 451 | final List<String> content = new LinkedList<String>(); |
ksrini@1277 | 452 | final Trees trees = Trees.instance(ct); |
ksrini@1277 | 453 | |
ksrini@1277 | 454 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 455 | @Override |
ksrini@1277 | 456 | public Void scan(Tree node, Void p) { |
ksrini@1277 | 457 | if (node == null) { |
ksrini@1277 | 458 | return null; |
ksrini@1277 | 459 | } |
ksrini@1277 | 460 | long start = trees.getSourcePositions().getStartPosition(cut, node); |
ksrini@1277 | 461 | |
ksrini@1277 | 462 | if (start == (-1)) { |
ksrini@1277 | 463 | return null; // synthetic tree |
ksrini@1277 | 464 | } |
ksrini@1277 | 465 | long end = trees.getSourcePositions().getEndPosition(cut, node); |
ksrini@1277 | 466 | String s = code.substring((int) start, (int) end); |
ksrini@1277 | 467 | content.add(s); |
ksrini@1277 | 468 | |
ksrini@1277 | 469 | return super.scan(node, p); |
ksrini@1277 | 470 | } |
ksrini@1277 | 471 | }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); |
ksrini@1277 | 472 | |
ksrini@1277 | 473 | assertEquals("performWildcardPositionsTest",golden.toString(), |
ksrini@1277 | 474 | content.toString()); |
ksrini@1277 | 475 | } |
ksrini@1277 | 476 | |
ksrini@1277 | 477 | @Test |
ksrini@1277 | 478 | void testStartPositionForMethodWithoutModifiers() throws IOException { |
ksrini@1277 | 479 | |
ksrini@1277 | 480 | String code = "package t; class Test { <T> void t() {} }"; |
ksrini@1277 | 481 | |
ksrini@1277 | 482 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1277 | 483 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 484 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1277 | 485 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1277 | 486 | MethodTree mt = (MethodTree) clazz.getMembers().get(0); |
ksrini@1277 | 487 | Trees t = Trees.instance(ct); |
ksrini@1277 | 488 | int start = (int) t.getSourcePositions().getStartPosition(cut, mt); |
ksrini@1277 | 489 | int end = (int) t.getSourcePositions().getEndPosition(cut, mt); |
ksrini@1277 | 490 | |
ksrini@1277 | 491 | assertEquals("testStartPositionForMethodWithoutModifiers", |
ksrini@1277 | 492 | "<T> void t() {}", code.substring(start, end)); |
ksrini@1277 | 493 | } |
ksrini@1277 | 494 | |
ksrini@1277 | 495 | @Test |
ksrini@1277 | 496 | void testVariableInIfThen1() throws IOException { |
ksrini@1277 | 497 | |
ksrini@1277 | 498 | String code = "package t; class Test { " + |
ksrini@1277 | 499 | "private static void t(String name) { " + |
ksrini@1277 | 500 | "if (name != null) String nn = name.trim(); } }"; |
ksrini@1277 | 501 | |
ksrini@1277 | 502 | DiagnosticCollector<JavaFileObject> coll = |
ksrini@1277 | 503 | new DiagnosticCollector<JavaFileObject>(); |
ksrini@1277 | 504 | |
ksrini@1277 | 505 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, |
ksrini@1277 | 506 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 507 | |
ksrini@1277 | 508 | ct.parse(); |
ksrini@1277 | 509 | |
ksrini@1277 | 510 | List<String> codes = new LinkedList<String>(); |
ksrini@1277 | 511 | |
ksrini@1277 | 512 | for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { |
ksrini@1277 | 513 | codes.add(d.getCode()); |
ksrini@1277 | 514 | } |
ksrini@1277 | 515 | |
ksrini@1277 | 516 | assertEquals("testVariableInIfThen1", |
ksrini@1277 | 517 | Arrays.<String>asList("compiler.err.variable.not.allowed"), |
ksrini@1277 | 518 | codes); |
ksrini@1277 | 519 | } |
ksrini@1277 | 520 | |
ksrini@1277 | 521 | @Test |
ksrini@1277 | 522 | void testVariableInIfThen2() throws IOException { |
ksrini@1277 | 523 | |
ksrini@1277 | 524 | String code = "package t; class Test { " + |
ksrini@1277 | 525 | "private static void t(String name) { " + |
ksrini@1277 | 526 | "if (name != null) class X {} } }"; |
ksrini@1277 | 527 | DiagnosticCollector<JavaFileObject> coll = |
ksrini@1277 | 528 | new DiagnosticCollector<JavaFileObject>(); |
ksrini@1277 | 529 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, |
ksrini@1277 | 530 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 531 | |
ksrini@1277 | 532 | ct.parse(); |
ksrini@1277 | 533 | |
ksrini@1277 | 534 | List<String> codes = new LinkedList<String>(); |
ksrini@1277 | 535 | |
ksrini@1277 | 536 | for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { |
ksrini@1277 | 537 | codes.add(d.getCode()); |
ksrini@1277 | 538 | } |
ksrini@1277 | 539 | |
ksrini@1277 | 540 | assertEquals("testVariableInIfThen2", |
ksrini@1277 | 541 | Arrays.<String>asList("compiler.err.class.not.allowed"), codes); |
ksrini@1277 | 542 | } |
ksrini@1277 | 543 | |
ksrini@1277 | 544 | @Test |
ksrini@1277 | 545 | void testVariableInIfThen3() throws IOException { |
ksrini@1277 | 546 | |
ksrini@1277 | 547 | String code = "package t; class Test { "+ |
ksrini@1277 | 548 | "private static void t() { " + |
ksrini@1277 | 549 | "if (true) abstract class F {} }}"; |
ksrini@1277 | 550 | DiagnosticCollector<JavaFileObject> coll = |
ksrini@1277 | 551 | new DiagnosticCollector<JavaFileObject>(); |
ksrini@1277 | 552 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, |
ksrini@1277 | 553 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 554 | |
ksrini@1277 | 555 | ct.parse(); |
ksrini@1277 | 556 | |
ksrini@1277 | 557 | List<String> codes = new LinkedList<String>(); |
ksrini@1277 | 558 | |
ksrini@1277 | 559 | for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { |
ksrini@1277 | 560 | codes.add(d.getCode()); |
ksrini@1277 | 561 | } |
ksrini@1277 | 562 | |
ksrini@1277 | 563 | assertEquals("testVariableInIfThen3", |
ksrini@1277 | 564 | Arrays.<String>asList("compiler.err.class.not.allowed"), codes); |
ksrini@1277 | 565 | } |
ksrini@1277 | 566 | |
ksrini@1277 | 567 | @Test |
ksrini@1277 | 568 | void testVariableInIfThen4() throws IOException { |
ksrini@1277 | 569 | |
ksrini@1277 | 570 | String code = "package t; class Test { "+ |
ksrini@1277 | 571 | "private static void t(String name) { " + |
ksrini@1277 | 572 | "if (name != null) interface X {} } }"; |
ksrini@1277 | 573 | DiagnosticCollector<JavaFileObject> coll = |
ksrini@1277 | 574 | new DiagnosticCollector<JavaFileObject>(); |
ksrini@1277 | 575 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, |
ksrini@1277 | 576 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 577 | |
ksrini@1277 | 578 | ct.parse(); |
ksrini@1277 | 579 | |
ksrini@1277 | 580 | List<String> codes = new LinkedList<String>(); |
ksrini@1277 | 581 | |
ksrini@1277 | 582 | for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { |
ksrini@1277 | 583 | codes.add(d.getCode()); |
ksrini@1277 | 584 | } |
ksrini@1277 | 585 | |
ksrini@1277 | 586 | assertEquals("testVariableInIfThen4", |
ksrini@1277 | 587 | Arrays.<String>asList("compiler.err.class.not.allowed"), codes); |
ksrini@1277 | 588 | } |
ksrini@1277 | 589 | |
ksrini@1277 | 590 | @Test |
ksrini@1277 | 591 | void testVariableInIfThen5() throws IOException { |
ksrini@1277 | 592 | |
ksrini@1277 | 593 | String code = "package t; class Test { "+ |
ksrini@1277 | 594 | "private static void t() { " + |
ksrini@1277 | 595 | "if (true) } }"; |
ksrini@1277 | 596 | DiagnosticCollector<JavaFileObject> coll = |
ksrini@1277 | 597 | new DiagnosticCollector<JavaFileObject>(); |
ksrini@1277 | 598 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, |
ksrini@1277 | 599 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 600 | |
ksrini@1277 | 601 | ct.parse(); |
ksrini@1277 | 602 | |
ksrini@1277 | 603 | List<String> codes = new LinkedList<String>(); |
ksrini@1277 | 604 | |
ksrini@1277 | 605 | for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { |
ksrini@1277 | 606 | codes.add(d.getCode()); |
ksrini@1277 | 607 | } |
ksrini@1277 | 608 | |
ksrini@1277 | 609 | assertEquals("testVariableInIfThen5", |
ksrini@1277 | 610 | Arrays.<String>asList("compiler.err.illegal.start.of.stmt"), |
ksrini@1277 | 611 | codes); |
ksrini@1277 | 612 | } |
ksrini@1277 | 613 | |
ksrini@1277 | 614 | // see javac bug #6882235, NB bug #98234: |
ksrini@1277 | 615 | @Test |
ksrini@1277 | 616 | void testMissingExponent() throws IOException { |
ksrini@1277 | 617 | |
ksrini@1277 | 618 | String code = "\nclass Test { { System.err.println(0e); } }"; |
ksrini@1277 | 619 | |
ksrini@1277 | 620 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1277 | 621 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1277 | 622 | |
ksrini@1277 | 623 | assertNotNull(ct.parse().iterator().next()); |
ksrini@1277 | 624 | } |
ksrini@1277 | 625 | |
ksrini@1277 | 626 | @Test |
ksrini@1277 | 627 | void testTryResourcePos() throws IOException { |
ksrini@1277 | 628 | |
ksrini@1277 | 629 | final String code = "package t; class Test { " + |
ksrini@1277 | 630 | "{ try (java.io.InputStream in = null) { } } }"; |
ksrini@1277 | 631 | |
ksrini@1277 | 632 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 633 | |
ksrini@1277 | 634 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 635 | @Override |
ksrini@1277 | 636 | public Void visitVariable(VariableTree node, Void p) { |
ksrini@1277 | 637 | if ("in".contentEquals(node.getName())) { |
ksrini@1277 | 638 | JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; |
ksrini@1277 | 639 | assertEquals("testTryResourcePos", "in = null) { } } }", |
ksrini@1277 | 640 | code.substring(var.pos)); |
ksrini@1277 | 641 | } |
ksrini@1277 | 642 | return super.visitVariable(node, p); |
ksrini@1277 | 643 | } |
ksrini@1277 | 644 | }.scan(cut, null); |
ksrini@1277 | 645 | } |
ksrini@1277 | 646 | |
ksrini@1277 | 647 | @Test |
ksrini@1277 | 648 | void testVarPos() throws IOException { |
ksrini@1277 | 649 | |
ksrini@1277 | 650 | final String code = "package t; class Test { " + |
ksrini@1277 | 651 | "{ java.io.InputStream in = null; } }"; |
ksrini@1277 | 652 | |
ksrini@1277 | 653 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 654 | |
ksrini@1277 | 655 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 656 | |
ksrini@1277 | 657 | @Override |
ksrini@1277 | 658 | public Void visitVariable(VariableTree node, Void p) { |
ksrini@1277 | 659 | if ("in".contentEquals(node.getName())) { |
ksrini@1277 | 660 | JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; |
ksrini@1277 | 661 | assertEquals("testVarPos","in = null; } }", |
ksrini@1277 | 662 | code.substring(var.pos)); |
ksrini@1277 | 663 | } |
ksrini@1277 | 664 | return super.visitVariable(node, p); |
ksrini@1277 | 665 | } |
ksrini@1277 | 666 | }.scan(cut, null); |
ksrini@1277 | 667 | } |
ksrini@1277 | 668 | |
ksrini@1277 | 669 | // expected erroneous tree: int x = y;(ERROR); |
ksrini@1277 | 670 | @Test |
ksrini@1277 | 671 | void testOperatorMissingError() throws IOException { |
ksrini@1277 | 672 | |
ksrini@1277 | 673 | String code = "package test; public class ErrorTest { " |
ksrini@1277 | 674 | + "void method() { int x = y z } }"; |
ksrini@1277 | 675 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 676 | final List<String> values = new ArrayList<>(); |
ksrini@1277 | 677 | final List<String> expectedValues = |
ksrini@1277 | 678 | new ArrayList<>(Arrays.asList("[z]")); |
ksrini@1277 | 679 | |
ksrini@1277 | 680 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 681 | @Override |
ksrini@1277 | 682 | public Void visitErroneous(ErroneousTree node, Void p) { |
ksrini@1277 | 683 | values.add(getErroneousTreeValues(node).toString()); |
ksrini@1277 | 684 | return null; |
ksrini@1277 | 685 | |
ksrini@1277 | 686 | } |
ksrini@1277 | 687 | }.scan(cut, null); |
ksrini@1277 | 688 | |
ksrini@1277 | 689 | assertEquals("testSwitchError: The Erroneous tree " |
ksrini@1277 | 690 | + "error values: " + values |
ksrini@1277 | 691 | + " do not match expected error values: " |
ksrini@1277 | 692 | + expectedValues, values, expectedValues); |
ksrini@1277 | 693 | } |
ksrini@1277 | 694 | |
ksrini@1277 | 695 | // expected erroneous tree: String s = (ERROR); |
ksrini@1277 | 696 | @Test |
ksrini@1277 | 697 | void testMissingParenthesisError() throws IOException { |
ksrini@1277 | 698 | |
ksrini@1277 | 699 | String code = "package test; public class ErrorTest { " |
ksrini@1277 | 700 | + "void f() {String s = new String; } }"; |
ksrini@1277 | 701 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 702 | final List<String> values = new ArrayList<>(); |
ksrini@1277 | 703 | final List<String> expectedValues = |
ksrini@1277 | 704 | new ArrayList<>(Arrays.asList("[new String()]")); |
ksrini@1277 | 705 | |
ksrini@1277 | 706 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 707 | @Override |
ksrini@1277 | 708 | public Void visitErroneous(ErroneousTree node, Void p) { |
ksrini@1277 | 709 | values.add(getErroneousTreeValues(node).toString()); |
ksrini@1277 | 710 | return null; |
ksrini@1277 | 711 | } |
ksrini@1277 | 712 | }.scan(cut, null); |
ksrini@1277 | 713 | |
ksrini@1277 | 714 | assertEquals("testSwitchError: The Erroneous tree " |
ksrini@1277 | 715 | + "error values: " + values |
ksrini@1277 | 716 | + " do not match expected error values: " |
ksrini@1277 | 717 | + expectedValues, values, expectedValues); |
ksrini@1277 | 718 | } |
ksrini@1277 | 719 | |
ksrini@1277 | 720 | // expected erroneous tree: package test; (ERROR)(ERROR) |
ksrini@1277 | 721 | @Test |
ksrini@1277 | 722 | void testMissingClassError() throws IOException { |
ksrini@1277 | 723 | |
ksrini@1277 | 724 | String code = "package Test; clas ErrorTest { " |
ksrini@1277 | 725 | + "void f() {String s = new String(); } }"; |
ksrini@1277 | 726 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 727 | final List<String> values = new ArrayList<>(); |
ksrini@1277 | 728 | final List<String> expectedValues = |
ksrini@1277 | 729 | new ArrayList<>(Arrays.asList("[, clas]", "[]")); |
ksrini@1277 | 730 | |
ksrini@1277 | 731 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 732 | @Override |
ksrini@1277 | 733 | public Void visitErroneous(ErroneousTree node, Void p) { |
ksrini@1277 | 734 | values.add(getErroneousTreeValues(node).toString()); |
ksrini@1277 | 735 | return null; |
ksrini@1277 | 736 | } |
ksrini@1277 | 737 | }.scan(cut, null); |
ksrini@1277 | 738 | |
ksrini@1277 | 739 | assertEquals("testSwitchError: The Erroneous tree " |
ksrini@1277 | 740 | + "error values: " + values |
ksrini@1277 | 741 | + " do not match expected error values: " |
ksrini@1277 | 742 | + expectedValues, values, expectedValues); |
ksrini@1277 | 743 | } |
ksrini@1277 | 744 | |
ksrini@1277 | 745 | // expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);} |
ksrini@1277 | 746 | @Test |
ksrini@1277 | 747 | void testSwitchError() throws IOException { |
ksrini@1277 | 748 | |
ksrini@1277 | 749 | String code = "package test; public class ErrorTest { " |
ksrini@1277 | 750 | + "int numDays; void m1(int i) { switchh {i} { case 1: " |
ksrini@1277 | 751 | + "numDays = 31; break; } } }"; |
ksrini@1277 | 752 | CompilationUnitTree cut = getCompilationUnitTree(code); |
ksrini@1277 | 753 | final List<String> values = new ArrayList<>(); |
ksrini@1277 | 754 | final List<String> expectedValues = |
ksrini@1277 | 755 | new ArrayList<>(Arrays.asList("[switchh]", "[i]")); |
ksrini@1277 | 756 | |
ksrini@1277 | 757 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 758 | @Override |
ksrini@1277 | 759 | public Void visitErroneous(ErroneousTree node, Void p) { |
ksrini@1277 | 760 | values.add(getErroneousTreeValues(node).toString()); |
ksrini@1277 | 761 | return null; |
ksrini@1277 | 762 | } |
ksrini@1277 | 763 | }.scan(cut, null); |
ksrini@1277 | 764 | |
ksrini@1277 | 765 | assertEquals("testSwitchError: The Erroneous tree " |
ksrini@1277 | 766 | + "error values: " + values |
ksrini@1277 | 767 | + " do not match expected error values: " |
ksrini@1277 | 768 | + expectedValues, values, expectedValues); |
ksrini@1277 | 769 | } |
ksrini@1277 | 770 | |
ksrini@1277 | 771 | // expected erroneous tree: class ErrorTest {(ERROR) |
ksrini@1277 | 772 | @Test |
ksrini@1277 | 773 | void testMethodError() throws IOException { |
ksrini@1277 | 774 | |
ksrini@1277 | 775 | String code = "package Test; class ErrorTest { " |
ksrini@1277 | 776 | + "static final void f) {String s = new String(); } }"; |
ksrini@1277 | 777 | CompilationUnitTree cut = cut = getCompilationUnitTree(code); |
ksrini@1277 | 778 | |
ksrini@1277 | 779 | final List<String> values = new ArrayList<>(); |
ksrini@1277 | 780 | final List<String> expectedValues = |
ksrini@1277 | 781 | new ArrayList<>(Arrays.asList("[\nstatic final void f();]")); |
ksrini@1277 | 782 | |
ksrini@1277 | 783 | new TreeScanner<Void, Void>() { |
ksrini@1277 | 784 | @Override |
ksrini@1277 | 785 | public Void visitErroneous(ErroneousTree node, Void p) { |
ksrini@1277 | 786 | values.add(normalize(getErroneousTreeValues(node).toString())); |
ksrini@1277 | 787 | return null; |
ksrini@1277 | 788 | } |
ksrini@1277 | 789 | }.scan(cut, null); |
ksrini@1277 | 790 | |
ksrini@1277 | 791 | assertEquals("testMethodError: The Erroneous tree " |
ksrini@1277 | 792 | + "error value: " + values |
ksrini@1277 | 793 | + " does not match expected error values: " |
ksrini@1277 | 794 | + expectedValues, values, expectedValues); |
ksrini@1277 | 795 | } |
ksrini@1277 | 796 | |
ksrini@1277 | 797 | /* |
ksrini@1277 | 798 | * The following tests do not work just yet with nb-javac nor javac, |
ksrini@1277 | 799 | * they need further investigation, see CR: 7167356 |
ksrini@1277 | 800 | */ |
ksrini@1277 | 801 | |
ksrini@1277 | 802 | void testPositionBrokenSource126732a() throws IOException { |
ksrini@1149 | 803 | String[] commands = new String[]{ |
ksrini@1149 | 804 | "return Runnable()", |
ksrini@1149 | 805 | "do { } while (true)", |
ksrini@1149 | 806 | "throw UnsupportedOperationException()", |
ksrini@1149 | 807 | "assert true", |
ksrini@1149 | 808 | "1 + 1",}; |
ksrini@1149 | 809 | |
ksrini@1149 | 810 | for (String command : commands) { |
ksrini@1149 | 811 | |
ksrini@1149 | 812 | String code = "package test;\n" |
ksrini@1149 | 813 | + "public class Test {\n" |
ksrini@1149 | 814 | + " public static void test() {\n" |
ksrini@1149 | 815 | + " " + command + " {\n" |
ksrini@1149 | 816 | + " new Runnable() {\n" |
ksrini@1149 | 817 | + " };\n" |
ksrini@1149 | 818 | + " }\n" |
ksrini@1149 | 819 | + "}"; |
ksrini@1149 | 820 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, |
ksrini@1149 | 821 | null, null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 822 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 823 | |
ksrini@1149 | 824 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 825 | MethodTree method = (MethodTree) clazz.getMembers().get(0); |
ksrini@1149 | 826 | List<? extends StatementTree> statements = |
ksrini@1149 | 827 | method.getBody().getStatements(); |
ksrini@1149 | 828 | |
ksrini@1149 | 829 | StatementTree ret = statements.get(0); |
ksrini@1149 | 830 | StatementTree block = statements.get(1); |
ksrini@1149 | 831 | |
ksrini@1149 | 832 | Trees t = Trees.instance(ct); |
ksrini@1149 | 833 | int len = code.indexOf(command + " {") + (command + " ").length(); |
ksrini@1149 | 834 | assertEquals(command, len, |
ksrini@1149 | 835 | t.getSourcePositions().getEndPosition(cut, ret)); |
ksrini@1149 | 836 | assertEquals(command, len, |
ksrini@1149 | 837 | t.getSourcePositions().getStartPosition(cut, block)); |
ksrini@1149 | 838 | } |
ksrini@1149 | 839 | } |
ksrini@1149 | 840 | |
ksrini@1277 | 841 | void testPositionBrokenSource126732b() throws IOException { |
ksrini@1149 | 842 | String[] commands = new String[]{ |
ksrini@1149 | 843 | "break", |
ksrini@1149 | 844 | "break A", |
ksrini@1149 | 845 | "continue ", |
ksrini@1149 | 846 | "continue A",}; |
ksrini@1149 | 847 | |
ksrini@1149 | 848 | for (String command : commands) { |
ksrini@1149 | 849 | |
ksrini@1149 | 850 | String code = "package test;\n" |
ksrini@1149 | 851 | + "public class Test {\n" |
ksrini@1149 | 852 | + " public static void test() {\n" |
ksrini@1149 | 853 | + " while (true) {\n" |
ksrini@1149 | 854 | + " " + command + " {\n" |
ksrini@1149 | 855 | + " new Runnable() {\n" |
ksrini@1149 | 856 | + " };\n" |
ksrini@1149 | 857 | + " }\n" |
ksrini@1149 | 858 | + " }\n" |
ksrini@1149 | 859 | + "}"; |
ksrini@1149 | 860 | |
ksrini@1149 | 861 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, |
ksrini@1149 | 862 | null, null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 863 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 864 | |
ksrini@1149 | 865 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 866 | MethodTree method = (MethodTree) clazz.getMembers().get(0); |
ksrini@1149 | 867 | List<? extends StatementTree> statements = |
ksrini@1149 | 868 | ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); |
ksrini@1149 | 869 | |
ksrini@1149 | 870 | StatementTree ret = statements.get(0); |
ksrini@1149 | 871 | StatementTree block = statements.get(1); |
ksrini@1149 | 872 | |
ksrini@1149 | 873 | Trees t = Trees.instance(ct); |
ksrini@1149 | 874 | int len = code.indexOf(command + " {") + (command + " ").length(); |
ksrini@1149 | 875 | assertEquals(command, len, |
ksrini@1149 | 876 | t.getSourcePositions().getEndPosition(cut, ret)); |
ksrini@1149 | 877 | assertEquals(command, len, |
ksrini@1149 | 878 | t.getSourcePositions().getStartPosition(cut, block)); |
ksrini@1149 | 879 | } |
ksrini@1149 | 880 | } |
ksrini@1149 | 881 | |
ksrini@1277 | 882 | void testStartPositionEnumConstantInit() throws IOException { |
ksrini@1149 | 883 | |
ksrini@1149 | 884 | String code = "package t; enum Test { AAA; }"; |
ksrini@1149 | 885 | |
ksrini@1149 | 886 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
ksrini@1149 | 887 | null, Arrays.asList(new MyFileObject(code))); |
ksrini@1149 | 888 | CompilationUnitTree cut = ct.parse().iterator().next(); |
ksrini@1149 | 889 | ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
ksrini@1149 | 890 | VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); |
ksrini@1149 | 891 | Trees t = Trees.instance(ct); |
ksrini@1149 | 892 | int start = (int) t.getSourcePositions().getStartPosition(cut, |
ksrini@1149 | 893 | enumAAA.getInitializer()); |
ksrini@1149 | 894 | |
ksrini@1149 | 895 | assertEquals("testStartPositionEnumConstantInit", -1, start); |
ksrini@1149 | 896 | } |
ksrini@1149 | 897 | |
ksrini@1277 | 898 | void run(String[] args) throws Exception { |
ksrini@1277 | 899 | int passed = 0, failed = 0; |
ksrini@1277 | 900 | final Pattern p = (args != null && args.length > 0) |
ksrini@1277 | 901 | ? Pattern.compile(args[0]) |
ksrini@1277 | 902 | : null; |
ksrini@1277 | 903 | for (Method m : this.getClass().getDeclaredMethods()) { |
ksrini@1277 | 904 | boolean selected = (p == null) |
ksrini@1277 | 905 | ? m.isAnnotationPresent(Test.class) |
ksrini@1277 | 906 | : p.matcher(m.getName()).matches(); |
ksrini@1277 | 907 | if (selected) { |
ksrini@1277 | 908 | try { |
ksrini@1277 | 909 | m.invoke(this, (Object[]) null); |
ksrini@1277 | 910 | System.out.println(m.getName() + ": OK"); |
ksrini@1277 | 911 | passed++; |
ksrini@1277 | 912 | } catch (Throwable ex) { |
ksrini@1277 | 913 | System.out.printf("Test %s failed: %s %n", m, ex.getCause()); |
ksrini@1277 | 914 | failed++; |
ksrini@1277 | 915 | } |
ksrini@1277 | 916 | } |
ksrini@1149 | 917 | } |
ksrini@1277 | 918 | System.out.printf("Passed: %d, Failed %d%n", passed, failed); |
ksrini@1277 | 919 | if (failed > 0) { |
ksrini@1277 | 920 | throw new RuntimeException("Tests failed: " + failed); |
ksrini@1149 | 921 | } |
ksrini@1277 | 922 | if (passed == 0 && failed == 0) { |
ksrini@1277 | 923 | throw new AssertionError("No test(s) selected: passed = " + |
ksrini@1277 | 924 | passed + ", failed = " + failed + " ??????????"); |
ksrini@1149 | 925 | } |
ksrini@1149 | 926 | } |
ksrini@1149 | 927 | } |
ksrini@1149 | 928 | |
ksrini@1149 | 929 | abstract class TestCase { |
ksrini@1149 | 930 | |
ksrini@1149 | 931 | void assertEquals(String message, int i, int pos) { |
ksrini@1149 | 932 | if (i != pos) { |
ksrini@1149 | 933 | fail(message); |
ksrini@1149 | 934 | } |
ksrini@1149 | 935 | } |
ksrini@1149 | 936 | |
ksrini@1259 | 937 | void assertFalse(String message, boolean bvalue) { |
ksrini@1259 | 938 | if (bvalue == true) { |
ksrini@1259 | 939 | fail(message); |
ksrini@1259 | 940 | } |
ksrini@1149 | 941 | } |
ksrini@1149 | 942 | |
ksrini@1149 | 943 | void assertEquals(String message, int i, long l) { |
ksrini@1149 | 944 | if (i != l) { |
ksrini@1149 | 945 | fail(message + ":" + i + ":" + l); |
ksrini@1149 | 946 | } |
ksrini@1149 | 947 | } |
ksrini@1149 | 948 | |
ksrini@1149 | 949 | void assertEquals(String message, Object o1, Object o2) { |
ksrini@1149 | 950 | if (o1 != null && o2 != null && !o1.equals(o2)) { |
ksrini@1149 | 951 | fail(message); |
ksrini@1149 | 952 | } |
ksrini@1149 | 953 | if (o1 == null && o2 != null) { |
ksrini@1149 | 954 | fail(message); |
ksrini@1149 | 955 | } |
ksrini@1149 | 956 | } |
ksrini@1149 | 957 | |
ksrini@1149 | 958 | void assertNotNull(Object o) { |
ksrini@1149 | 959 | if (o == null) { |
ksrini@1149 | 960 | fail(); |
ksrini@1149 | 961 | } |
ksrini@1149 | 962 | } |
ksrini@1149 | 963 | |
ksrini@1149 | 964 | void fail() { |
ksrini@1149 | 965 | fail("test failed"); |
ksrini@1149 | 966 | } |
ksrini@1149 | 967 | |
ksrini@1149 | 968 | void fail(String message) { |
ksrini@1149 | 969 | throw new RuntimeException(message); |
ksrini@1149 | 970 | } |
ksrini@1277 | 971 | |
ksrini@1277 | 972 | /** |
ksrini@1277 | 973 | * Indicates that the annotated method is a test method. |
ksrini@1277 | 974 | */ |
ksrini@1277 | 975 | @Retention(RetentionPolicy.RUNTIME) |
ksrini@1277 | 976 | @Target(ElementType.METHOD) |
ksrini@1277 | 977 | public @interface Test {} |
ksrini@1149 | 978 | } |