1.1 --- a/test/tools/javac/parser/netbeans/JavacParserTest.java Tue Dec 06 20:26:09 2011 -0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,716 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. 1.11 - * 1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 - * version 2 for more details (a copy is included in the LICENSE file that 1.16 - * accompanied this code). 1.17 - * 1.18 - * You should have received a copy of the GNU General Public License version 1.19 - * 2 along with this work; if not, write to the Free Software Foundation, 1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 - * 1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 - * or visit www.oracle.com if you need additional information or have any 1.24 - * questions. 1.25 - */ 1.26 - 1.27 -/* 1.28 - * @test 1.29 - * @bug 7073631 1.30 - * @summary tests error and diagnostics positions 1.31 - * @author jan.lahoda@oracle.com 1.32 - */ 1.33 - 1.34 -import com.sun.source.tree.BinaryTree; 1.35 -import com.sun.source.tree.BlockTree; 1.36 -import com.sun.source.tree.ClassTree; 1.37 -import com.sun.source.tree.CompilationUnitTree; 1.38 -import com.sun.source.tree.ExpressionStatementTree; 1.39 -import com.sun.source.tree.ExpressionTree; 1.40 -import com.sun.source.tree.MethodInvocationTree; 1.41 -import com.sun.source.tree.MethodTree; 1.42 -import com.sun.source.tree.ModifiersTree; 1.43 -import com.sun.source.tree.StatementTree; 1.44 -import com.sun.source.tree.Tree; 1.45 -import com.sun.source.tree.Tree.Kind; 1.46 -import com.sun.source.tree.VariableTree; 1.47 -import com.sun.source.tree.WhileLoopTree; 1.48 -import com.sun.source.util.SourcePositions; 1.49 -import com.sun.source.util.TreeScanner; 1.50 -import com.sun.source.util.Trees; 1.51 -import com.sun.tools.javac.api.JavacTaskImpl; 1.52 -import com.sun.tools.javac.tree.JCTree; 1.53 -import java.io.IOException; 1.54 -import java.net.URI; 1.55 -import java.util.Arrays; 1.56 -import java.util.LinkedList; 1.57 -import java.util.List; 1.58 -import javax.tools.Diagnostic; 1.59 -import javax.tools.DiagnosticCollector; 1.60 -import javax.tools.DiagnosticListener; 1.61 -import javax.tools.JavaCompiler; 1.62 -import javax.tools.JavaFileObject; 1.63 -import javax.tools.SimpleJavaFileObject; 1.64 -import javax.tools.ToolProvider; 1.65 - 1.66 -public class JavacParserTest extends TestCase { 1.67 - final JavaCompiler tool; 1.68 - public JavacParserTest(String testName) { 1.69 - tool = ToolProvider.getSystemJavaCompiler(); 1.70 - System.out.println("java.home=" + System.getProperty("java.home")); 1.71 - } 1.72 - 1.73 - static class MyFileObject extends SimpleJavaFileObject { 1.74 - 1.75 - private String text; 1.76 - 1.77 - public MyFileObject(String text) { 1.78 - super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.79 - this.text = text; 1.80 - } 1.81 - 1.82 - @Override 1.83 - public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.84 - return text; 1.85 - } 1.86 - } 1.87 - 1.88 - public void testPositionForSuperConstructorCalls() throws IOException { 1.89 - assert tool != null; 1.90 - 1.91 - String code = "package test; public class Test {public Test() {super();}}"; 1.92 - 1.93 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.94 - null, Arrays.asList(new MyFileObject(code))); 1.95 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.96 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 1.97 - 1.98 - MethodTree method = 1.99 - (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); 1.100 - ExpressionStatementTree es = 1.101 - (ExpressionStatementTree) method.getBody().getStatements().get(0); 1.102 - 1.103 - assertEquals("testPositionForSuperConstructorCalls", 1.104 - 72 - 24, pos.getStartPosition(cut, es)); 1.105 - assertEquals("testPositionForSuperConstructorCalls", 1.106 - 80 - 24, pos.getEndPosition(cut, es)); 1.107 - 1.108 - MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); 1.109 - 1.110 - assertEquals("testPositionForSuperConstructorCalls", 1.111 - 72 - 24, pos.getStartPosition(cut, mit)); 1.112 - assertEquals("testPositionForSuperConstructorCalls", 1.113 - 79 - 24, pos.getEndPosition(cut, mit)); 1.114 - 1.115 - assertEquals("testPositionForSuperConstructorCalls", 1.116 - 72 - 24, pos.getStartPosition(cut, mit.getMethodSelect())); 1.117 - assertEquals("testPositionForSuperConstructorCalls", 1.118 - 77 - 24, pos.getEndPosition(cut, mit.getMethodSelect())); 1.119 - 1.120 - } 1.121 - 1.122 - public void testPositionForEnumModifiers() throws IOException { 1.123 - 1.124 - String code = "package test; public enum Test {A;}"; 1.125 - 1.126 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.127 - null, Arrays.asList(new MyFileObject(code))); 1.128 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.129 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 1.130 - 1.131 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.132 - ModifiersTree mt = clazz.getModifiers(); 1.133 - 1.134 - assertEquals("testPositionForEnumModifiers", 1.135 - 38 - 24, pos.getStartPosition(cut, mt)); 1.136 - assertEquals("testPositionForEnumModifiers", 1.137 - 44 - 24, pos.getEndPosition(cut, mt)); 1.138 - } 1.139 - 1.140 - public void testNewClassWithEnclosing() throws IOException { 1.141 - 1.142 - 1.143 - String code = "package test; class Test { " + 1.144 - "class d {} private void method() { " + 1.145 - "Object o = Test.this.new d(); } }"; 1.146 - 1.147 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.148 - null, Arrays.asList(new MyFileObject(code))); 1.149 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.150 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 1.151 - 1.152 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.153 - ExpressionTree est = 1.154 - ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); 1.155 - 1.156 - assertEquals("testNewClassWithEnclosing", 1.157 - 97 - 24, pos.getStartPosition(cut, est)); 1.158 - assertEquals("testNewClassWithEnclosing", 1.159 - 114 - 24, pos.getEndPosition(cut, est)); 1.160 - } 1.161 - 1.162 - public void testPreferredPositionForBinaryOp() throws IOException { 1.163 - 1.164 - String code = "package test; public class Test {" + 1.165 - "private void test() {" + 1.166 - "Object o = null; boolean b = o != null && o instanceof String;" + 1.167 - "} private Test() {}}"; 1.168 - 1.169 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.170 - null, Arrays.asList(new MyFileObject(code))); 1.171 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.172 - 1.173 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.174 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 1.175 - VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); 1.176 - BinaryTree cond = (BinaryTree) condSt.getInitializer(); 1.177 - 1.178 - JCTree condJC = (JCTree) cond; 1.179 - 1.180 - assertEquals("testNewClassWithEnclosing", 1.181 - 117 - 24, condJC.pos); 1.182 - } 1.183 - 1.184 - public void testPositionBrokenSource126732a() throws IOException { 1.185 - String[] commands = new String[]{ 1.186 - "return Runnable()", 1.187 - "do { } while (true)", 1.188 - "throw UnsupportedOperationException()", 1.189 - "assert true", 1.190 - "1 + 1",}; 1.191 - 1.192 - for (String command : commands) { 1.193 - 1.194 - String code = "package test;\n" 1.195 - + "public class Test {\n" 1.196 - + " public static void test() {\n" 1.197 - + " " + command + " {\n" 1.198 - + " new Runnable() {\n" 1.199 - + " };\n" 1.200 - + " }\n" 1.201 - + "}"; 1.202 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 1.203 - null, null, Arrays.asList(new MyFileObject(code))); 1.204 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.205 - 1.206 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.207 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 1.208 - List<? extends StatementTree> statements = 1.209 - method.getBody().getStatements(); 1.210 - 1.211 - StatementTree ret = statements.get(0); 1.212 - StatementTree block = statements.get(1); 1.213 - 1.214 - Trees t = Trees.instance(ct); 1.215 - int len = code.indexOf(command + " {") + (command + " ").length(); 1.216 - assertEquals(command, len, 1.217 - t.getSourcePositions().getEndPosition(cut, ret)); 1.218 - assertEquals(command, len, 1.219 - t.getSourcePositions().getStartPosition(cut, block)); 1.220 - } 1.221 - } 1.222 - 1.223 - public void testPositionBrokenSource126732b() throws IOException { 1.224 - String[] commands = new String[]{ 1.225 - "break", 1.226 - "break A", 1.227 - "continue ", 1.228 - "continue A",}; 1.229 - 1.230 - for (String command : commands) { 1.231 - 1.232 - String code = "package test;\n" 1.233 - + "public class Test {\n" 1.234 - + " public static void test() {\n" 1.235 - + " while (true) {\n" 1.236 - + " " + command + " {\n" 1.237 - + " new Runnable() {\n" 1.238 - + " };\n" 1.239 - + " }\n" 1.240 - + " }\n" 1.241 - + "}"; 1.242 - 1.243 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 1.244 - null, null, Arrays.asList(new MyFileObject(code))); 1.245 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.246 - 1.247 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.248 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 1.249 - List<? extends StatementTree> statements = 1.250 - ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); 1.251 - 1.252 - StatementTree ret = statements.get(0); 1.253 - StatementTree block = statements.get(1); 1.254 - 1.255 - Trees t = Trees.instance(ct); 1.256 - int len = code.indexOf(command + " {") + (command + " ").length(); 1.257 - assertEquals(command, len, 1.258 - t.getSourcePositions().getEndPosition(cut, ret)); 1.259 - assertEquals(command, len, 1.260 - t.getSourcePositions().getStartPosition(cut, block)); 1.261 - } 1.262 - } 1.263 - 1.264 - public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { 1.265 - 1.266 - String code = "package test; class Test { " + 1.267 - "private void method() { " + 1.268 - "java.util.Set<String> s = null; for (a : s) {} } }"; 1.269 - 1.270 - final List<Diagnostic<? extends JavaFileObject>> errors = 1.271 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 1.272 - 1.273 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 1.274 - new DiagnosticListener<JavaFileObject>() { 1.275 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.276 - errors.add(diagnostic); 1.277 - } 1.278 - }, null, null, Arrays.asList(new MyFileObject(code))); 1.279 - 1.280 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.281 - 1.282 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.283 - StatementTree forStatement = 1.284 - ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); 1.285 - 1.286 - assertEquals("testErrorRecoveryForEnhancedForLoop142381", 1.287 - Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); 1.288 - assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); 1.289 - } 1.290 - 1.291 - public void testPositionAnnotationNoPackage187551() throws IOException { 1.292 - 1.293 - String code = "\n@interface Test {}"; 1.294 - 1.295 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.296 - null, Arrays.asList(new MyFileObject(code))); 1.297 - 1.298 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.299 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.300 - Trees t = Trees.instance(ct); 1.301 - 1.302 - assertEquals("testPositionAnnotationNoPackage187551", 1.303 - 1, t.getSourcePositions().getStartPosition(cut, clazz)); 1.304 - } 1.305 - 1.306 - public void testPositionsSane() throws IOException { 1.307 - performPositionsSanityTest("package test; class Test { " + 1.308 - "private void method() { " + 1.309 - "java.util.List<? extends java.util.List<? extends String>> l; " + 1.310 - "} }"); 1.311 - performPositionsSanityTest("package test; class Test { " + 1.312 - "private void method() { " + 1.313 - "java.util.List<? super java.util.List<? super String>> l; " + 1.314 - "} }"); 1.315 - performPositionsSanityTest("package test; class Test { " + 1.316 - "private void method() { " + 1.317 - "java.util.List<? super java.util.List<?>> l; } }"); 1.318 - } 1.319 - 1.320 - private void performPositionsSanityTest(String code) throws IOException { 1.321 - 1.322 - final List<Diagnostic<? extends JavaFileObject>> errors = 1.323 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 1.324 - 1.325 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 1.326 - new DiagnosticListener<JavaFileObject>() { 1.327 - 1.328 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.329 - errors.add(diagnostic); 1.330 - } 1.331 - }, null, null, Arrays.asList(new MyFileObject(code))); 1.332 - 1.333 - final CompilationUnitTree cut = ct.parse().iterator().next(); 1.334 - final Trees trees = Trees.instance(ct); 1.335 - 1.336 - new TreeScanner<Void, Void>() { 1.337 - 1.338 - private long parentStart = 0; 1.339 - private long parentEnd = Integer.MAX_VALUE; 1.340 - 1.341 - @Override 1.342 - public Void scan(Tree node, Void p) { 1.343 - if (node == null) { 1.344 - return null; 1.345 - } 1.346 - 1.347 - long start = trees.getSourcePositions().getStartPosition(cut, node); 1.348 - 1.349 - if (start == (-1)) { 1.350 - return null; //synthetic tree 1.351 - } 1.352 - assertTrue(node.toString() + ":" + start + "/" + parentStart, 1.353 - parentStart <= start); 1.354 - 1.355 - long prevParentStart = parentStart; 1.356 - 1.357 - parentStart = start; 1.358 - 1.359 - long end = trees.getSourcePositions().getEndPosition(cut, node); 1.360 - 1.361 - assertTrue(node.toString() + ":" + end + "/" + parentEnd, 1.362 - end <= parentEnd); 1.363 - 1.364 - long prevParentEnd = parentEnd; 1.365 - 1.366 - parentEnd = end; 1.367 - 1.368 - super.scan(node, p); 1.369 - 1.370 - parentStart = prevParentStart; 1.371 - parentEnd = prevParentEnd; 1.372 - 1.373 - return null; 1.374 - } 1.375 - 1.376 - private void assertTrue(String message, boolean b) { 1.377 - if (!b) fail(message); 1.378 - } 1.379 - }.scan(cut, null); 1.380 - } 1.381 - 1.382 - public void testCorrectWilcardPositions() throws IOException { 1.383 - performWildcardPositionsTest("package test; import java.util.List; " + 1.384 - "class Test { private void method() { List<? extends List<? extends String>> l; } }", 1.385 - 1.386 - Arrays.asList("List<? extends List<? extends String>> l;", 1.387 - "List<? extends List<? extends String>>", 1.388 - "List", 1.389 - "? extends List<? extends String>", 1.390 - "List<? extends String>", 1.391 - "List", 1.392 - "? extends String", 1.393 - "String")); 1.394 - performWildcardPositionsTest("package test; import java.util.List; " + 1.395 - "class Test { private void method() { List<? super List<? super String>> l; } }", 1.396 - 1.397 - Arrays.asList("List<? super List<? super String>> l;", 1.398 - "List<? super List<? super String>>", 1.399 - "List", 1.400 - "? super List<? super String>", 1.401 - "List<? super String>", 1.402 - "List", 1.403 - "? super String", 1.404 - "String")); 1.405 - performWildcardPositionsTest("package test; import java.util.List; " + 1.406 - "class Test { private void method() { List<? super List<?>> l; } }", 1.407 - 1.408 - Arrays.asList("List<? super List<?>> l;", 1.409 - "List<? super List<?>>", 1.410 - "List", 1.411 - "? super List<?>", 1.412 - "List<?>", 1.413 - "List", 1.414 - "?")); 1.415 - performWildcardPositionsTest("package test; import java.util.List; " + 1.416 - "class Test { private void method() { " + 1.417 - "List<? extends List<? extends List<? extends String>>> l; } }", 1.418 - 1.419 - Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", 1.420 - "List<? extends List<? extends List<? extends String>>>", 1.421 - "List", 1.422 - "? extends List<? extends List<? extends String>>", 1.423 - "List<? extends List<? extends String>>", 1.424 - "List", 1.425 - "? extends List<? extends String>", 1.426 - "List<? extends String>", 1.427 - "List", 1.428 - "? extends String", 1.429 - "String")); 1.430 - performWildcardPositionsTest("package test; import java.util.List; " + 1.431 - "class Test { private void method() { " + 1.432 - "List<? extends List<? extends List<? extends String >>> l; } }", 1.433 - Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", 1.434 - "List<? extends List<? extends List<? extends String >>>", 1.435 - "List", 1.436 - "? extends List<? extends List<? extends String >>", 1.437 - "List<? extends List<? extends String >>", 1.438 - "List", 1.439 - "? extends List<? extends String >", 1.440 - "List<? extends String >", 1.441 - "List", 1.442 - "? extends String", 1.443 - "String")); 1.444 - } 1.445 - 1.446 - public void performWildcardPositionsTest(final String code, 1.447 - List<String> golden) throws IOException { 1.448 - 1.449 - final List<Diagnostic<? extends JavaFileObject>> errors = 1.450 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 1.451 - 1.452 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 1.453 - new DiagnosticListener<JavaFileObject>() { 1.454 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.455 - errors.add(diagnostic); 1.456 - } 1.457 - }, null, null, Arrays.asList(new MyFileObject(code))); 1.458 - 1.459 - final CompilationUnitTree cut = ct.parse().iterator().next(); 1.460 - final List<String> content = new LinkedList<String>(); 1.461 - final Trees trees = Trees.instance(ct); 1.462 - 1.463 - new TreeScanner<Void, Void>() { 1.464 - @Override 1.465 - public Void scan(Tree node, Void p) { 1.466 - if (node == null) { 1.467 - return null; 1.468 - } 1.469 - long start = trees.getSourcePositions().getStartPosition(cut, node); 1.470 - 1.471 - if (start == (-1)) { 1.472 - return null; //synthetic tree 1.473 - } 1.474 - long end = trees.getSourcePositions().getEndPosition(cut, node); 1.475 - String s = code.substring((int) start, (int) end); 1.476 - content.add(s); 1.477 - 1.478 - return super.scan(node, p); 1.479 - } 1.480 - }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); 1.481 - 1.482 - assertEquals("performWildcardPositionsTest",golden.toString(), 1.483 - content.toString()); 1.484 - } 1.485 - 1.486 - public void testStartPositionForMethodWithoutModifiers() throws IOException { 1.487 - 1.488 - String code = "package t; class Test { <T> void t() {} }"; 1.489 - 1.490 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.491 - null, Arrays.asList(new MyFileObject(code))); 1.492 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.493 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.494 - MethodTree mt = (MethodTree) clazz.getMembers().get(0); 1.495 - Trees t = Trees.instance(ct); 1.496 - int start = (int) t.getSourcePositions().getStartPosition(cut, mt); 1.497 - int end = (int) t.getSourcePositions().getEndPosition(cut, mt); 1.498 - 1.499 - assertEquals("testStartPositionForMethodWithoutModifiers", 1.500 - "<T> void t() {}", code.substring(start, end)); 1.501 - } 1.502 - 1.503 - public void testStartPositionEnumConstantInit() throws IOException { 1.504 - 1.505 - String code = "package t; enum Test { AAA; }"; 1.506 - 1.507 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.508 - null, Arrays.asList(new MyFileObject(code))); 1.509 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.510 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 1.511 - VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); 1.512 - Trees t = Trees.instance(ct); 1.513 - int start = (int) t.getSourcePositions().getStartPosition(cut, 1.514 - enumAAA.getInitializer()); 1.515 - 1.516 - assertEquals("testStartPositionEnumConstantInit", -1, start); 1.517 - } 1.518 - 1.519 - public void testVariableInIfThen1() throws IOException { 1.520 - 1.521 - String code = "package t; class Test { " + 1.522 - "private static void t(String name) { " + 1.523 - "if (name != null) String nn = name.trim(); } }"; 1.524 - 1.525 - DiagnosticCollector<JavaFileObject> coll = 1.526 - new DiagnosticCollector<JavaFileObject>(); 1.527 - 1.528 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 1.529 - null, Arrays.asList(new MyFileObject(code))); 1.530 - 1.531 - ct.parse(); 1.532 - 1.533 - List<String> codes = new LinkedList<String>(); 1.534 - 1.535 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 1.536 - codes.add(d.getCode()); 1.537 - } 1.538 - 1.539 - assertEquals("testVariableInIfThen1", 1.540 - Arrays.<String>asList("compiler.err.variable.not.allowed"), 1.541 - codes); 1.542 - } 1.543 - 1.544 - public void testVariableInIfThen2() throws IOException { 1.545 - 1.546 - String code = "package t; class Test { " + 1.547 - "private static void t(String name) { " + 1.548 - "if (name != null) class X {} } }"; 1.549 - DiagnosticCollector<JavaFileObject> coll = 1.550 - new DiagnosticCollector<JavaFileObject>(); 1.551 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 1.552 - null, Arrays.asList(new MyFileObject(code))); 1.553 - 1.554 - ct.parse(); 1.555 - 1.556 - List<String> codes = new LinkedList<String>(); 1.557 - 1.558 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 1.559 - codes.add(d.getCode()); 1.560 - } 1.561 - 1.562 - assertEquals("testVariableInIfThen2", 1.563 - Arrays.<String>asList("compiler.err.class.not.allowed"), codes); 1.564 - } 1.565 - 1.566 - public void testVariableInIfThen3() throws IOException { 1.567 - 1.568 - String code = "package t; class Test { "+ 1.569 - "private static void t(String name) { " + 1.570 - "if (name != null) abstract } }"; 1.571 - DiagnosticCollector<JavaFileObject> coll = 1.572 - new DiagnosticCollector<JavaFileObject>(); 1.573 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 1.574 - null, Arrays.asList(new MyFileObject(code))); 1.575 - 1.576 - ct.parse(); 1.577 - 1.578 - List<String> codes = new LinkedList<String>(); 1.579 - 1.580 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 1.581 - codes.add(d.getCode()); 1.582 - } 1.583 - 1.584 - assertEquals("testVariableInIfThen3", 1.585 - Arrays.<String>asList("compiler.err.illegal.start.of.expr"), 1.586 - codes); 1.587 - } 1.588 - 1.589 - //see javac bug #6882235, NB bug #98234: 1.590 - public void testMissingExponent() throws IOException { 1.591 - 1.592 - String code = "\nclass Test { { System.err.println(0e); } }"; 1.593 - 1.594 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.595 - null, Arrays.asList(new MyFileObject(code))); 1.596 - 1.597 - assertNotNull(ct.parse().iterator().next()); 1.598 - } 1.599 - 1.600 - public void testTryResourcePos() throws IOException { 1.601 - 1.602 - final String code = "package t; class Test { " + 1.603 - "{ try (java.io.InputStream in = null) { } } }"; 1.604 - 1.605 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.606 - null, Arrays.asList(new MyFileObject(code))); 1.607 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.608 - 1.609 - new TreeScanner<Void, Void>() { 1.610 - @Override 1.611 - public Void visitVariable(VariableTree node, Void p) { 1.612 - if ("in".contentEquals(node.getName())) { 1.613 - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 1.614 - System.out.println(node.getName() + "," + var.pos); 1.615 - assertEquals("testTryResourcePos", "in = null) { } } }", 1.616 - code.substring(var.pos)); 1.617 - } 1.618 - return super.visitVariable(node, p); 1.619 - } 1.620 - }.scan(cut, null); 1.621 - } 1.622 - 1.623 - public void testVarPos() throws IOException { 1.624 - 1.625 - final String code = "package t; class Test { " + 1.626 - "{ java.io.InputStream in = null; } }"; 1.627 - 1.628 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 1.629 - null, Arrays.asList(new MyFileObject(code))); 1.630 - CompilationUnitTree cut = ct.parse().iterator().next(); 1.631 - 1.632 - new TreeScanner<Void, Void>() { 1.633 - 1.634 - @Override 1.635 - public Void visitVariable(VariableTree node, Void p) { 1.636 - if ("in".contentEquals(node.getName())) { 1.637 - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 1.638 - assertEquals("testVarPos","in = null; } }", 1.639 - code.substring(var.pos)); 1.640 - } 1.641 - return super.visitVariable(node, p); 1.642 - } 1.643 - }.scan(cut, null); 1.644 - } 1.645 - 1.646 - void testsNotWorking() throws IOException { 1.647 - 1.648 - // Fails with nb-javac, needs further investigation 1.649 - testPositionBrokenSource126732a(); 1.650 - testPositionBrokenSource126732b(); 1.651 - 1.652 - // Fails, these tests yet to be addressed 1.653 - testVariableInIfThen1(); 1.654 - testVariableInIfThen2(); 1.655 - testPositionForEnumModifiers(); 1.656 - testStartPositionEnumConstantInit(); 1.657 - } 1.658 - void testPositions() throws IOException { 1.659 - testPositionsSane(); 1.660 - testCorrectWilcardPositions(); 1.661 - testPositionAnnotationNoPackage187551(); 1.662 - testPositionForSuperConstructorCalls(); 1.663 - testPreferredPositionForBinaryOp(); 1.664 - testStartPositionForMethodWithoutModifiers(); 1.665 - testVarPos(); 1.666 - testVariableInIfThen3(); 1.667 - testTryResourcePos(); 1.668 - } 1.669 - 1.670 - public static void main(String... args) throws IOException { 1.671 - JavacParserTest jpt = new JavacParserTest("JavacParserTest"); 1.672 - jpt.testPositions(); 1.673 - System.out.println("PASS"); 1.674 - } 1.675 -} 1.676 - 1.677 -abstract class TestCase { 1.678 - 1.679 - void assertEquals(String message, int i, int pos) { 1.680 - if (i != pos) { 1.681 - fail(message); 1.682 - } 1.683 - } 1.684 - 1.685 - void assertFalse(String message, boolean empty) { 1.686 - throw new UnsupportedOperationException("Not yet implemented"); 1.687 - } 1.688 - 1.689 - void assertEquals(String message, int i, long l) { 1.690 - if (i != l) { 1.691 - fail(message + ":" + i + ":" + l); 1.692 - } 1.693 - } 1.694 - 1.695 - void assertEquals(String message, Object o1, Object o2) { 1.696 - System.out.println(o1); 1.697 - System.out.println(o2); 1.698 - if (o1 != null && o2 != null && !o1.equals(o2)) { 1.699 - fail(message); 1.700 - } 1.701 - if (o1 == null && o2 != null) { 1.702 - fail(message); 1.703 - } 1.704 - } 1.705 - 1.706 - void assertNotNull(Object o) { 1.707 - if (o == null) { 1.708 - fail(); 1.709 - } 1.710 - } 1.711 - 1.712 - void fail() { 1.713 - fail("test failed"); 1.714 - } 1.715 - 1.716 - void fail(String message) { 1.717 - throw new RuntimeException(message); 1.718 - } 1.719 -}