Skip to content

Commit

Permalink
Handled sealed & non-sealed interfaces through ITypeBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Dec 12, 2024
1 parent 6695651 commit 42ee04d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,100 @@ protected abstract class A {}
assertEquals("'public' modifier is not set in binding", Modifier.isProtected(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isAbstract(aBinding.getModifiers()), true);
}

//public sealed interface
public void test003_k() throws CoreException {
ASTParser astParser = ASTParser.newParser(getAST23());
Map<String, String> options = new HashMap<>();
options.put(JavaCore.COMPILER_COMPLIANCE, "23");
options.put(JavaCore.COMPILER_SOURCE, "23");

astParser.setCompilerOptions(options);
astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true);
astParser.setUnitName("Example.java");
astParser.setResolveBindings(true);
astParser.setBindingsRecovery(true);

String source ="""
public sealed interface A permits X {}
public final class X implements A {}
""";

astParser.setSource(source.toCharArray());

CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertTrue(Modifier.isSealed(a.getModifiers()));
assertTrue(Modifier.isPublic(a.getModifiers()));
assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 1);

ITypeBinding aBinding = a.resolveBinding();
assertTrue(Modifier.isSealed(aBinding.getModifiers()));
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
}

//public non-sealed interface
public void test003_l() throws CoreException {
ASTParser astParser = ASTParser.newParser(getAST23());
Map<String, String> options = new HashMap<>();
options.put(JavaCore.COMPILER_COMPLIANCE, "23");
options.put(JavaCore.COMPILER_SOURCE, "23");

astParser.setCompilerOptions(options);
astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true);
astParser.setUnitName("Example.java");
astParser.setResolveBindings(true);
astParser.setBindingsRecovery(true);

String source ="""
public non-sealed interface A permits X {}
public final class X implements A {}
""";

astParser.setSource(source.toCharArray());

CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertTrue(Modifier.isNonSealed(a.getModifiers()));
assertTrue(Modifier.isPublic(a.getModifiers()));
assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 1);

ITypeBinding aBinding = a.resolveBinding();
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
}

//public strictfp interface
public void test003_m() throws CoreException {
ASTParser astParser = ASTParser.newParser(getAST23());
Map<String, String> options = new HashMap<>();
options.put(JavaCore.COMPILER_COMPLIANCE, "23");
options.put(JavaCore.COMPILER_SOURCE, "23");

astParser.setCompilerOptions(options);
astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true);
astParser.setUnitName("Example.java");
astParser.setResolveBindings(true);
astParser.setBindingsRecovery(true);

String source ="""
public strictfp interface A permits X {}
public final class X implements A {}
""";

astParser.setSource(source.toCharArray());

CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertTrue(Modifier.isStrictfp(a.getModifiers()));
assertTrue(Modifier.isPublic(a.getModifiers()));
assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 1);

ITypeBinding aBinding = a.resolveBinding();
assertTrue(Modifier.isStrictfp(aBinding.getModifiers()));
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,13 @@ public int getModifiers() {
} else if (isInterface()) {
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;

if (referenceBinding.isSealed()) {
return accessFlags | Modifier.SEALED;
}
if (referenceBinding.isNonSealed()) {
return accessFlags | Modifier.NON_SEALED;
}
// clear the AccAbstract and the AccInterface bits
return accessFlags & ~(ClassFileConstants.AccAbstract | ClassFileConstants.AccInterface);
} else if (isEnum()) {
Expand Down

0 comments on commit 42ee04d

Please sign in to comment.