From 42ee04d37b70fc694a7c2eea022e9aa689f7b330 Mon Sep 17 00:00:00 2001 From: Suby S Surendran Date: Thu, 12 Dec 2024 13:56:09 +0530 Subject: [PATCH] Handled sealed & non-sealed interfaces through ITypeBinding --- .../core/tests/dom/ASTConverter_23Test.java | 96 +++++++++++++++++++ .../org/eclipse/jdt/core/dom/TypeBinding.java | 7 ++ 2 files changed, 103 insertions(+) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_23Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_23Test.java index 62f634a3ed7..a507cf3cd71 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_23Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_23Test.java @@ -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 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 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 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())); + } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java index 6ae56ee242c..deefed19206 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java @@ -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()) {