test/tools/javac/parser/JavacParserTest.java

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

mercurial