Skip to content

Commit

Permalink
[23] DOM support for JEP 476: Module Import Declarations (Preview)
Browse files Browse the repository at this point in the history
+ adaptations in other parts of DOM implementation
+ consistently use Modifier even for static at JLS23
+ enable DOM testing at JLS23

fixes eclipse-jdt#2834
  • Loading branch information
stephan-herrmann committed Aug 20, 2024
1 parent b9c89be commit 682cda4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.eclipse.jdt.core.dom.MethodRef;
import org.eclipse.jdt.core.dom.MethodRefParameter;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ModuleDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NormalAnnotation;
Expand Down Expand Up @@ -151,6 +152,13 @@ public class ASTTest extends org.eclipse.jdt.core.tests.junit.extension.TestCase
*/
protected static final int AST_INTERNAL_JLS9 = AST.JLS9;

/**
* Internal synonym for constant AST.JSL9
* to alleviate deprecation warnings once AST.JLS9 is deprecated in future.
* @deprecated
*/
protected static final int AST_INTERNAL_JLS23 = AST.JLS23;

class CheckPositionsMatcher extends ASTMatcher {

public CheckPositionsMatcher() {
Expand Down Expand Up @@ -862,6 +870,7 @@ public static Test suite() {
suite.addTest(new ASTTest(methods[i].getName(), JLS3_INTERNAL));
suite.addTest(new ASTTest(methods[i].getName(), AST.JLS4));
suite.addTest(new ASTTest(methods[i].getName(), getJLS8()));
suite.addTest(new ASTTest(methods[i].getName(), AST_INTERNAL_JLS23));
}
}
return suite;
Expand Down Expand Up @@ -2734,7 +2743,19 @@ public void set(ASTNode value) {
assertTrue(this.ast.modificationCount() > previousCount);
assertTrue(x.isOnDemand() == true);

if (this.ast.apiLevel() >= JLS3_INTERNAL) {
if (this.ast.apiLevel() >= AST_INTERNAL_JLS23) {
Modifier mod = this.ast.newModifier(ModifierKeyword.STATIC_KEYWORD);
x.modifiers().add(mod);
assertTrue(this.ast.modificationCount() > previousCount);
assertTrue(x.isStatic() == true);
previousCount = this.ast.modificationCount();
x.modifiers().clear();
mod = this.ast.newModifier(ModifierKeyword.MODULE_KEYWORD);
x.modifiers().add(mod);
assertTrue(this.ast.modificationCount() > previousCount);
assertTrue(x.modifiers().size() == 1);
assertEquals(((Modifier) x.modifiers().get(0)).getKeyword(), ModifierKeyword.MODULE_KEYWORD);
} else if (this.ast.apiLevel() >= JLS3_INTERNAL) {
x.setStatic(true);
assertTrue(this.ast.modificationCount() > previousCount);
assertTrue(x.isStatic() == true);
Expand Down Expand Up @@ -6643,7 +6664,11 @@ public void testSwitchCase() {
assertTrue(x.getParent() == null);
assertTrue(x.getExpression().getParent() == x);
assertTrue(x.getLeadingComment() == null);
assertTrue(!x.isDefault());
if (this.ast.apiLevel() < AST_INTERNAL_JLS23) {
assertTrue(!x.isDefault());
} else {
assertEquals(0, x.expressions().size());
}
assertTrue(x.getNodeType() == ASTNode.SWITCH_CASE);
assertTrue(x.structuralPropertiesForType() ==
SwitchCase.propertyDescriptors(this.ast.apiLevel()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,11 @@ public boolean match(ImportDeclaration node, Object other) {
return false;
}
ImportDeclaration o = (ImportDeclaration) other;
if (node.getAST().apiLevel >= AST.JLS3_INTERNAL) {
if (node.getAST().apiLevel >= AST.JLS23_INTERNAL) {
if (!safeSubtreeListMatch(node.modifiers(), o.modifiers())) {
return false;
}
} else if (node.getAST().apiLevel >= AST.JLS3_INTERNAL) {
if (node.isStatic() != o.isStatic()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

/**
* Import declaration AST node type.
*
Expand Down Expand Up @@ -202,14 +204,14 @@ public List modifiers() {
* @since 3.39
*/
public int getModifiers() {
int computedmodifierFlags = this.isStatic ? Modifier.STATIC : Modifier.NONE;
if (this.modifiers == null) {
// JLS3 behavior (for JLS2 this is constantly 0)
return computedmodifierFlags;
return this.isStatic ? Modifier.STATIC : Modifier.NONE;
}
// JLS23 behavior - convenience method
// performance could be improved by caching computed flags
// but this would require tracking changes to this.modifiers
int computedmodifierFlags = Modifier.NONE;
for (Object x : modifiers()) {
if (x instanceof Modifier modifier) {
computedmodifierFlags |= modifier.getKeyword().toFlagValue();
Expand Down Expand Up @@ -411,13 +413,35 @@ public boolean isStatic() {
/**
* Sets whether this import declaration is a static import (added in JLS3 API).
*
* Note, that in JLS23 API this method creates a {@link Modifier} without source positions
* (or removes, if {@code isStatic == false}), so it should not be invoked in situations where
* valid source positions are required.
*
* @param isStatic <code>true</code> if this is a static import,
* and <code>false</code> if this is a regular import
* @exception UnsupportedOperationException if this operation is used in
* a JLS2 AST
* @since 3.1
*
* @see #modifiers()
*/
@SuppressWarnings("unchecked")
public void setStatic(boolean isStatic) {
if (this.ast.apiLevel >= AST.JLS23_INTERNAL) {
List<Modifier> mods = modifiers();
for (Modifier mod : mods) {
if (mod.isStatic()) {
if (!isStatic)
this.modifiers.remove(mod);
return;
}
}
if (isStatic) {
Modifier newMod = this.ast.newModifier(ModifierKeyword.STATIC_KEYWORD);
this.modifiers.add(newMod);
}
return;
}
unsupportedIn2();
preValueChange(STATIC_PROPERTY);
this.isStatic = isStatic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public class NaiveASTFlattener extends ASTVisitor {
*/
private static final int JLS21 = AST.JLS21;

/**
* Internal synonym for {@link AST#JLS23}. Use to alleviate
* deprecation warnings.
* @deprecated
*/
private static final int JLS23 = AST.JLS23;

/**
* The string buffer into which the serialized representation of the AST is
* written.
Expand Down Expand Up @@ -845,7 +852,11 @@ public boolean visit(IfStatement node) {
public boolean visit(ImportDeclaration node) {
printIndent();
this.buffer.append("import ");//$NON-NLS-1$
if (node.getAST().apiLevel() >= JLS3) {
if (node.getAST().apiLevel() >= JLS23) {
if (node.modifiers().size() == 1) {
this.buffer.append(((Modifier) node.modifiers().get(0)).getKeyword().toString()).append(' ');
}
} else if (node.getAST().apiLevel() >= JLS3) {
if (node.isStatic()) {
this.buffer.append("static ");//$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public class ASTRewriteFlattener extends ASTVisitor {
/** @deprecated using deprecated code */
private static final int JLS14_INTERNAL = AST.JLS14;

/** @deprecated using deprecated code */
private static final int JLS23_INTERNAL = AST.JLS23;

public static String asString(ASTNode node, RewriteEventStore store) {
ASTRewriteFlattener flattener= new ASTRewriteFlattener(store);
node.accept(flattener);
Expand Down Expand Up @@ -603,7 +606,12 @@ public boolean visit(IfStatement node) {
@Override
public boolean visit(ImportDeclaration node) {
this.result.append("import "); //$NON-NLS-1$
if (node.getAST().apiLevel() >= JLS3_INTERNAL) {
if (node.getAST().apiLevel() >= JLS23_INTERNAL) {
List<Modifier> modifiers = node.modifiers();
for (Modifier modifier : modifiers) {
this.result.append(modifier.getKeyword().toString()).append(' ');
}
} else if (node.getAST().apiLevel() >= JLS3_INTERNAL) {
if (getBooleanAttribute(node, ImportDeclaration.STATIC_PROPERTY)) {
this.result.append("static ");//$NON-NLS-1$
}
Expand Down

0 comments on commit 682cda4

Please sign in to comment.