|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 |
| - * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 |
| 26 | + * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 8315452 |
27 | 27 | * @summary tests error and diagnostics positions
|
28 | 28 | * @author Jan Lahoda
|
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api
|
|
60 | 60 | import com.sun.tools.javac.tree.JCTree;
|
61 | 61 | import java.io.IOException;
|
62 | 62 | import java.io.StringWriter;
|
| 63 | +import java.io.UncheckedIOException; |
| 64 | +import java.io.Writer; |
63 | 65 | import java.lang.annotation.ElementType;
|
64 | 66 | import java.lang.annotation.Retention;
|
65 | 67 | import java.lang.annotation.RetentionPolicy;
|
|
70 | 72 | import java.util.Arrays;
|
71 | 73 | import java.util.LinkedList;
|
72 | 74 | import java.util.List;
|
| 75 | +import java.util.Objects; |
73 | 76 | import java.util.regex.Pattern;
|
74 | 77 | import javax.lang.model.element.Modifier;
|
75 | 78 | import javax.lang.model.type.TypeKind;
|
|
88 | 91 | import com.sun.source.util.TreePathScanner;
|
89 | 92 | import com.sun.tools.javac.api.JavacTaskPool;
|
90 | 93 | import com.sun.tools.javac.api.JavacTaskPool.Worker;
|
91 |
| -import java.util.Objects; |
| 94 | +import com.sun.tools.javac.tree.JCTree.JCErroneous; |
| 95 | +import com.sun.tools.javac.tree.Pretty; |
92 | 96 |
|
93 | 97 | public class JavacParserTest extends TestCase {
|
94 | 98 | static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
@@ -2486,6 +2490,28 @@ void main() {
|
2486 | 2490 | codes);
|
2487 | 2491 | }
|
2488 | 2492 |
|
| 2493 | + @Test //JDK-8315452 |
| 2494 | + void testPartialTopLevelModifiers() throws IOException { |
| 2495 | + String code = """ |
| 2496 | + package test; |
| 2497 | + public |
| 2498 | + """; |
| 2499 | + DiagnosticCollector<JavaFileObject> coll = |
| 2500 | + new DiagnosticCollector<>(); |
| 2501 | + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, |
| 2502 | + List.of("--enable-preview", "--source", SOURCE_VERSION), |
| 2503 | + null, Arrays.asList(new MyFileObject(code))); |
| 2504 | + CompilationUnitTree cut = ct.parse().iterator().next(); |
| 2505 | + |
| 2506 | + String result = toStringWithErrors(cut).replaceAll("\\R", "\n"); |
| 2507 | + System.out.println("RESULT\n" + result); |
| 2508 | + assertEquals("incorrect AST", |
| 2509 | + result, |
| 2510 | + """ |
| 2511 | + package test; |
| 2512 | + (ERROR: public )"""); |
| 2513 | + } |
| 2514 | + |
2489 | 2515 | void run(String[] args) throws Exception {
|
2490 | 2516 | int passed = 0, failed = 0;
|
2491 | 2517 | final Pattern p = (args != null && args.length > 0)
|
@@ -2515,6 +2541,38 @@ void run(String[] args) throws Exception {
|
2515 | 2541 | passed + ", failed = " + failed + " ??????????");
|
2516 | 2542 | }
|
2517 | 2543 | }
|
| 2544 | + |
| 2545 | + private String toStringWithErrors(Tree tree) { |
| 2546 | + StringWriter s = new StringWriter(); |
| 2547 | + try { |
| 2548 | + new PrettyWithErrors(s, false).printExpr((JCTree) tree); |
| 2549 | + } catch (IOException e) { |
| 2550 | + // should never happen, because StringWriter is defined |
| 2551 | + // never to throw any IOExceptions |
| 2552 | + throw new AssertionError(e); |
| 2553 | + } |
| 2554 | + return s.toString(); |
| 2555 | + } |
| 2556 | + |
| 2557 | + private static final class PrettyWithErrors extends Pretty { |
| 2558 | + |
| 2559 | + public PrettyWithErrors(Writer out, boolean sourceOutput) { |
| 2560 | + super(out, sourceOutput); |
| 2561 | + } |
| 2562 | + |
| 2563 | + @Override |
| 2564 | + public void visitErroneous(JCErroneous tree) { |
| 2565 | + try { |
| 2566 | + print("(ERROR: "); |
| 2567 | + print(tree.errs); |
| 2568 | + print(")"); |
| 2569 | + } catch (IOException e) { |
| 2570 | + throw new UncheckedIOException(e); |
| 2571 | + } |
| 2572 | + } |
| 2573 | + |
| 2574 | + } |
| 2575 | + |
2518 | 2576 | }
|
2519 | 2577 |
|
2520 | 2578 | abstract class TestCase {
|
|
0 commit comments