Skip to content

Commit

Permalink
In-corporated code review changes and added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Nov 13, 2024
1 parent d5f45dd commit 5244291
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public interface ExtraCompilerModifiers { // modifier constant
final int AccUnresolved = ASTNode.Bit26;
final int AccBlankFinal = ASTNode.Bit27; // for blank final variables
final int AccIsDefaultConstructor = ASTNode.Bit27; // for default constructor
final int AccNonSealed = ASTNode.Bit27; // for class/interface
final int AccNonSealed = ASTNode.Bit29; // for class/interface
final int AccLocallyUsed = ASTNode.Bit28; // used to diagnose unused (a) private/local members or (b) members of private classes
// generally set when actual usage has been detected
// or, (b) when member of a private class is exposed via a non-private subclass
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=328281
final int AccVisibilityMASK = ClassFileConstants.AccPublic | ClassFileConstants.AccProtected | ClassFileConstants.AccPrivate;

final int AccSealed = ASTNode.Bit29; // used for class/interface to set sealed
final int AccSealed = ASTNode.Bit26; // used for class/interface to set sealed
final int AccOverriding = ASTNode.Bit29; // record fact a method overrides another one
final int AccImplementing = ASTNode.Bit30; // record fact a method implements another one (it is concrete and overrides an abstract one)
final int AccImplicitlyDeclared = ASTNode.Bit30; // used for implicitly declared classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,10 @@ public final int getAccessFlags() {
return this.modifiers & ExtraCompilerModifiers.AccJustFlag;
}

public final int getAccessFlagsForSealedAndNonSealed() {
return ((this.modifiers >>> 16) & 0xFFFF) & ExtraCompilerModifiers.AccJustFlag;
}

/**
* @return the JSR 175 annotations for this type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,35 @@ non-sealed class C extends A {}
ITypeBinding aBinding = a.resolveBinding();
assertEquals("'non-sealed' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
}

public void test003_c() 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 class A permits B, C {}
final class B extends A {}
non-sealed class C extends A {}
""";

astParser.setSource(source.toCharArray());

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("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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -588,16 +588,13 @@ public int getKind() {
public int getModifiers() {
if (isClass()) {
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
int k = referenceBinding.getAccessFlags();
final int accessFlags = k & VALID_MODIFIERS;
if (referenceBinding.isSealed() || referenceBinding.isNonSealed()) {
return referenceBinding.getAccessFlagsForSealedAndNonSealed();
}
final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
if (referenceBinding.isAnonymousType()) {
return accessFlags & ~Modifier.FINAL;
} else if (referenceBinding.isSealed()) {
return Modifier.SEALED;
} else if (referenceBinding.isNonSealed()) {
return Modifier.NON_SEALED;
}

return accessFlags;
} else if (isAnnotation()) {
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
Expand Down

0 comments on commit 5244291

Please sign in to comment.