|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.jdt.core.tests.dom; |
15 | 15 |
|
| 16 | +import java.util.HashMap; |
16 | 17 | import java.util.List; |
| 18 | +import java.util.Map; |
17 | 19 | import junit.framework.Test; |
18 | 20 | import org.eclipse.core.runtime.CoreException; |
19 | 21 | import org.eclipse.jdt.core.ICompilationUnit; |
20 | 22 | import org.eclipse.jdt.core.JavaCore; |
21 | | -import org.eclipse.jdt.core.dom.AST; |
22 | | -import org.eclipse.jdt.core.dom.ASTNode; |
23 | | -import org.eclipse.jdt.core.dom.Block; |
24 | | -import org.eclipse.jdt.core.dom.CompilationUnit; |
25 | | -import org.eclipse.jdt.core.dom.ImplicitTypeDeclaration; |
26 | | -import org.eclipse.jdt.core.dom.ImportDeclaration; |
27 | | -import org.eclipse.jdt.core.dom.Javadoc; |
28 | | -import org.eclipse.jdt.core.dom.MethodDeclaration; |
29 | | -import org.eclipse.jdt.core.dom.Modifier; |
| 23 | +import org.eclipse.jdt.core.dom.*; |
30 | 24 |
|
31 | 25 | public class ASTConverter_23Test extends ConverterTestSetup { |
32 | 26 |
|
@@ -133,4 +127,64 @@ void main() { |
133 | 127 | assertEquals("Not a Block", block.getNodeType(), ASTNode.BLOCK); |
134 | 128 | assertEquals("Block startPosition is not correct", block.getStartPosition(), 21); |
135 | 129 | } |
| 130 | + |
| 131 | + public void test003_a() throws CoreException { |
| 132 | + ASTParser astParser = ASTParser.newParser(getAST23()); |
| 133 | + Map<String, String> options = new HashMap<>(); |
| 134 | + options.put(JavaCore.COMPILER_COMPLIANCE, "23"); |
| 135 | + options.put(JavaCore.COMPILER_SOURCE, "23"); |
| 136 | + |
| 137 | + astParser.setCompilerOptions(options); |
| 138 | + astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true); |
| 139 | + astParser.setUnitName("Example.java"); |
| 140 | + astParser.setResolveBindings(true); |
| 141 | + astParser.setBindingsRecovery(true); |
| 142 | + |
| 143 | + String source =""" |
| 144 | + sealed class A permits B, C {} |
| 145 | + final class B extends A {} |
| 146 | + non-sealed class C extends A {} |
| 147 | + """; |
| 148 | + |
| 149 | + astParser.setSource(source.toCharArray()); |
| 150 | + |
| 151 | + CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null); |
| 152 | + TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0); |
| 153 | + |
| 154 | + assertEquals("Modifier is not present in AST", Modifier.isSealed(a.getModifiers()), true); |
| 155 | + |
| 156 | + assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 2); |
| 157 | + |
| 158 | + ITypeBinding aBinding = a.resolveBinding(); |
| 159 | + assertEquals("'sealed' modifier is not set in binding", Modifier.isSealed(aBinding.getModifiers()), true); |
| 160 | + } |
| 161 | + |
| 162 | + public void test003_b() throws CoreException { |
| 163 | + ASTParser astParser = ASTParser.newParser(getAST23()); |
| 164 | + Map<String, String> options = new HashMap<>(); |
| 165 | + options.put(JavaCore.COMPILER_COMPLIANCE, "23"); |
| 166 | + options.put(JavaCore.COMPILER_SOURCE, "23"); |
| 167 | + |
| 168 | + astParser.setCompilerOptions(options); |
| 169 | + astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true); |
| 170 | + astParser.setUnitName("Example.java"); |
| 171 | + astParser.setResolveBindings(true); |
| 172 | + astParser.setBindingsRecovery(true); |
| 173 | + |
| 174 | + String source =""" |
| 175 | + sealed class A permits B, C {} |
| 176 | + final class B extends A {} |
| 177 | + non-sealed class C extends A {} |
| 178 | + """; |
| 179 | + |
| 180 | + astParser.setSource(source.toCharArray()); |
| 181 | + |
| 182 | + CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null); |
| 183 | + TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(2); |
| 184 | + |
| 185 | + assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true); |
| 186 | + |
| 187 | + ITypeBinding aBinding = a.resolveBinding(); |
| 188 | + assertEquals("'non-sealed' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true); |
| 189 | + } |
136 | 190 | } |
0 commit comments