Skip to content

Commit 178a88c

Browse files
Added the unit tests and added the modifiers to the resolve bindings
for sealed and non-sealed modifiers
1 parent 09286e7 commit 178a88c

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_23Test.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.core.tests.dom;
1515

16+
import java.util.HashMap;
1617
import java.util.List;
18+
import java.util.Map;
1719
import junit.framework.Test;
1820
import org.eclipse.core.runtime.CoreException;
1921
import org.eclipse.jdt.core.ICompilationUnit;
2022
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.*;
3024

3125
public class ASTConverter_23Test extends ConverterTestSetup {
3226

@@ -133,4 +127,64 @@ void main() {
133127
assertEquals("Not a Block", block.getNodeType(), ASTNode.BLOCK);
134128
assertEquals("Block startPosition is not correct", block.getStartPosition(), 21);
135129
}
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+
}
136190
}

org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,16 @@ public int getKind() {
588588
public int getModifiers() {
589589
if (isClass()) {
590590
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
591-
final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
591+
int k = referenceBinding.getAccessFlags();
592+
final int accessFlags = k & VALID_MODIFIERS;
592593
if (referenceBinding.isAnonymousType()) {
593594
return accessFlags & ~Modifier.FINAL;
595+
} else if (referenceBinding.isSealed()) {
596+
return Modifier.SEALED;
597+
} else if (referenceBinding.isNonSealed()) {
598+
return Modifier.NON_SEALED;
594599
}
600+
595601
return accessFlags;
596602
} else if (isAnnotation()) {
597603
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;

0 commit comments

Comments
 (0)