|
15 | 15 |
|
16 | 16 | import java.io.IOException; |
17 | 17 | import java.util.Arrays; |
| 18 | +import java.util.HashMap; |
18 | 19 | import java.util.Hashtable; |
19 | 20 | import java.util.Iterator; |
20 | 21 | import java.util.List; |
|
31 | 32 | import org.eclipse.jdt.core.tests.model.ReconcilerTests; |
32 | 33 | import org.eclipse.jdt.core.tests.util.Util; |
33 | 34 | import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
| 35 | +import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener; |
34 | 36 |
|
35 | 37 | @SuppressWarnings({"rawtypes", "unchecked"}) |
36 | 38 | public class ASTConverterTest2 extends ConverterTestSetup { |
@@ -5514,4 +5516,122 @@ public void test0610() throws CoreException { |
5514 | 5516 | } |
5515 | 5517 | } |
5516 | 5518 |
|
| 5519 | + public void testBug4234_01() throws CoreException { |
| 5520 | + String source = """ |
| 5521 | + module a { |
| 5522 | + exports pack to b; |
| 5523 | + } |
| 5524 | + """; |
| 5525 | + |
| 5526 | + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
| 5527 | + parser.setKind(ASTParser.K_COMPILATION_UNIT); |
| 5528 | + parser.setUnitName("module-info.java"); |
| 5529 | + |
| 5530 | + Map<String, String> options = new HashMap<>(); |
| 5531 | + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.latestSupportedJavaVersion()); |
| 5532 | + options.put(JavaCore.COMPILER_SOURCE, JavaCore.latestSupportedJavaVersion()); |
| 5533 | + options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.latestSupportedJavaVersion()); |
| 5534 | + parser.setCompilerOptions(options); |
| 5535 | + |
| 5536 | + parser.setSource(source.toCharArray()); |
| 5537 | + parser.setResolveBindings(false); |
| 5538 | + parser.setStatementsRecovery(false); |
| 5539 | + parser.setBindingsRecovery(false); |
| 5540 | + CompilationUnit cu = (CompilationUnit) parser.createAST(null); |
| 5541 | + ModuleDeclaration module = cu.getModule(); |
| 5542 | + |
| 5543 | + ExportsDirective export = null; |
| 5544 | + for (Object directive : module.moduleStatements()) { |
| 5545 | + if (directive instanceof ExportsDirective) { |
| 5546 | + export = (ExportsDirective) directive; |
| 5547 | + break; |
| 5548 | + } |
| 5549 | + } |
| 5550 | + assertNotNull("ExportsDirective not found", export); |
| 5551 | + |
| 5552 | + NaiveASTFlattener flattener = new NaiveASTFlattener(); |
| 5553 | + export.accept(flattener); |
| 5554 | + assertEquals("exports pack to b;\n", flattener.getResult()); |
| 5555 | + } |
| 5556 | + |
| 5557 | + public void testBug4234_02() throws CoreException { |
| 5558 | + String source = """ |
| 5559 | + module a { |
| 5560 | + opens pack to b; |
| 5561 | + } |
| 5562 | + """; |
| 5563 | + |
| 5564 | + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
| 5565 | + parser.setKind(ASTParser.K_COMPILATION_UNIT); |
| 5566 | + parser.setUnitName("module-info.java"); |
| 5567 | + |
| 5568 | + Map<String, String> options = new HashMap<>(); |
| 5569 | + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.latestSupportedJavaVersion()); |
| 5570 | + options.put(JavaCore.COMPILER_SOURCE, JavaCore.latestSupportedJavaVersion()); |
| 5571 | + options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.latestSupportedJavaVersion()); |
| 5572 | + parser.setCompilerOptions(options); |
| 5573 | + |
| 5574 | + parser.setSource(source.toCharArray()); |
| 5575 | + parser.setResolveBindings(false); |
| 5576 | + parser.setStatementsRecovery(false); |
| 5577 | + parser.setBindingsRecovery(false); |
| 5578 | + CompilationUnit cu = (CompilationUnit) parser.createAST(null); |
| 5579 | + ModuleDeclaration module = cu.getModule(); |
| 5580 | + |
| 5581 | + OpensDirective opens = null; |
| 5582 | + for (Object directive : module.moduleStatements()) { |
| 5583 | + if (directive instanceof OpensDirective) { |
| 5584 | + opens = (OpensDirective) directive; |
| 5585 | + break; |
| 5586 | + } |
| 5587 | + } |
| 5588 | + assertNotNull("OpensDirective not found", opens); |
| 5589 | + |
| 5590 | + NaiveASTFlattener flattener = new NaiveASTFlattener(); |
| 5591 | + opens.accept(flattener); |
| 5592 | + assertEquals("opens pack to b;\n", flattener.getResult()); |
| 5593 | + } |
| 5594 | + |
| 5595 | + public void testBug4234_03() throws CoreException { |
| 5596 | + String source = """ |
| 5597 | + module a { |
| 5598 | + provides java.util.spi.ResourceBundleProvider |
| 5599 | + with com.example.MyProvider, com.example.AltProvider; |
| 5600 | + } |
| 5601 | + """; |
| 5602 | + |
| 5603 | + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
| 5604 | + parser.setKind(ASTParser.K_COMPILATION_UNIT); |
| 5605 | + parser.setUnitName("module-info.java"); |
| 5606 | + |
| 5607 | + Map<String, String> options = new HashMap<>(); |
| 5608 | + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.latestSupportedJavaVersion()); |
| 5609 | + options.put(JavaCore.COMPILER_SOURCE, JavaCore.latestSupportedJavaVersion()); |
| 5610 | + options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.latestSupportedJavaVersion()); |
| 5611 | + parser.setCompilerOptions(options); |
| 5612 | + |
| 5613 | + parser.setSource(source.toCharArray()); |
| 5614 | + parser.setResolveBindings(false); |
| 5615 | + parser.setStatementsRecovery(false); |
| 5616 | + parser.setBindingsRecovery(false); |
| 5617 | + CompilationUnit cu = (CompilationUnit) parser.createAST(null); |
| 5618 | + ModuleDeclaration module = cu.getModule(); |
| 5619 | + |
| 5620 | + ProvidesDirective provides = null; |
| 5621 | + for (Object directive : module.moduleStatements()) { |
| 5622 | + if (directive instanceof ProvidesDirective) { |
| 5623 | + provides = (ProvidesDirective) directive; |
| 5624 | + break; |
| 5625 | + } |
| 5626 | + } |
| 5627 | + assertNotNull("ProvidesDirective not found", provides); |
| 5628 | + |
| 5629 | + NaiveASTFlattener flattener = new NaiveASTFlattener(); |
| 5630 | + provides.accept(flattener); |
| 5631 | + |
| 5632 | + // Note: Flattener output has no newlines |
| 5633 | + String expected = "provides java.util.spi.ResourceBundleProvider with com.example.MyProvider, com.example.AltProvider;\n"; |
| 5634 | + assertEquals(expected, flattener.getResult()); |
| 5635 | + } |
| 5636 | + |
5517 | 5637 | } |
0 commit comments