|
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 8312093 |
| 26 | + * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8312093 8315452 |
| 27 | +
|
27 | 28 | * @summary tests error and diagnostics positions
|
28 | 29 | * @author Jan Lahoda
|
29 | 30 | * @modules jdk.compiler/com.sun.tools.javac.api
|
|
60 | 61 | import com.sun.tools.javac.tree.JCTree;
|
61 | 62 | import java.io.IOException;
|
62 | 63 | import java.io.StringWriter;
|
| 64 | +import java.io.UncheckedIOException; |
| 65 | +import java.io.Writer; |
63 | 66 | import java.lang.annotation.ElementType;
|
64 | 67 | import java.lang.annotation.Retention;
|
65 | 68 | import java.lang.annotation.RetentionPolicy;
|
|
70 | 73 | import java.util.Arrays;
|
71 | 74 | import java.util.LinkedList;
|
72 | 75 | import java.util.List;
|
| 76 | +import java.util.Objects; |
73 | 77 | import java.util.regex.Pattern;
|
74 | 78 | import javax.lang.model.element.Modifier;
|
75 | 79 | import javax.lang.model.type.TypeKind;
|
|
88 | 92 | import com.sun.source.util.TreePathScanner;
|
89 | 93 | import com.sun.tools.javac.api.JavacTaskPool;
|
90 | 94 | import com.sun.tools.javac.api.JavacTaskPool.Worker;
|
91 |
| -import java.util.Objects; |
| 95 | +import com.sun.tools.javac.tree.JCTree.JCErroneous; |
| 96 | +import com.sun.tools.javac.tree.Pretty; |
92 | 97 |
|
93 | 98 | public class JavacParserTest extends TestCase {
|
94 | 99 | static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
@@ -2423,6 +2428,28 @@ public Void visitMethod(MethodTree node, Void p) {
|
2423 | 2428 | }.scan(cut, null);
|
2424 | 2429 | }
|
2425 | 2430 |
|
| 2431 | + @Test //JDK-8315452 |
| 2432 | + void testPartialTopLevelModifiers() throws IOException { |
| 2433 | + String code = """ |
| 2434 | + package test; |
| 2435 | + public |
| 2436 | + """; |
| 2437 | + DiagnosticCollector<JavaFileObject> coll = |
| 2438 | + new DiagnosticCollector<>(); |
| 2439 | + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, |
| 2440 | + List.of("--enable-preview", "--source", SOURCE_VERSION), |
| 2441 | + null, Arrays.asList(new MyFileObject(code))); |
| 2442 | + CompilationUnitTree cut = ct.parse().iterator().next(); |
| 2443 | + |
| 2444 | + String result = toStringWithErrors(cut).replaceAll("\\R", "\n"); |
| 2445 | + System.out.println("RESULT\n" + result); |
| 2446 | + assertEquals("incorrect AST", |
| 2447 | + result, |
| 2448 | + """ |
| 2449 | + package test; |
| 2450 | + (ERROR: public )"""); |
| 2451 | + } |
| 2452 | + |
2426 | 2453 | void run(String[] args) throws Exception {
|
2427 | 2454 | int passed = 0, failed = 0;
|
2428 | 2455 | final Pattern p = (args != null && args.length > 0)
|
@@ -2452,6 +2479,38 @@ void run(String[] args) throws Exception {
|
2452 | 2479 | passed + ", failed = " + failed + " ??????????");
|
2453 | 2480 | }
|
2454 | 2481 | }
|
| 2482 | + |
| 2483 | + private String toStringWithErrors(Tree tree) { |
| 2484 | + StringWriter s = new StringWriter(); |
| 2485 | + try { |
| 2486 | + new PrettyWithErrors(s, false).printExpr((JCTree) tree); |
| 2487 | + } catch (IOException e) { |
| 2488 | + // should never happen, because StringWriter is defined |
| 2489 | + // never to throw any IOExceptions |
| 2490 | + throw new AssertionError(e); |
| 2491 | + } |
| 2492 | + return s.toString(); |
| 2493 | + } |
| 2494 | + |
| 2495 | + private static final class PrettyWithErrors extends Pretty { |
| 2496 | + |
| 2497 | + public PrettyWithErrors(Writer out, boolean sourceOutput) { |
| 2498 | + super(out, sourceOutput); |
| 2499 | + } |
| 2500 | + |
| 2501 | + @Override |
| 2502 | + public void visitErroneous(JCErroneous tree) { |
| 2503 | + try { |
| 2504 | + print("(ERROR: "); |
| 2505 | + print(tree.errs); |
| 2506 | + print(")"); |
| 2507 | + } catch (IOException e) { |
| 2508 | + throw new UncheckedIOException(e); |
| 2509 | + } |
| 2510 | + } |
| 2511 | + |
| 2512 | + } |
| 2513 | + |
2455 | 2514 | }
|
2456 | 2515 |
|
2457 | 2516 | abstract class TestCase {
|
|
0 commit comments