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 17, 2024
1 parent d557d32 commit 14f867a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ non-sealed class C extends A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isSealed(a.getModifiers()), true);
assertTrue(Modifier.isSealed(a.getModifiers()));

assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 2);

ITypeBinding aBinding = a.resolveBinding();
assertEquals("'sealed' modifier is not set in binding", Modifier.isSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isSealed(aBinding.getModifiers()));
}

public void test003_b() throws CoreException {
Expand All @@ -182,10 +182,10 @@ non-sealed class C extends A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(2);

assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true);
assertTrue(Modifier.isNonSealed(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();
assertEquals("'non-sealed' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
}

//public sealed
Expand All @@ -212,14 +212,14 @@ non-sealed class C extends A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isSealed(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isPublic(a.getModifiers()), true);
assertTrue(Modifier.isSealed(a.getModifiers()));
assertTrue(Modifier.isPublic(a.getModifiers()));

assertEquals("permitted types are not present in AST", a.permittedTypes().size(), 2);

ITypeBinding aBinding = a.resolveBinding();
assertEquals("'sealed' modifier is not set in binding", Modifier.isSealed(aBinding.getModifiers()), true);
assertEquals("'public' modifier is not set in binding", Modifier.isPublic(aBinding.getModifiers()), true);
assertTrue(Modifier.isSealed(aBinding.getModifiers()));
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
}

//abstract final
Expand All @@ -243,13 +243,13 @@ abstract final class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isAbstract(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isFinal(a.getModifiers()), true);
assertTrue(Modifier.isAbstract(a.getModifiers()));
assertTrue(Modifier.isFinal(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'abstract' modifier is not set in binding", Modifier.isAbstract(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isFinal(aBinding.getModifiers()), true);
assertTrue(Modifier.isAbstract(aBinding.getModifiers()));
assertTrue(Modifier.isFinal(aBinding.getModifiers()));
}

//abstract non-sealed
Expand All @@ -273,13 +273,13 @@ abstract non-sealed class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isAbstract(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true);
assertTrue(Modifier.isAbstract(a.getModifiers()));
assertTrue(Modifier.isNonSealed(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'abstract' modifier is not set in binding", Modifier.isAbstract(aBinding.getModifiers()), true);
assertEquals("'non-sealed' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isAbstract(aBinding.getModifiers()));
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
}

//public final
Expand All @@ -303,13 +303,13 @@ public final class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isPublic(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isFinal(a.getModifiers()), true);
assertTrue(Modifier.isPublic(a.getModifiers()));
assertTrue(Modifier.isFinal(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'public' modifier is not set in binding", Modifier.isPublic(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isFinal(aBinding.getModifiers()), true);
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
assertTrue(Modifier.isFinal(aBinding.getModifiers()));
}

//public non-sealed
Expand All @@ -333,13 +333,13 @@ public non-sealed class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isPublic(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true);
assertTrue(Modifier.isPublic(a.getModifiers()));
assertTrue(Modifier.isNonSealed(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'public' modifier is not set in binding", Modifier.isPublic(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isPublic(aBinding.getModifiers()));
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
}

//protected non-sealed
Expand All @@ -363,13 +363,13 @@ protected non-sealed class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isProtected(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true);
assertTrue(Modifier.isProtected(a.getModifiers()));
assertTrue(Modifier.isNonSealed(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'public' modifier is not set in binding", Modifier.isProtected(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isProtected(aBinding.getModifiers()));
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
}

//private non-sealed
Expand All @@ -393,13 +393,13 @@ private non-sealed class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isPrivate(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isNonSealed(a.getModifiers()), true);
assertTrue(Modifier.isPrivate(a.getModifiers()));
assertTrue(Modifier.isNonSealed(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

assertEquals("'public' modifier is not set in binding", Modifier.isPrivate(aBinding.getModifiers()), true);
assertEquals("'final' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
assertTrue(Modifier.isPrivate(aBinding.getModifiers()));
assertTrue(Modifier.isNonSealed(aBinding.getModifiers()));
}

//protected abstract
Expand All @@ -423,12 +423,108 @@ protected abstract class A {}
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
TypeDeclaration a = (TypeDeclaration) compilationUnit.types().get(0);

assertEquals("Modifier is not present in AST", Modifier.isProtected(a.getModifiers()), true);
assertEquals("Modifier is not present in AST", Modifier.isAbstract(a.getModifiers()), true);
assertTrue(Modifier.isProtected(a.getModifiers()));
assertTrue(Modifier.isAbstract(a.getModifiers()));

ITypeBinding aBinding = a.resolveBinding();

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);
assertTrue(Modifier.isProtected(aBinding.getModifiers()));
assertTrue(Modifier.isAbstract(aBinding.getModifiers()));
}

//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 14f867a

Please sign in to comment.