Skip to content

Commit

Permalink
Added the unit tests and added the modifiers to the resolve bindings
Browse files Browse the repository at this point in the history
for sealed and non-sealed modifiers
  • Loading branch information
subyssurendran666 committed Nov 13, 2024
1 parent 9398905 commit d5f45dd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImplicitTypeDeclaration;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.*;

public class ASTConverter_23Test extends ConverterTestSetup {

Expand Down Expand Up @@ -133,4 +127,64 @@ void main() {
assertEquals("Not a Block", block.getNodeType(), ASTNode.BLOCK);
assertEquals("Block startPosition is not correct", block.getStartPosition(), 21);
}

public void test003_a() 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 ="""
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);
}

public void test003_b() 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 ="""
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(2);

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

ITypeBinding aBinding = a.resolveBinding();
assertEquals("'non-sealed' modifier is not set in binding", Modifier.isNonSealed(aBinding.getModifiers()), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,16 @@ public int getKind() {
public int getModifiers() {
if (isClass()) {
ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
int k = referenceBinding.getAccessFlags();
final int accessFlags = k & 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 d5f45dd

Please sign in to comment.