Mon, 12 Dec 2011 10:08:26 -0800
Merge
test/tools/javac/parser/netbeans/JavacParserTest.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Dec 06 20:26:09 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Dec 12 10:08:26 2011 -0800 1.3 @@ -1143,49 +1143,49 @@ 1.4 // typeArgs saved for next loop iteration. 1.5 t = toP(F.at(pos).Select(t, ident())); 1.6 break; 1.7 - case LT: 1.8 - if ((mode & (TYPE | NOPARAMS)) == 0) { 1.9 - //could be an unbound method reference whose qualifier 1.10 - //is a generic type i.e. A<S>#m 1.11 - mode = EXPR | TYPE; 1.12 - JCTree.Tag op = JCTree.Tag.LT; 1.13 - int pos1 = token.pos; 1.14 - nextToken(); 1.15 - mode |= EXPR | TYPE | TYPEARG; 1.16 - JCExpression t1 = term3(); 1.17 - if ((mode & TYPE) != 0 && 1.18 - (token.kind == COMMA || token.kind == GT)) { 1.19 - mode = TYPE; 1.20 - ListBuffer<JCExpression> args = new ListBuffer<JCExpression>(); 1.21 - args.append(t1); 1.22 - while (token.kind == COMMA) { 1.23 - nextToken(); 1.24 - args.append(typeArgument()); 1.25 - } 1.26 - accept(GT); 1.27 - t = toP(F.at(pos1).TypeApply(t, args.toList())); 1.28 - checkGenerics(); 1.29 - while (token.kind == DOT) { 1.30 - nextToken(); 1.31 - mode = TYPE; 1.32 - t = toP(F.at(token.pos).Select(t, ident())); 1.33 - t = typeArgumentsOpt(t); 1.34 - } 1.35 - if (token.kind != HASH) { 1.36 - //method reference expected here 1.37 - t = illegal(); 1.38 - } 1.39 - mode = EXPR; 1.40 - break; 1.41 - } else if ((mode & EXPR) != 0) { 1.42 - //rollback - it was a binary expression 1.43 - mode = EXPR; 1.44 - JCExpression e = term2Rest(t1, TreeInfo.shiftPrec); 1.45 - t = F.at(pos1).Binary(op, t, e); 1.46 - t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec))); 1.47 - } 1.48 - } 1.49 - break loop; 1.50 +// case LT: 1.51 +// if ((mode & (TYPE | NOPARAMS)) == 0) { 1.52 +// //could be an unbound method reference whose qualifier 1.53 +// //is a generic type i.e. A<S>#m 1.54 +// mode = EXPR | TYPE; 1.55 +// JCTree.Tag op = JCTree.Tag.LT; 1.56 +// int pos1 = token.pos; 1.57 +// nextToken(); 1.58 +// mode |= EXPR | TYPE | TYPEARG; 1.59 +// JCExpression t1 = term3(); 1.60 +// if ((mode & TYPE) != 0 && 1.61 +// (token.kind == COMMA || token.kind == GT)) { 1.62 +// mode = TYPE; 1.63 +// ListBuffer<JCExpression> args = new ListBuffer<JCExpression>(); 1.64 +// args.append(t1); 1.65 +// while (token.kind == COMMA) { 1.66 +// nextToken(); 1.67 +// args.append(typeArgument()); 1.68 +// } 1.69 +// accept(GT); 1.70 +// t = toP(F.at(pos1).TypeApply(t, args.toList())); 1.71 +// checkGenerics(); 1.72 +// while (token.kind == DOT) { 1.73 +// nextToken(); 1.74 +// mode = TYPE; 1.75 +// t = toP(F.at(token.pos).Select(t, ident())); 1.76 +// t = typeArgumentsOpt(t); 1.77 +// } 1.78 +// if (token.kind != HASH) { 1.79 +// //method reference expected here 1.80 +// t = illegal(); 1.81 +// } 1.82 +// mode = EXPR; 1.83 +// break; 1.84 +// } else if ((mode & EXPR) != 0) { 1.85 +// //rollback - it was a binary expression 1.86 +// mode = EXPR; 1.87 +// JCExpression e = term2Rest(t1, TreeInfo.shiftPrec); 1.88 +// t = F.at(pos1).Binary(op, t, e); 1.89 +// t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec))); 1.90 +// } 1.91 +// } 1.92 +// break loop; 1.93 default: 1.94 break loop; 1.95 } 1.96 @@ -2604,7 +2604,7 @@ 1.97 * @param mods The modifiers starting the class declaration 1.98 * @param dc The documentation comment for the class, or null. 1.99 */ 1.100 - JCClassDecl classDeclaration(JCModifiers mods, String dc) { 1.101 + protected JCClassDecl classDeclaration(JCModifiers mods, String dc) { 1.102 int pos = token.pos; 1.103 accept(CLASS); 1.104 Name name = ident(); 1.105 @@ -2633,7 +2633,7 @@ 1.106 * @param mods The modifiers starting the interface declaration 1.107 * @param dc The documentation comment for the interface, or null. 1.108 */ 1.109 - JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { 1.110 + protected JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { 1.111 int pos = token.pos; 1.112 accept(INTERFACE); 1.113 Name name = ident(); 1.114 @@ -2656,7 +2656,7 @@ 1.115 * @param mods The modifiers starting the enum declaration 1.116 * @param dc The documentation comment for the enum, or null. 1.117 */ 1.118 - JCClassDecl enumDeclaration(JCModifiers mods, String dc) { 1.119 + protected JCClassDecl enumDeclaration(JCModifiers mods, String dc) { 1.120 int pos = token.pos; 1.121 accept(ENUM); 1.122 Name name = ident(); 1.123 @@ -2878,7 +2878,7 @@ 1.124 * ConstructorDeclaratorRest = 1.125 * "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody 1.126 */ 1.127 - JCTree methodDeclaratorRest(int pos, 1.128 + protected JCTree methodDeclaratorRest(int pos, 1.129 JCModifiers mods, 1.130 JCExpression type, 1.131 Name name,
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T7120266.java Mon Dec 12 10:08:26 2011 -0800 2.3 @@ -0,0 +1,34 @@ 2.4 +/* 2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 7120266 2.30 + * @summary javac fails to compile hotspot code 2.31 + * @compile T7120266.java 2.32 + */ 2.33 + 2.34 +class T7120266 { 2.35 + void test(int i, int len) { that(i < len, "oopmap"); } 2.36 + void that(boolean b, String s) { }; 2.37 +}
3.1 --- a/test/tools/javac/lambda/MethodReferenceParserTest.java Tue Dec 06 20:26:09 2011 -0800 3.2 +++ b/test/tools/javac/lambda/MethodReferenceParserTest.java Mon Dec 12 10:08:26 2011 -0800 3.3 @@ -24,6 +24,7 @@ 3.4 /* 3.5 * @test 3.6 * @bug 7115052 3.7 + * @ignore 7120266 3.8 * @summary Add parser support for method references 3.9 */ 3.10
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/parser/JavacParserTest.java Mon Dec 12 10:08:26 2011 -0800 4.3 @@ -0,0 +1,883 @@ 4.4 +/* 4.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 7073631 4.30 + * @summary tests error and diagnostics positions 4.31 + * @author Jan Lahoda 4.32 + */ 4.33 + 4.34 +import com.sun.source.tree.BinaryTree; 4.35 +import com.sun.source.tree.BlockTree; 4.36 +import com.sun.source.tree.ClassTree; 4.37 +import com.sun.source.tree.CompilationUnitTree; 4.38 +import com.sun.source.tree.ErroneousTree; 4.39 +import com.sun.source.tree.ExpressionStatementTree; 4.40 +import com.sun.source.tree.ExpressionTree; 4.41 +import com.sun.source.tree.MethodInvocationTree; 4.42 +import com.sun.source.tree.MethodTree; 4.43 +import com.sun.source.tree.ModifiersTree; 4.44 +import com.sun.source.tree.StatementTree; 4.45 +import com.sun.source.tree.Tree; 4.46 +import com.sun.source.tree.Tree.Kind; 4.47 +import com.sun.source.tree.VariableTree; 4.48 +import com.sun.source.tree.WhileLoopTree; 4.49 +import com.sun.source.util.SourcePositions; 4.50 +import com.sun.source.util.TreeScanner; 4.51 +import com.sun.source.util.Trees; 4.52 +import com.sun.tools.javac.api.JavacTaskImpl; 4.53 +import com.sun.tools.javac.tree.JCTree; 4.54 +import java.io.IOException; 4.55 +import java.net.URI; 4.56 +import java.util.ArrayList; 4.57 +import java.util.Arrays; 4.58 +import java.util.LinkedList; 4.59 +import java.util.List; 4.60 +import javax.tools.Diagnostic; 4.61 +import javax.tools.DiagnosticCollector; 4.62 +import javax.tools.DiagnosticListener; 4.63 +import javax.tools.JavaCompiler; 4.64 +import javax.tools.JavaFileObject; 4.65 +import javax.tools.SimpleJavaFileObject; 4.66 +import javax.tools.ToolProvider; 4.67 + 4.68 +public class JavacParserTest extends TestCase { 4.69 + final JavaCompiler tool; 4.70 + public JavacParserTest(String testName) { 4.71 + tool = ToolProvider.getSystemJavaCompiler(); 4.72 + System.out.println("java.home=" + System.getProperty("java.home")); 4.73 + } 4.74 + 4.75 + static class MyFileObject extends SimpleJavaFileObject { 4.76 + 4.77 + private String text; 4.78 + 4.79 + public MyFileObject(String text) { 4.80 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 4.81 + this.text = text; 4.82 + } 4.83 + 4.84 + @Override 4.85 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 4.86 + return text; 4.87 + } 4.88 + } 4.89 + /* 4.90 + * converts Windows to Unix style LFs for comparing strings 4.91 + */ 4.92 + private String normalize(String in) { 4.93 + return in.replace(System.getProperty("line.separator"), "\n"); 4.94 + } 4.95 + 4.96 + public CompilationUnitTree getCompilationUnitTree(String code) throws IOException { 4.97 + 4.98 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.99 + null, Arrays.asList(new MyFileObject(code))); 4.100 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.101 + return cut; 4.102 + } 4.103 + 4.104 + public List<String> getErroneousTreeValues(ErroneousTree node) { 4.105 + 4.106 + List<String> values = new ArrayList<>(); 4.107 + if (node.getErrorTrees() != null) { 4.108 + for (Tree t : node.getErrorTrees()) { 4.109 + values.add(t.toString()); 4.110 + } 4.111 + } else { 4.112 + throw new RuntimeException("ERROR: No Erroneous tree " 4.113 + + "has been created."); 4.114 + } 4.115 + return values; 4.116 + } 4.117 + 4.118 + public void testPositionForSuperConstructorCalls() throws IOException { 4.119 + assert tool != null; 4.120 + 4.121 + String code = "package test; public class Test {public Test() {super();}}"; 4.122 + 4.123 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.124 + null, Arrays.asList(new MyFileObject(code))); 4.125 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.126 + SourcePositions pos = Trees.instance(ct).getSourcePositions(); 4.127 + 4.128 + MethodTree method = 4.129 + (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); 4.130 + ExpressionStatementTree es = 4.131 + (ExpressionStatementTree) method.getBody().getStatements().get(0); 4.132 + 4.133 + final int esStartPos = code.indexOf(es.toString()); 4.134 + final int esEndPos = esStartPos + es.toString().length(); 4.135 + assertEquals("testPositionForSuperConstructorCalls", 4.136 + esStartPos, pos.getStartPosition(cut, es)); 4.137 + assertEquals("testPositionForSuperConstructorCalls", 4.138 + esEndPos, pos.getEndPosition(cut, es)); 4.139 + 4.140 + MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); 4.141 + 4.142 + final int mitStartPos = code.indexOf(mit.toString()); 4.143 + final int mitEndPos = mitStartPos + mit.toString().length(); 4.144 + assertEquals("testPositionForSuperConstructorCalls", 4.145 + mitStartPos, pos.getStartPosition(cut, mit)); 4.146 + assertEquals("testPositionForSuperConstructorCalls", 4.147 + mitEndPos, pos.getEndPosition(cut, mit)); 4.148 + 4.149 + final int methodStartPos = mitStartPos; 4.150 + final int methodEndPos = methodStartPos + mit.getMethodSelect().toString().length(); 4.151 + assertEquals("testPositionForSuperConstructorCalls", 4.152 + methodStartPos, pos.getStartPosition(cut, mit.getMethodSelect())); 4.153 + assertEquals("testPositionForSuperConstructorCalls", 4.154 + methodEndPos, pos.getEndPosition(cut, mit.getMethodSelect())); 4.155 + 4.156 + } 4.157 + 4.158 + public void testPositionForEnumModifiers() throws IOException { 4.159 + 4.160 + String code = "package test; public enum Test {A;}"; 4.161 + 4.162 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.163 + null, Arrays.asList(new MyFileObject(code))); 4.164 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.165 + SourcePositions pos = Trees.instance(ct).getSourcePositions(); 4.166 + 4.167 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.168 + ModifiersTree mt = clazz.getModifiers(); 4.169 + 4.170 + assertEquals("testPositionForEnumModifiers", 4.171 + 38 - 24, pos.getStartPosition(cut, mt)); 4.172 + assertEquals("testPositionForEnumModifiers", 4.173 + 44 - 24, pos.getEndPosition(cut, mt)); 4.174 + } 4.175 + 4.176 + public void testNewClassWithEnclosing() throws IOException { 4.177 + 4.178 + 4.179 + String code = "package test; class Test { " + 4.180 + "class d {} private void method() { " + 4.181 + "Object o = Test.this.new d(); } }"; 4.182 + 4.183 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.184 + null, Arrays.asList(new MyFileObject(code))); 4.185 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.186 + SourcePositions pos = Trees.instance(ct).getSourcePositions(); 4.187 + 4.188 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.189 + ExpressionTree est = 4.190 + ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); 4.191 + 4.192 + assertEquals("testNewClassWithEnclosing", 4.193 + 97 - 24, pos.getStartPosition(cut, est)); 4.194 + assertEquals("testNewClassWithEnclosing", 4.195 + 114 - 24, pos.getEndPosition(cut, est)); 4.196 + } 4.197 + 4.198 + public void testPreferredPositionForBinaryOp() throws IOException { 4.199 + 4.200 + String code = "package test; public class Test {" 4.201 + + "private void test() {" 4.202 + + "Object o = null; boolean b = o != null && o instanceof String;" 4.203 + + "} private Test() {}}"; 4.204 + 4.205 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.206 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.207 + MethodTree method = (MethodTree) clazz.getMembers().get(0); 4.208 + VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); 4.209 + BinaryTree cond = (BinaryTree) condSt.getInitializer(); 4.210 + 4.211 + JCTree condJC = (JCTree) cond; 4.212 + int condStartPos = code.indexOf("&&"); 4.213 + assertEquals("testPreferredPositionForBinaryOp", 4.214 + condStartPos, condJC.pos); 4.215 + } 4.216 + 4.217 + public void testPositionBrokenSource126732a() throws IOException { 4.218 + String[] commands = new String[]{ 4.219 + "return Runnable()", 4.220 + "do { } while (true)", 4.221 + "throw UnsupportedOperationException()", 4.222 + "assert true", 4.223 + "1 + 1",}; 4.224 + 4.225 + for (String command : commands) { 4.226 + 4.227 + String code = "package test;\n" 4.228 + + "public class Test {\n" 4.229 + + " public static void test() {\n" 4.230 + + " " + command + " {\n" 4.231 + + " new Runnable() {\n" 4.232 + + " };\n" 4.233 + + " }\n" 4.234 + + "}"; 4.235 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 4.236 + null, null, Arrays.asList(new MyFileObject(code))); 4.237 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.238 + 4.239 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.240 + MethodTree method = (MethodTree) clazz.getMembers().get(0); 4.241 + List<? extends StatementTree> statements = 4.242 + method.getBody().getStatements(); 4.243 + 4.244 + StatementTree ret = statements.get(0); 4.245 + StatementTree block = statements.get(1); 4.246 + 4.247 + Trees t = Trees.instance(ct); 4.248 + int len = code.indexOf(command + " {") + (command + " ").length(); 4.249 + assertEquals(command, len, 4.250 + t.getSourcePositions().getEndPosition(cut, ret)); 4.251 + assertEquals(command, len, 4.252 + t.getSourcePositions().getStartPosition(cut, block)); 4.253 + } 4.254 + } 4.255 + 4.256 + public void testPositionBrokenSource126732b() throws IOException { 4.257 + String[] commands = new String[]{ 4.258 + "break", 4.259 + "break A", 4.260 + "continue ", 4.261 + "continue A",}; 4.262 + 4.263 + for (String command : commands) { 4.264 + 4.265 + String code = "package test;\n" 4.266 + + "public class Test {\n" 4.267 + + " public static void test() {\n" 4.268 + + " while (true) {\n" 4.269 + + " " + command + " {\n" 4.270 + + " new Runnable() {\n" 4.271 + + " };\n" 4.272 + + " }\n" 4.273 + + " }\n" 4.274 + + "}"; 4.275 + 4.276 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 4.277 + null, null, Arrays.asList(new MyFileObject(code))); 4.278 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.279 + 4.280 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.281 + MethodTree method = (MethodTree) clazz.getMembers().get(0); 4.282 + List<? extends StatementTree> statements = 4.283 + ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); 4.284 + 4.285 + StatementTree ret = statements.get(0); 4.286 + StatementTree block = statements.get(1); 4.287 + 4.288 + Trees t = Trees.instance(ct); 4.289 + int len = code.indexOf(command + " {") + (command + " ").length(); 4.290 + assertEquals(command, len, 4.291 + t.getSourcePositions().getEndPosition(cut, ret)); 4.292 + assertEquals(command, len, 4.293 + t.getSourcePositions().getStartPosition(cut, block)); 4.294 + } 4.295 + } 4.296 + 4.297 + public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { 4.298 + 4.299 + String code = "package test; class Test { " + 4.300 + "private void method() { " + 4.301 + "java.util.Set<String> s = null; for (a : s) {} } }"; 4.302 + 4.303 + final List<Diagnostic<? extends JavaFileObject>> errors = 4.304 + new LinkedList<Diagnostic<? extends JavaFileObject>>(); 4.305 + 4.306 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 4.307 + new DiagnosticListener<JavaFileObject>() { 4.308 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 4.309 + errors.add(diagnostic); 4.310 + } 4.311 + }, null, null, Arrays.asList(new MyFileObject(code))); 4.312 + 4.313 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.314 + 4.315 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.316 + StatementTree forStatement = 4.317 + ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); 4.318 + 4.319 + assertEquals("testErrorRecoveryForEnhancedForLoop142381", 4.320 + Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); 4.321 + assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); 4.322 + } 4.323 + 4.324 + public void testPositionAnnotationNoPackage187551() throws IOException { 4.325 + 4.326 + String code = "\n@interface Test {}"; 4.327 + 4.328 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.329 + null, Arrays.asList(new MyFileObject(code))); 4.330 + 4.331 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.332 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.333 + Trees t = Trees.instance(ct); 4.334 + 4.335 + assertEquals("testPositionAnnotationNoPackage187551", 4.336 + 1, t.getSourcePositions().getStartPosition(cut, clazz)); 4.337 + } 4.338 + 4.339 + public void testPositionsSane() throws IOException { 4.340 + performPositionsSanityTest("package test; class Test { " + 4.341 + "private void method() { " + 4.342 + "java.util.List<? extends java.util.List<? extends String>> l; " + 4.343 + "} }"); 4.344 + performPositionsSanityTest("package test; class Test { " + 4.345 + "private void method() { " + 4.346 + "java.util.List<? super java.util.List<? super String>> l; " + 4.347 + "} }"); 4.348 + performPositionsSanityTest("package test; class Test { " + 4.349 + "private void method() { " + 4.350 + "java.util.List<? super java.util.List<?>> l; } }"); 4.351 + } 4.352 + 4.353 + private void performPositionsSanityTest(String code) throws IOException { 4.354 + 4.355 + final List<Diagnostic<? extends JavaFileObject>> errors = 4.356 + new LinkedList<Diagnostic<? extends JavaFileObject>>(); 4.357 + 4.358 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 4.359 + new DiagnosticListener<JavaFileObject>() { 4.360 + 4.361 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 4.362 + errors.add(diagnostic); 4.363 + } 4.364 + }, null, null, Arrays.asList(new MyFileObject(code))); 4.365 + 4.366 + final CompilationUnitTree cut = ct.parse().iterator().next(); 4.367 + final Trees trees = Trees.instance(ct); 4.368 + 4.369 + new TreeScanner<Void, Void>() { 4.370 + 4.371 + private long parentStart = 0; 4.372 + private long parentEnd = Integer.MAX_VALUE; 4.373 + 4.374 + @Override 4.375 + public Void scan(Tree node, Void p) { 4.376 + if (node == null) { 4.377 + return null; 4.378 + } 4.379 + 4.380 + long start = trees.getSourcePositions().getStartPosition(cut, node); 4.381 + 4.382 + if (start == (-1)) { 4.383 + return null; //synthetic tree 4.384 + } 4.385 + assertTrue(node.toString() + ":" + start + "/" + parentStart, 4.386 + parentStart <= start); 4.387 + 4.388 + long prevParentStart = parentStart; 4.389 + 4.390 + parentStart = start; 4.391 + 4.392 + long end = trees.getSourcePositions().getEndPosition(cut, node); 4.393 + 4.394 + assertTrue(node.toString() + ":" + end + "/" + parentEnd, 4.395 + end <= parentEnd); 4.396 + 4.397 + long prevParentEnd = parentEnd; 4.398 + 4.399 + parentEnd = end; 4.400 + 4.401 + super.scan(node, p); 4.402 + 4.403 + parentStart = prevParentStart; 4.404 + parentEnd = prevParentEnd; 4.405 + 4.406 + return null; 4.407 + } 4.408 + 4.409 + private void assertTrue(String message, boolean b) { 4.410 + if (!b) fail(message); 4.411 + } 4.412 + }.scan(cut, null); 4.413 + } 4.414 + 4.415 + public void testCorrectWilcardPositions() throws IOException { 4.416 + performWildcardPositionsTest("package test; import java.util.List; " + 4.417 + "class Test { private void method() { List<? extends List<? extends String>> l; } }", 4.418 + 4.419 + Arrays.asList("List<? extends List<? extends String>> l;", 4.420 + "List<? extends List<? extends String>>", 4.421 + "List", 4.422 + "? extends List<? extends String>", 4.423 + "List<? extends String>", 4.424 + "List", 4.425 + "? extends String", 4.426 + "String")); 4.427 + performWildcardPositionsTest("package test; import java.util.List; " + 4.428 + "class Test { private void method() { List<? super List<? super String>> l; } }", 4.429 + 4.430 + Arrays.asList("List<? super List<? super String>> l;", 4.431 + "List<? super List<? super String>>", 4.432 + "List", 4.433 + "? super List<? super String>", 4.434 + "List<? super String>", 4.435 + "List", 4.436 + "? super String", 4.437 + "String")); 4.438 + performWildcardPositionsTest("package test; import java.util.List; " + 4.439 + "class Test { private void method() { List<? super List<?>> l; } }", 4.440 + 4.441 + Arrays.asList("List<? super List<?>> l;", 4.442 + "List<? super List<?>>", 4.443 + "List", 4.444 + "? super List<?>", 4.445 + "List<?>", 4.446 + "List", 4.447 + "?")); 4.448 + performWildcardPositionsTest("package test; import java.util.List; " + 4.449 + "class Test { private void method() { " + 4.450 + "List<? extends List<? extends List<? extends String>>> l; } }", 4.451 + 4.452 + Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", 4.453 + "List<? extends List<? extends List<? extends String>>>", 4.454 + "List", 4.455 + "? extends List<? extends List<? extends String>>", 4.456 + "List<? extends List<? extends String>>", 4.457 + "List", 4.458 + "? extends List<? extends String>", 4.459 + "List<? extends String>", 4.460 + "List", 4.461 + "? extends String", 4.462 + "String")); 4.463 + performWildcardPositionsTest("package test; import java.util.List; " + 4.464 + "class Test { private void method() { " + 4.465 + "List<? extends List<? extends List<? extends String >>> l; } }", 4.466 + Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", 4.467 + "List<? extends List<? extends List<? extends String >>>", 4.468 + "List", 4.469 + "? extends List<? extends List<? extends String >>", 4.470 + "List<? extends List<? extends String >>", 4.471 + "List", 4.472 + "? extends List<? extends String >", 4.473 + "List<? extends String >", 4.474 + "List", 4.475 + "? extends String", 4.476 + "String")); 4.477 + } 4.478 + 4.479 + public void performWildcardPositionsTest(final String code, 4.480 + List<String> golden) throws IOException { 4.481 + 4.482 + final List<Diagnostic<? extends JavaFileObject>> errors = 4.483 + new LinkedList<Diagnostic<? extends JavaFileObject>>(); 4.484 + 4.485 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 4.486 + new DiagnosticListener<JavaFileObject>() { 4.487 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 4.488 + errors.add(diagnostic); 4.489 + } 4.490 + }, null, null, Arrays.asList(new MyFileObject(code))); 4.491 + 4.492 + final CompilationUnitTree cut = ct.parse().iterator().next(); 4.493 + final List<String> content = new LinkedList<String>(); 4.494 + final Trees trees = Trees.instance(ct); 4.495 + 4.496 + new TreeScanner<Void, Void>() { 4.497 + @Override 4.498 + public Void scan(Tree node, Void p) { 4.499 + if (node == null) { 4.500 + return null; 4.501 + } 4.502 + long start = trees.getSourcePositions().getStartPosition(cut, node); 4.503 + 4.504 + if (start == (-1)) { 4.505 + return null; //synthetic tree 4.506 + } 4.507 + long end = trees.getSourcePositions().getEndPosition(cut, node); 4.508 + String s = code.substring((int) start, (int) end); 4.509 + content.add(s); 4.510 + 4.511 + return super.scan(node, p); 4.512 + } 4.513 + }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); 4.514 + 4.515 + assertEquals("performWildcardPositionsTest",golden.toString(), 4.516 + content.toString()); 4.517 + } 4.518 + 4.519 + public void testStartPositionForMethodWithoutModifiers() throws IOException { 4.520 + 4.521 + String code = "package t; class Test { <T> void t() {} }"; 4.522 + 4.523 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.524 + null, Arrays.asList(new MyFileObject(code))); 4.525 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.526 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.527 + MethodTree mt = (MethodTree) clazz.getMembers().get(0); 4.528 + Trees t = Trees.instance(ct); 4.529 + int start = (int) t.getSourcePositions().getStartPosition(cut, mt); 4.530 + int end = (int) t.getSourcePositions().getEndPosition(cut, mt); 4.531 + 4.532 + assertEquals("testStartPositionForMethodWithoutModifiers", 4.533 + "<T> void t() {}", code.substring(start, end)); 4.534 + } 4.535 + 4.536 + public void testStartPositionEnumConstantInit() throws IOException { 4.537 + 4.538 + String code = "package t; enum Test { AAA; }"; 4.539 + 4.540 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.541 + null, Arrays.asList(new MyFileObject(code))); 4.542 + CompilationUnitTree cut = ct.parse().iterator().next(); 4.543 + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 4.544 + VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); 4.545 + Trees t = Trees.instance(ct); 4.546 + int start = (int) t.getSourcePositions().getStartPosition(cut, 4.547 + enumAAA.getInitializer()); 4.548 + 4.549 + assertEquals("testStartPositionEnumConstantInit", -1, start); 4.550 + } 4.551 + 4.552 + public void testVariableInIfThen1() throws IOException { 4.553 + 4.554 + String code = "package t; class Test { " + 4.555 + "private static void t(String name) { " + 4.556 + "if (name != null) String nn = name.trim(); } }"; 4.557 + 4.558 + DiagnosticCollector<JavaFileObject> coll = 4.559 + new DiagnosticCollector<JavaFileObject>(); 4.560 + 4.561 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 4.562 + null, Arrays.asList(new MyFileObject(code))); 4.563 + 4.564 + ct.parse(); 4.565 + 4.566 + List<String> codes = new LinkedList<String>(); 4.567 + 4.568 + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 4.569 + codes.add(d.getCode()); 4.570 + } 4.571 + 4.572 + assertEquals("testVariableInIfThen1", 4.573 + Arrays.<String>asList("compiler.err.variable.not.allowed"), 4.574 + codes); 4.575 + } 4.576 + 4.577 + public void testVariableInIfThen2() throws IOException { 4.578 + 4.579 + String code = "package t; class Test { " + 4.580 + "private static void t(String name) { " + 4.581 + "if (name != null) class X {} } }"; 4.582 + DiagnosticCollector<JavaFileObject> coll = 4.583 + new DiagnosticCollector<JavaFileObject>(); 4.584 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 4.585 + null, Arrays.asList(new MyFileObject(code))); 4.586 + 4.587 + ct.parse(); 4.588 + 4.589 + List<String> codes = new LinkedList<String>(); 4.590 + 4.591 + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 4.592 + codes.add(d.getCode()); 4.593 + } 4.594 + 4.595 + assertEquals("testVariableInIfThen2", 4.596 + Arrays.<String>asList("compiler.err.class.not.allowed"), codes); 4.597 + } 4.598 + 4.599 + public void testVariableInIfThen3() throws IOException { 4.600 + 4.601 + String code = "package t; class Test { "+ 4.602 + "private static void t(String name) { " + 4.603 + "if (name != null) abstract } }"; 4.604 + DiagnosticCollector<JavaFileObject> coll = 4.605 + new DiagnosticCollector<JavaFileObject>(); 4.606 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 4.607 + null, Arrays.asList(new MyFileObject(code))); 4.608 + 4.609 + ct.parse(); 4.610 + 4.611 + List<String> codes = new LinkedList<String>(); 4.612 + 4.613 + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 4.614 + codes.add(d.getCode()); 4.615 + } 4.616 + 4.617 + assertEquals("testVariableInIfThen3", 4.618 + Arrays.<String>asList("compiler.err.illegal.start.of.expr"), 4.619 + codes); 4.620 + } 4.621 + 4.622 + //see javac bug #6882235, NB bug #98234: 4.623 + public void testMissingExponent() throws IOException { 4.624 + 4.625 + String code = "\nclass Test { { System.err.println(0e); } }"; 4.626 + 4.627 + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 4.628 + null, Arrays.asList(new MyFileObject(code))); 4.629 + 4.630 + assertNotNull(ct.parse().iterator().next()); 4.631 + } 4.632 + 4.633 + public void testTryResourcePos() throws IOException { 4.634 + 4.635 + final String code = "package t; class Test { " + 4.636 + "{ try (java.io.InputStream in = null) { } } }"; 4.637 + 4.638 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.639 + 4.640 + new TreeScanner<Void, Void>() { 4.641 + @Override 4.642 + public Void visitVariable(VariableTree node, Void p) { 4.643 + if ("in".contentEquals(node.getName())) { 4.644 + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 4.645 + System.out.println(node.getName() + "," + var.pos); 4.646 + assertEquals("testTryResourcePos", "in = null) { } } }", 4.647 + code.substring(var.pos)); 4.648 + } 4.649 + return super.visitVariable(node, p); 4.650 + } 4.651 + }.scan(cut, null); 4.652 + } 4.653 + 4.654 + public void testVarPos() throws IOException { 4.655 + 4.656 + final String code = "package t; class Test { " + 4.657 + "{ java.io.InputStream in = null; } }"; 4.658 + 4.659 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.660 + 4.661 + new TreeScanner<Void, Void>() { 4.662 + 4.663 + @Override 4.664 + public Void visitVariable(VariableTree node, Void p) { 4.665 + if ("in".contentEquals(node.getName())) { 4.666 + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 4.667 + assertEquals("testVarPos","in = null; } }", 4.668 + code.substring(var.pos)); 4.669 + } 4.670 + return super.visitVariable(node, p); 4.671 + } 4.672 + }.scan(cut, null); 4.673 + } 4.674 + 4.675 + // expected erroneous tree: int x = y;(ERROR); 4.676 + public void testOperatorMissingError() throws IOException { 4.677 + 4.678 + String code = "package test; public class ErrorTest { " 4.679 + + "void method() { int x = y z } }"; 4.680 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.681 + final List<String> values = new ArrayList<>(); 4.682 + final List<String> expectedValues = 4.683 + new ArrayList<>(Arrays.asList("[z]")); 4.684 + 4.685 + new TreeScanner<Void, Void>() { 4.686 + 4.687 + @Override 4.688 + public Void visitErroneous(ErroneousTree node, Void p) { 4.689 + 4.690 + values.add(getErroneousTreeValues(node).toString()); 4.691 + return null; 4.692 + 4.693 + } 4.694 + }.scan(cut, null); 4.695 + 4.696 + assertEquals("testSwitchError: The Erroneous tree " 4.697 + + "error values: " + values 4.698 + + " do not match expected error values: " 4.699 + + expectedValues, values, expectedValues); 4.700 + } 4.701 + 4.702 + //expected erroneous tree: String s = (ERROR); 4.703 + public void testMissingParenthesisError() throws IOException { 4.704 + 4.705 + String code = "package test; public class ErrorTest { " 4.706 + + "void f() {String s = new String; } }"; 4.707 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.708 + final List<String> values = new ArrayList<>(); 4.709 + final List<String> expectedValues = 4.710 + new ArrayList<>(Arrays.asList("[new String()]")); 4.711 + 4.712 + new TreeScanner<Void, Void>() { 4.713 + 4.714 + @Override 4.715 + public Void visitErroneous(ErroneousTree node, Void p) { 4.716 + 4.717 + values.add(getErroneousTreeValues(node).toString()); 4.718 + return null; 4.719 + } 4.720 + }.scan(cut, null); 4.721 + 4.722 + assertEquals("testSwitchError: The Erroneous tree " 4.723 + + "error values: " + values 4.724 + + " do not match expected error values: " 4.725 + + expectedValues, values, expectedValues); 4.726 + } 4.727 + 4.728 + //expected erroneous tree: package test; (ERROR)(ERROR) 4.729 + public void testMissingClassError() throws IOException { 4.730 + 4.731 + String code = "package Test; clas ErrorTest { " 4.732 + + "void f() {String s = new String(); } }"; 4.733 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.734 + final List<String> values = new ArrayList<>(); 4.735 + final List<String> expectedValues = 4.736 + new ArrayList<>(Arrays.asList("[, clas]", "[]")); 4.737 + 4.738 + new TreeScanner<Void, Void>() { 4.739 + 4.740 + @Override 4.741 + public Void visitErroneous(ErroneousTree node, Void p) { 4.742 + 4.743 + values.add(getErroneousTreeValues(node).toString()); 4.744 + return null; 4.745 + } 4.746 + }.scan(cut, null); 4.747 + 4.748 + assertEquals("testSwitchError: The Erroneous tree " 4.749 + + "error values: " + values 4.750 + + " do not match expected error values: " 4.751 + + expectedValues, values, expectedValues); 4.752 + } 4.753 + 4.754 + //expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);} 4.755 + public void testSwitchError() throws IOException { 4.756 + 4.757 + String code = "package test; public class ErrorTest { " 4.758 + + "int numDays; void m1(int i) { switchh {i} { case 1: " 4.759 + + "numDays = 31; break; } } }"; 4.760 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.761 + final List<String> values = new ArrayList<>(); 4.762 + final List<String> expectedValues = 4.763 + new ArrayList<>(Arrays.asList("[switchh]", "[i]")); 4.764 + 4.765 + new TreeScanner<Void, Void>() { 4.766 + 4.767 + @Override 4.768 + public Void visitErroneous(ErroneousTree node, Void p) { 4.769 + 4.770 + values.add(getErroneousTreeValues(node).toString()); 4.771 + return null; 4.772 + } 4.773 + }.scan(cut, null); 4.774 + 4.775 + assertEquals("testSwitchError: The Erroneous tree " 4.776 + + "error values: " + values 4.777 + + " do not match expected error values: " 4.778 + + expectedValues, values, expectedValues); 4.779 + } 4.780 + 4.781 + //expected erroneous tree: class ErrorTest {(ERROR) 4.782 + public void testMethodError() throws IOException { 4.783 + 4.784 + String code = "package Test; class ErrorTest { " 4.785 + + "static final void f) {String s = new String(); } }"; 4.786 + CompilationUnitTree cut = getCompilationUnitTree(code); 4.787 + final List<String> values = new ArrayList<>(); 4.788 + final List<String> expectedValues = 4.789 + new ArrayList<>(Arrays.asList("[\nstatic final void f();]")); 4.790 + 4.791 + new TreeScanner<Void, Void>() { 4.792 + 4.793 + @Override 4.794 + public Void visitErroneous(ErroneousTree node, Void p) { 4.795 + 4.796 + values.add(normalize(getErroneousTreeValues(node).toString())); 4.797 + return null; 4.798 + } 4.799 + }.scan(cut, null); 4.800 + 4.801 + assertEquals("testMethodError: The Erroneous tree " 4.802 + + "error value: " + values 4.803 + + " does not match expected error values: " 4.804 + + expectedValues, values, expectedValues); 4.805 + } 4.806 + 4.807 + void testsNotWorking() throws IOException { 4.808 + 4.809 + // Fails with nb-javac, needs further investigation 4.810 + testPositionBrokenSource126732a(); 4.811 + testPositionBrokenSource126732b(); 4.812 + 4.813 + // Fails, these tests yet to be addressed 4.814 + testVariableInIfThen1(); 4.815 + testVariableInIfThen2(); 4.816 + testPositionForEnumModifiers(); 4.817 + testStartPositionEnumConstantInit(); 4.818 + } 4.819 + void testPositions() throws IOException { 4.820 + testPositionsSane(); 4.821 + testCorrectWilcardPositions(); 4.822 + testPositionAnnotationNoPackage187551(); 4.823 + testPositionForSuperConstructorCalls(); 4.824 + testPreferredPositionForBinaryOp(); 4.825 + testStartPositionForMethodWithoutModifiers(); 4.826 + testVarPos(); 4.827 + testVariableInIfThen3(); 4.828 + testMissingExponent(); 4.829 + testTryResourcePos(); 4.830 + testOperatorMissingError(); 4.831 + testMissingParenthesisError(); 4.832 + testMissingClassError(); 4.833 + testSwitchError(); 4.834 + testMethodError(); 4.835 + } 4.836 + 4.837 + public static void main(String... args) throws IOException { 4.838 + JavacParserTest jpt = new JavacParserTest("JavacParserTest"); 4.839 + jpt.testPositions(); 4.840 + System.out.println("PASS"); 4.841 + } 4.842 +} 4.843 + 4.844 +abstract class TestCase { 4.845 + 4.846 + void assertEquals(String message, int i, int pos) { 4.847 + if (i != pos) { 4.848 + fail(message); 4.849 + } 4.850 + } 4.851 + 4.852 + void assertFalse(String message, boolean empty) { 4.853 + throw new UnsupportedOperationException("Not yet implemented"); 4.854 + } 4.855 + 4.856 + void assertEquals(String message, int i, long l) { 4.857 + if (i != l) { 4.858 + fail(message + ":" + i + ":" + l); 4.859 + } 4.860 + } 4.861 + 4.862 + void assertEquals(String message, Object o1, Object o2) { 4.863 + System.out.println(o1); 4.864 + System.out.println(o2); 4.865 + if (o1 != null && o2 != null && !o1.equals(o2)) { 4.866 + fail(message); 4.867 + } 4.868 + if (o1 == null && o2 != null) { 4.869 + fail(message); 4.870 + } 4.871 + } 4.872 + 4.873 + void assertNotNull(Object o) { 4.874 + if (o == null) { 4.875 + fail(); 4.876 + } 4.877 + } 4.878 + 4.879 + void fail() { 4.880 + fail("test failed"); 4.881 + } 4.882 + 4.883 + void fail(String message) { 4.884 + throw new RuntimeException(message); 4.885 + } 4.886 +}
5.1 --- a/test/tools/javac/parser/netbeans/JavacParserTest.java Tue Dec 06 20:26:09 2011 -0800 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,716 +0,0 @@ 5.4 -/* 5.5 - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 5.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 - * 5.8 - * This code is free software; you can redistribute it and/or modify it 5.9 - * under the terms of the GNU General Public License version 2 only, as 5.10 - * published by the Free Software Foundation. 5.11 - * 5.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 - * version 2 for more details (a copy is included in the LICENSE file that 5.16 - * accompanied this code). 5.17 - * 5.18 - * You should have received a copy of the GNU General Public License version 5.19 - * 2 along with this work; if not, write to the Free Software Foundation, 5.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 - * 5.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 - * or visit www.oracle.com if you need additional information or have any 5.24 - * questions. 5.25 - */ 5.26 - 5.27 -/* 5.28 - * @test 5.29 - * @bug 7073631 5.30 - * @summary tests error and diagnostics positions 5.31 - * @author jan.lahoda@oracle.com 5.32 - */ 5.33 - 5.34 -import com.sun.source.tree.BinaryTree; 5.35 -import com.sun.source.tree.BlockTree; 5.36 -import com.sun.source.tree.ClassTree; 5.37 -import com.sun.source.tree.CompilationUnitTree; 5.38 -import com.sun.source.tree.ExpressionStatementTree; 5.39 -import com.sun.source.tree.ExpressionTree; 5.40 -import com.sun.source.tree.MethodInvocationTree; 5.41 -import com.sun.source.tree.MethodTree; 5.42 -import com.sun.source.tree.ModifiersTree; 5.43 -import com.sun.source.tree.StatementTree; 5.44 -import com.sun.source.tree.Tree; 5.45 -import com.sun.source.tree.Tree.Kind; 5.46 -import com.sun.source.tree.VariableTree; 5.47 -import com.sun.source.tree.WhileLoopTree; 5.48 -import com.sun.source.util.SourcePositions; 5.49 -import com.sun.source.util.TreeScanner; 5.50 -import com.sun.source.util.Trees; 5.51 -import com.sun.tools.javac.api.JavacTaskImpl; 5.52 -import com.sun.tools.javac.tree.JCTree; 5.53 -import java.io.IOException; 5.54 -import java.net.URI; 5.55 -import java.util.Arrays; 5.56 -import java.util.LinkedList; 5.57 -import java.util.List; 5.58 -import javax.tools.Diagnostic; 5.59 -import javax.tools.DiagnosticCollector; 5.60 -import javax.tools.DiagnosticListener; 5.61 -import javax.tools.JavaCompiler; 5.62 -import javax.tools.JavaFileObject; 5.63 -import javax.tools.SimpleJavaFileObject; 5.64 -import javax.tools.ToolProvider; 5.65 - 5.66 -public class JavacParserTest extends TestCase { 5.67 - final JavaCompiler tool; 5.68 - public JavacParserTest(String testName) { 5.69 - tool = ToolProvider.getSystemJavaCompiler(); 5.70 - System.out.println("java.home=" + System.getProperty("java.home")); 5.71 - } 5.72 - 5.73 - static class MyFileObject extends SimpleJavaFileObject { 5.74 - 5.75 - private String text; 5.76 - 5.77 - public MyFileObject(String text) { 5.78 - super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 5.79 - this.text = text; 5.80 - } 5.81 - 5.82 - @Override 5.83 - public CharSequence getCharContent(boolean ignoreEncodingErrors) { 5.84 - return text; 5.85 - } 5.86 - } 5.87 - 5.88 - public void testPositionForSuperConstructorCalls() throws IOException { 5.89 - assert tool != null; 5.90 - 5.91 - String code = "package test; public class Test {public Test() {super();}}"; 5.92 - 5.93 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.94 - null, Arrays.asList(new MyFileObject(code))); 5.95 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.96 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 5.97 - 5.98 - MethodTree method = 5.99 - (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); 5.100 - ExpressionStatementTree es = 5.101 - (ExpressionStatementTree) method.getBody().getStatements().get(0); 5.102 - 5.103 - assertEquals("testPositionForSuperConstructorCalls", 5.104 - 72 - 24, pos.getStartPosition(cut, es)); 5.105 - assertEquals("testPositionForSuperConstructorCalls", 5.106 - 80 - 24, pos.getEndPosition(cut, es)); 5.107 - 5.108 - MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); 5.109 - 5.110 - assertEquals("testPositionForSuperConstructorCalls", 5.111 - 72 - 24, pos.getStartPosition(cut, mit)); 5.112 - assertEquals("testPositionForSuperConstructorCalls", 5.113 - 79 - 24, pos.getEndPosition(cut, mit)); 5.114 - 5.115 - assertEquals("testPositionForSuperConstructorCalls", 5.116 - 72 - 24, pos.getStartPosition(cut, mit.getMethodSelect())); 5.117 - assertEquals("testPositionForSuperConstructorCalls", 5.118 - 77 - 24, pos.getEndPosition(cut, mit.getMethodSelect())); 5.119 - 5.120 - } 5.121 - 5.122 - public void testPositionForEnumModifiers() throws IOException { 5.123 - 5.124 - String code = "package test; public enum Test {A;}"; 5.125 - 5.126 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.127 - null, Arrays.asList(new MyFileObject(code))); 5.128 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.129 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 5.130 - 5.131 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.132 - ModifiersTree mt = clazz.getModifiers(); 5.133 - 5.134 - assertEquals("testPositionForEnumModifiers", 5.135 - 38 - 24, pos.getStartPosition(cut, mt)); 5.136 - assertEquals("testPositionForEnumModifiers", 5.137 - 44 - 24, pos.getEndPosition(cut, mt)); 5.138 - } 5.139 - 5.140 - public void testNewClassWithEnclosing() throws IOException { 5.141 - 5.142 - 5.143 - String code = "package test; class Test { " + 5.144 - "class d {} private void method() { " + 5.145 - "Object o = Test.this.new d(); } }"; 5.146 - 5.147 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.148 - null, Arrays.asList(new MyFileObject(code))); 5.149 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.150 - SourcePositions pos = Trees.instance(ct).getSourcePositions(); 5.151 - 5.152 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.153 - ExpressionTree est = 5.154 - ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); 5.155 - 5.156 - assertEquals("testNewClassWithEnclosing", 5.157 - 97 - 24, pos.getStartPosition(cut, est)); 5.158 - assertEquals("testNewClassWithEnclosing", 5.159 - 114 - 24, pos.getEndPosition(cut, est)); 5.160 - } 5.161 - 5.162 - public void testPreferredPositionForBinaryOp() throws IOException { 5.163 - 5.164 - String code = "package test; public class Test {" + 5.165 - "private void test() {" + 5.166 - "Object o = null; boolean b = o != null && o instanceof String;" + 5.167 - "} private Test() {}}"; 5.168 - 5.169 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.170 - null, Arrays.asList(new MyFileObject(code))); 5.171 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.172 - 5.173 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.174 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 5.175 - VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); 5.176 - BinaryTree cond = (BinaryTree) condSt.getInitializer(); 5.177 - 5.178 - JCTree condJC = (JCTree) cond; 5.179 - 5.180 - assertEquals("testNewClassWithEnclosing", 5.181 - 117 - 24, condJC.pos); 5.182 - } 5.183 - 5.184 - public void testPositionBrokenSource126732a() throws IOException { 5.185 - String[] commands = new String[]{ 5.186 - "return Runnable()", 5.187 - "do { } while (true)", 5.188 - "throw UnsupportedOperationException()", 5.189 - "assert true", 5.190 - "1 + 1",}; 5.191 - 5.192 - for (String command : commands) { 5.193 - 5.194 - String code = "package test;\n" 5.195 - + "public class Test {\n" 5.196 - + " public static void test() {\n" 5.197 - + " " + command + " {\n" 5.198 - + " new Runnable() {\n" 5.199 - + " };\n" 5.200 - + " }\n" 5.201 - + "}"; 5.202 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 5.203 - null, null, Arrays.asList(new MyFileObject(code))); 5.204 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.205 - 5.206 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.207 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 5.208 - List<? extends StatementTree> statements = 5.209 - method.getBody().getStatements(); 5.210 - 5.211 - StatementTree ret = statements.get(0); 5.212 - StatementTree block = statements.get(1); 5.213 - 5.214 - Trees t = Trees.instance(ct); 5.215 - int len = code.indexOf(command + " {") + (command + " ").length(); 5.216 - assertEquals(command, len, 5.217 - t.getSourcePositions().getEndPosition(cut, ret)); 5.218 - assertEquals(command, len, 5.219 - t.getSourcePositions().getStartPosition(cut, block)); 5.220 - } 5.221 - } 5.222 - 5.223 - public void testPositionBrokenSource126732b() throws IOException { 5.224 - String[] commands = new String[]{ 5.225 - "break", 5.226 - "break A", 5.227 - "continue ", 5.228 - "continue A",}; 5.229 - 5.230 - for (String command : commands) { 5.231 - 5.232 - String code = "package test;\n" 5.233 - + "public class Test {\n" 5.234 - + " public static void test() {\n" 5.235 - + " while (true) {\n" 5.236 - + " " + command + " {\n" 5.237 - + " new Runnable() {\n" 5.238 - + " };\n" 5.239 - + " }\n" 5.240 - + " }\n" 5.241 - + "}"; 5.242 - 5.243 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, 5.244 - null, null, Arrays.asList(new MyFileObject(code))); 5.245 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.246 - 5.247 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.248 - MethodTree method = (MethodTree) clazz.getMembers().get(0); 5.249 - List<? extends StatementTree> statements = 5.250 - ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); 5.251 - 5.252 - StatementTree ret = statements.get(0); 5.253 - StatementTree block = statements.get(1); 5.254 - 5.255 - Trees t = Trees.instance(ct); 5.256 - int len = code.indexOf(command + " {") + (command + " ").length(); 5.257 - assertEquals(command, len, 5.258 - t.getSourcePositions().getEndPosition(cut, ret)); 5.259 - assertEquals(command, len, 5.260 - t.getSourcePositions().getStartPosition(cut, block)); 5.261 - } 5.262 - } 5.263 - 5.264 - public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { 5.265 - 5.266 - String code = "package test; class Test { " + 5.267 - "private void method() { " + 5.268 - "java.util.Set<String> s = null; for (a : s) {} } }"; 5.269 - 5.270 - final List<Diagnostic<? extends JavaFileObject>> errors = 5.271 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 5.272 - 5.273 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 5.274 - new DiagnosticListener<JavaFileObject>() { 5.275 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 5.276 - errors.add(diagnostic); 5.277 - } 5.278 - }, null, null, Arrays.asList(new MyFileObject(code))); 5.279 - 5.280 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.281 - 5.282 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.283 - StatementTree forStatement = 5.284 - ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); 5.285 - 5.286 - assertEquals("testErrorRecoveryForEnhancedForLoop142381", 5.287 - Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); 5.288 - assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); 5.289 - } 5.290 - 5.291 - public void testPositionAnnotationNoPackage187551() throws IOException { 5.292 - 5.293 - String code = "\n@interface Test {}"; 5.294 - 5.295 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.296 - null, Arrays.asList(new MyFileObject(code))); 5.297 - 5.298 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.299 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.300 - Trees t = Trees.instance(ct); 5.301 - 5.302 - assertEquals("testPositionAnnotationNoPackage187551", 5.303 - 1, t.getSourcePositions().getStartPosition(cut, clazz)); 5.304 - } 5.305 - 5.306 - public void testPositionsSane() throws IOException { 5.307 - performPositionsSanityTest("package test; class Test { " + 5.308 - "private void method() { " + 5.309 - "java.util.List<? extends java.util.List<? extends String>> l; " + 5.310 - "} }"); 5.311 - performPositionsSanityTest("package test; class Test { " + 5.312 - "private void method() { " + 5.313 - "java.util.List<? super java.util.List<? super String>> l; " + 5.314 - "} }"); 5.315 - performPositionsSanityTest("package test; class Test { " + 5.316 - "private void method() { " + 5.317 - "java.util.List<? super java.util.List<?>> l; } }"); 5.318 - } 5.319 - 5.320 - private void performPositionsSanityTest(String code) throws IOException { 5.321 - 5.322 - final List<Diagnostic<? extends JavaFileObject>> errors = 5.323 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 5.324 - 5.325 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 5.326 - new DiagnosticListener<JavaFileObject>() { 5.327 - 5.328 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 5.329 - errors.add(diagnostic); 5.330 - } 5.331 - }, null, null, Arrays.asList(new MyFileObject(code))); 5.332 - 5.333 - final CompilationUnitTree cut = ct.parse().iterator().next(); 5.334 - final Trees trees = Trees.instance(ct); 5.335 - 5.336 - new TreeScanner<Void, Void>() { 5.337 - 5.338 - private long parentStart = 0; 5.339 - private long parentEnd = Integer.MAX_VALUE; 5.340 - 5.341 - @Override 5.342 - public Void scan(Tree node, Void p) { 5.343 - if (node == null) { 5.344 - return null; 5.345 - } 5.346 - 5.347 - long start = trees.getSourcePositions().getStartPosition(cut, node); 5.348 - 5.349 - if (start == (-1)) { 5.350 - return null; //synthetic tree 5.351 - } 5.352 - assertTrue(node.toString() + ":" + start + "/" + parentStart, 5.353 - parentStart <= start); 5.354 - 5.355 - long prevParentStart = parentStart; 5.356 - 5.357 - parentStart = start; 5.358 - 5.359 - long end = trees.getSourcePositions().getEndPosition(cut, node); 5.360 - 5.361 - assertTrue(node.toString() + ":" + end + "/" + parentEnd, 5.362 - end <= parentEnd); 5.363 - 5.364 - long prevParentEnd = parentEnd; 5.365 - 5.366 - parentEnd = end; 5.367 - 5.368 - super.scan(node, p); 5.369 - 5.370 - parentStart = prevParentStart; 5.371 - parentEnd = prevParentEnd; 5.372 - 5.373 - return null; 5.374 - } 5.375 - 5.376 - private void assertTrue(String message, boolean b) { 5.377 - if (!b) fail(message); 5.378 - } 5.379 - }.scan(cut, null); 5.380 - } 5.381 - 5.382 - public void testCorrectWilcardPositions() throws IOException { 5.383 - performWildcardPositionsTest("package test; import java.util.List; " + 5.384 - "class Test { private void method() { List<? extends List<? extends String>> l; } }", 5.385 - 5.386 - Arrays.asList("List<? extends List<? extends String>> l;", 5.387 - "List<? extends List<? extends String>>", 5.388 - "List", 5.389 - "? extends List<? extends String>", 5.390 - "List<? extends String>", 5.391 - "List", 5.392 - "? extends String", 5.393 - "String")); 5.394 - performWildcardPositionsTest("package test; import java.util.List; " + 5.395 - "class Test { private void method() { List<? super List<? super String>> l; } }", 5.396 - 5.397 - Arrays.asList("List<? super List<? super String>> l;", 5.398 - "List<? super List<? super String>>", 5.399 - "List", 5.400 - "? super List<? super String>", 5.401 - "List<? super String>", 5.402 - "List", 5.403 - "? super String", 5.404 - "String")); 5.405 - performWildcardPositionsTest("package test; import java.util.List; " + 5.406 - "class Test { private void method() { List<? super List<?>> l; } }", 5.407 - 5.408 - Arrays.asList("List<? super List<?>> l;", 5.409 - "List<? super List<?>>", 5.410 - "List", 5.411 - "? super List<?>", 5.412 - "List<?>", 5.413 - "List", 5.414 - "?")); 5.415 - performWildcardPositionsTest("package test; import java.util.List; " + 5.416 - "class Test { private void method() { " + 5.417 - "List<? extends List<? extends List<? extends String>>> l; } }", 5.418 - 5.419 - Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", 5.420 - "List<? extends List<? extends List<? extends String>>>", 5.421 - "List", 5.422 - "? extends List<? extends List<? extends String>>", 5.423 - "List<? extends List<? extends String>>", 5.424 - "List", 5.425 - "? extends List<? extends String>", 5.426 - "List<? extends String>", 5.427 - "List", 5.428 - "? extends String", 5.429 - "String")); 5.430 - performWildcardPositionsTest("package test; import java.util.List; " + 5.431 - "class Test { private void method() { " + 5.432 - "List<? extends List<? extends List<? extends String >>> l; } }", 5.433 - Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", 5.434 - "List<? extends List<? extends List<? extends String >>>", 5.435 - "List", 5.436 - "? extends List<? extends List<? extends String >>", 5.437 - "List<? extends List<? extends String >>", 5.438 - "List", 5.439 - "? extends List<? extends String >", 5.440 - "List<? extends String >", 5.441 - "List", 5.442 - "? extends String", 5.443 - "String")); 5.444 - } 5.445 - 5.446 - public void performWildcardPositionsTest(final String code, 5.447 - List<String> golden) throws IOException { 5.448 - 5.449 - final List<Diagnostic<? extends JavaFileObject>> errors = 5.450 - new LinkedList<Diagnostic<? extends JavaFileObject>>(); 5.451 - 5.452 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 5.453 - new DiagnosticListener<JavaFileObject>() { 5.454 - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 5.455 - errors.add(diagnostic); 5.456 - } 5.457 - }, null, null, Arrays.asList(new MyFileObject(code))); 5.458 - 5.459 - final CompilationUnitTree cut = ct.parse().iterator().next(); 5.460 - final List<String> content = new LinkedList<String>(); 5.461 - final Trees trees = Trees.instance(ct); 5.462 - 5.463 - new TreeScanner<Void, Void>() { 5.464 - @Override 5.465 - public Void scan(Tree node, Void p) { 5.466 - if (node == null) { 5.467 - return null; 5.468 - } 5.469 - long start = trees.getSourcePositions().getStartPosition(cut, node); 5.470 - 5.471 - if (start == (-1)) { 5.472 - return null; //synthetic tree 5.473 - } 5.474 - long end = trees.getSourcePositions().getEndPosition(cut, node); 5.475 - String s = code.substring((int) start, (int) end); 5.476 - content.add(s); 5.477 - 5.478 - return super.scan(node, p); 5.479 - } 5.480 - }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); 5.481 - 5.482 - assertEquals("performWildcardPositionsTest",golden.toString(), 5.483 - content.toString()); 5.484 - } 5.485 - 5.486 - public void testStartPositionForMethodWithoutModifiers() throws IOException { 5.487 - 5.488 - String code = "package t; class Test { <T> void t() {} }"; 5.489 - 5.490 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.491 - null, Arrays.asList(new MyFileObject(code))); 5.492 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.493 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.494 - MethodTree mt = (MethodTree) clazz.getMembers().get(0); 5.495 - Trees t = Trees.instance(ct); 5.496 - int start = (int) t.getSourcePositions().getStartPosition(cut, mt); 5.497 - int end = (int) t.getSourcePositions().getEndPosition(cut, mt); 5.498 - 5.499 - assertEquals("testStartPositionForMethodWithoutModifiers", 5.500 - "<T> void t() {}", code.substring(start, end)); 5.501 - } 5.502 - 5.503 - public void testStartPositionEnumConstantInit() throws IOException { 5.504 - 5.505 - String code = "package t; enum Test { AAA; }"; 5.506 - 5.507 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.508 - null, Arrays.asList(new MyFileObject(code))); 5.509 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.510 - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 5.511 - VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); 5.512 - Trees t = Trees.instance(ct); 5.513 - int start = (int) t.getSourcePositions().getStartPosition(cut, 5.514 - enumAAA.getInitializer()); 5.515 - 5.516 - assertEquals("testStartPositionEnumConstantInit", -1, start); 5.517 - } 5.518 - 5.519 - public void testVariableInIfThen1() throws IOException { 5.520 - 5.521 - String code = "package t; class Test { " + 5.522 - "private static void t(String name) { " + 5.523 - "if (name != null) String nn = name.trim(); } }"; 5.524 - 5.525 - DiagnosticCollector<JavaFileObject> coll = 5.526 - new DiagnosticCollector<JavaFileObject>(); 5.527 - 5.528 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 5.529 - null, Arrays.asList(new MyFileObject(code))); 5.530 - 5.531 - ct.parse(); 5.532 - 5.533 - List<String> codes = new LinkedList<String>(); 5.534 - 5.535 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 5.536 - codes.add(d.getCode()); 5.537 - } 5.538 - 5.539 - assertEquals("testVariableInIfThen1", 5.540 - Arrays.<String>asList("compiler.err.variable.not.allowed"), 5.541 - codes); 5.542 - } 5.543 - 5.544 - public void testVariableInIfThen2() throws IOException { 5.545 - 5.546 - String code = "package t; class Test { " + 5.547 - "private static void t(String name) { " + 5.548 - "if (name != null) class X {} } }"; 5.549 - DiagnosticCollector<JavaFileObject> coll = 5.550 - new DiagnosticCollector<JavaFileObject>(); 5.551 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 5.552 - null, Arrays.asList(new MyFileObject(code))); 5.553 - 5.554 - ct.parse(); 5.555 - 5.556 - List<String> codes = new LinkedList<String>(); 5.557 - 5.558 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 5.559 - codes.add(d.getCode()); 5.560 - } 5.561 - 5.562 - assertEquals("testVariableInIfThen2", 5.563 - Arrays.<String>asList("compiler.err.class.not.allowed"), codes); 5.564 - } 5.565 - 5.566 - public void testVariableInIfThen3() throws IOException { 5.567 - 5.568 - String code = "package t; class Test { "+ 5.569 - "private static void t(String name) { " + 5.570 - "if (name != null) abstract } }"; 5.571 - DiagnosticCollector<JavaFileObject> coll = 5.572 - new DiagnosticCollector<JavaFileObject>(); 5.573 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, 5.574 - null, Arrays.asList(new MyFileObject(code))); 5.575 - 5.576 - ct.parse(); 5.577 - 5.578 - List<String> codes = new LinkedList<String>(); 5.579 - 5.580 - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { 5.581 - codes.add(d.getCode()); 5.582 - } 5.583 - 5.584 - assertEquals("testVariableInIfThen3", 5.585 - Arrays.<String>asList("compiler.err.illegal.start.of.expr"), 5.586 - codes); 5.587 - } 5.588 - 5.589 - //see javac bug #6882235, NB bug #98234: 5.590 - public void testMissingExponent() throws IOException { 5.591 - 5.592 - String code = "\nclass Test { { System.err.println(0e); } }"; 5.593 - 5.594 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.595 - null, Arrays.asList(new MyFileObject(code))); 5.596 - 5.597 - assertNotNull(ct.parse().iterator().next()); 5.598 - } 5.599 - 5.600 - public void testTryResourcePos() throws IOException { 5.601 - 5.602 - final String code = "package t; class Test { " + 5.603 - "{ try (java.io.InputStream in = null) { } } }"; 5.604 - 5.605 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.606 - null, Arrays.asList(new MyFileObject(code))); 5.607 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.608 - 5.609 - new TreeScanner<Void, Void>() { 5.610 - @Override 5.611 - public Void visitVariable(VariableTree node, Void p) { 5.612 - if ("in".contentEquals(node.getName())) { 5.613 - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 5.614 - System.out.println(node.getName() + "," + var.pos); 5.615 - assertEquals("testTryResourcePos", "in = null) { } } }", 5.616 - code.substring(var.pos)); 5.617 - } 5.618 - return super.visitVariable(node, p); 5.619 - } 5.620 - }.scan(cut, null); 5.621 - } 5.622 - 5.623 - public void testVarPos() throws IOException { 5.624 - 5.625 - final String code = "package t; class Test { " + 5.626 - "{ java.io.InputStream in = null; } }"; 5.627 - 5.628 - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, 5.629 - null, Arrays.asList(new MyFileObject(code))); 5.630 - CompilationUnitTree cut = ct.parse().iterator().next(); 5.631 - 5.632 - new TreeScanner<Void, Void>() { 5.633 - 5.634 - @Override 5.635 - public Void visitVariable(VariableTree node, Void p) { 5.636 - if ("in".contentEquals(node.getName())) { 5.637 - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; 5.638 - assertEquals("testVarPos","in = null; } }", 5.639 - code.substring(var.pos)); 5.640 - } 5.641 - return super.visitVariable(node, p); 5.642 - } 5.643 - }.scan(cut, null); 5.644 - } 5.645 - 5.646 - void testsNotWorking() throws IOException { 5.647 - 5.648 - // Fails with nb-javac, needs further investigation 5.649 - testPositionBrokenSource126732a(); 5.650 - testPositionBrokenSource126732b(); 5.651 - 5.652 - // Fails, these tests yet to be addressed 5.653 - testVariableInIfThen1(); 5.654 - testVariableInIfThen2(); 5.655 - testPositionForEnumModifiers(); 5.656 - testStartPositionEnumConstantInit(); 5.657 - } 5.658 - void testPositions() throws IOException { 5.659 - testPositionsSane(); 5.660 - testCorrectWilcardPositions(); 5.661 - testPositionAnnotationNoPackage187551(); 5.662 - testPositionForSuperConstructorCalls(); 5.663 - testPreferredPositionForBinaryOp(); 5.664 - testStartPositionForMethodWithoutModifiers(); 5.665 - testVarPos(); 5.666 - testVariableInIfThen3(); 5.667 - testTryResourcePos(); 5.668 - } 5.669 - 5.670 - public static void main(String... args) throws IOException { 5.671 - JavacParserTest jpt = new JavacParserTest("JavacParserTest"); 5.672 - jpt.testPositions(); 5.673 - System.out.println("PASS"); 5.674 - } 5.675 -} 5.676 - 5.677 -abstract class TestCase { 5.678 - 5.679 - void assertEquals(String message, int i, int pos) { 5.680 - if (i != pos) { 5.681 - fail(message); 5.682 - } 5.683 - } 5.684 - 5.685 - void assertFalse(String message, boolean empty) { 5.686 - throw new UnsupportedOperationException("Not yet implemented"); 5.687 - } 5.688 - 5.689 - void assertEquals(String message, int i, long l) { 5.690 - if (i != l) { 5.691 - fail(message + ":" + i + ":" + l); 5.692 - } 5.693 - } 5.694 - 5.695 - void assertEquals(String message, Object o1, Object o2) { 5.696 - System.out.println(o1); 5.697 - System.out.println(o2); 5.698 - if (o1 != null && o2 != null && !o1.equals(o2)) { 5.699 - fail(message); 5.700 - } 5.701 - if (o1 == null && o2 != null) { 5.702 - fail(message); 5.703 - } 5.704 - } 5.705 - 5.706 - void assertNotNull(Object o) { 5.707 - if (o == null) { 5.708 - fail(); 5.709 - } 5.710 - } 5.711 - 5.712 - void fail() { 5.713 - fail("test failed"); 5.714 - } 5.715 - 5.716 - void fail(String message) { 5.717 - throw new RuntimeException(message); 5.718 - } 5.719 -}