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