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
+ parser to record modifier start position of imports
+ avoid code duplication in SourceElementParser
+ new list ImportDeclaration.modifiers()
  + at 23 even 'static' is represented in that list
+ existing accessors may scan that list if present

fixes eclipse-jdt#2834
  • Loading branch information
stephan-herrmann committed Aug 19, 2024
1 parent 9b33f16 commit a922863
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ImportReference extends ASTNode {
public int declarationSourceStart;
public int declarationSourceEnd;
public int modifiers; // 1.5 addition for static imports
public int modifiersSourceStart;
public Annotation[] annotations;
// star end position
public int trailingStarPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8894,7 +8894,13 @@ protected void consumeSingleModifierImportDeclarationName(int modifier) {
pushOnAstStack(impt = new ImportReference(tokens, positions, false, modifier));

this.modifiers = ClassFileConstants.AccDefault;
this.modifiersSourceStart = -1; // <-- see comment into modifiersFlag(int)
// 'module' stores position on stack, 'static' sets modifiersSourceStart:
if (modifier == ClassFileConstants.AccModule) {
impt.modifiersSourceStart = this.intStack[this.intPtr--];
} else { // static
impt.modifiersSourceStart = this.modifiersSourceStart;
}
this.modifiersSourceStart = -1; // see checkAndSetModifiers()

if (this.currentToken == TokenNameSEMICOLON){
impt.declarationSourceEnd = this.scanner.currentPosition - 1;
Expand Down Expand Up @@ -9334,6 +9340,7 @@ protected void consumeStaticImportOnDemandDeclarationName() {
// star end position
impt.trailingStarPosition = this.intStack[this.intPtr--];
this.modifiers = ClassFileConstants.AccDefault;
impt.modifiersSourceStart = this.modifiersSourceStart;
this.modifiersSourceStart = -1; // <-- see comment into modifiersFlag(int)

if (this.currentToken == TokenNameSEMICOLON){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*******************************************************************************
* Copyright (c) 2024 GK Software SE and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* This is an implementation of an early-draft specification developed under the Java
* Community Process (JCP) and is made available for testing and evaluation purposes
* only. The code is not compatible with any specification of the JCP.
*
* Contributors:
* Stephan Herrmann - Initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import java.util.List;

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.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Modifier;

import junit.framework.Test;

public class ASTConverter_23Test extends ConverterTestSetup {

ICompilationUnit workingCopy;

public void setUpSuite() throws Exception {
super.setUpSuite();
this.ast = AST.newAST(getAST23(), false);
this.currentProject = getJavaProject("Converter_23");
this.currentProject.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_23);
this.currentProject.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_23);
this.currentProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_23);
this.currentProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
this.currentProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
}

public ASTConverter_23Test(String name) {
super(name);
}

public static Test suite() {
return buildModelTestSuite(ASTConverter_23Test.class);
}

static int getAST23() {
return AST.JLS23;
}
protected void tearDown() throws Exception {
super.tearDown();
if (this.workingCopy != null) {
this.workingCopy.discardWorkingCopy();
this.workingCopy = null;
}
}


public void test001() throws CoreException {
String contents = """
package p;
import module java.base;
import static java.lang.System.out;
class X {
void m() {
out.println(Map.class.toString());
}
}
""";
this.workingCopy = getWorkingCopy("/Converter_23/src/p/X.java", true/*resolve*/);
ASTNode node = buildAST(
contents,
this.workingCopy);
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit compilationUnit = (CompilationUnit) node;
assertProblemsSize(compilationUnit, 0);
List<ImportDeclaration> imports = compilationUnit.imports();
assertEquals("Incorrect no of imports", 2, imports.size());

{
ImportDeclaration imp = imports.get(0);
assertEquals("Incorrect modifier bits", Modifier.MODULE, imp.getModifiers());
assertEquals("Incorrect no of modifiers", 1, imp.modifiers().size());
Modifier mod = (Modifier) imp.modifiers().get(0);
assertEquals("Incorrect modifier", "module", mod.getKeyword().toString());
assertEquals("Incorrect modifier", Modifier.ModifierKeyword.MODULE_KEYWORD, mod.getKeyword());
assertEquals("Incorrect position", 18, mod.getStartPosition());
assertEquals("Incorrect content", "module", contents.substring(mod.getStartPosition(), mod.getStartPosition()+6));
assertEquals("Incorrect name", "java.base", imp.getName().toString());
}
{
ImportDeclaration imp = imports.get(1);
assertEquals("Incorrect modifier bits", Modifier.STATIC, imp.getModifiers());
assertEquals("Incorrect no of modifiers", 1, imp.modifiers().size());
Modifier mod = (Modifier) imp.modifiers().get(0);
assertEquals("Incorrect modifier", "static", mod.getKeyword().toString());
assertEquals("Incorrect modifier", Modifier.ModifierKeyword.STATIC_KEYWORD, mod.getKeyword());
assertEquals("Incorrect position", 43, mod.getStartPosition());
assertEquals("Incorrect content", "static", contents.substring(mod.getStartPosition(), mod.getStartPosition()+6));
assertEquals("Incorrect name", "java.lang.System.out", imp.getName().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static Class[] getAllTestClasses() {
ASTConverter_15Test.class,
ASTConverter_16Test.class,
ASTConverter_17Test.class,
ASTConverter_23Test.class,
ASTConverter_GuardedPattern_Test.class,
ASTConverter_RecordPattern_Test.class,
ASTConverterSuperAfterStatements.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="var" path="CONVERTER_JCL_21_LIB" sourcepath="CONVERTER_JCL_21_SRC" rootpath="CONVERTER_JCL_SRCROOT"/>
<classpathentry kind="var" path="CONVERTER_JCL_22_LIB" sourcepath="CONVERTER_JCL_22_SRC" rootpath="CONVERTER_JCL_SRCROOT">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
14 changes: 14 additions & 0 deletions org.eclipse.jdt.core/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@
</filter>
</resource>
<resource path="dom/org/eclipse/jdt/core/dom/ImportDeclaration.java" type="org.eclipse.jdt.core.dom.ImportDeclaration">
<filter comment="Evolution to support module imports" id="336658481">
<message_arguments>
<message_argument value="org.eclipse.jdt.core.dom.ImportDeclaration"/>
<message_argument value="MODIFIERS_PROPERTY"/>
</message_arguments>
</filter>
<filter id="576778288">
<message_arguments>
<message_argument value="ASTNode"/>
Expand Down Expand Up @@ -251,6 +257,14 @@
</message_arguments>
</filter>
</resource>
<resource path="dom/org/eclipse/jdt/core/dom/Modifier.java" type="org.eclipse.jdt.core.dom.Modifier$ModifierKeyword">
<filter comment="Evolution to support module imports" id="336658481">
<message_arguments>
<message_argument value="org.eclipse.jdt.core.dom.Modifier.ModifierKeyword"/>
<message_argument value="MODULE_KEYWORD"/>
</message_arguments>
</filter>
</resource>
<resource path="dom/org/eclipse/jdt/core/dom/ModuleQualifiedName.java" type="org.eclipse.jdt.core.dom.ModuleQualifiedName">
<filter comment="Java 15 Merge" id="576778288">
<message_arguments>
Expand Down
3 changes: 3 additions & 0 deletions org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,9 @@ public List newModifiers(int flags) {
if (Modifier.isNonSealed(flags)) {
result.add(newModifier(Modifier.ModifierKeyword.NON_SEALED_KEYWORD));
}
if (Modifier.isModule(flags)) {
result.add(newModifier(Modifier.ModifierKeyword.MODULE_KEYWORD));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3687,16 +3687,31 @@ public ImportDeclaration convertImport(org.eclipse.jdt.internal.compiler.ast.Imp
importDeclaration.setOnDemand(onDemand);
int modifiers = importReference.modifiers;
if (modifiers != ClassFileConstants.AccDefault) {
switch(this.ast.apiLevel) {
case AST.JLS2_INTERNAL :
if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
} else if (this.ast.apiLevel < AST.JLS23_INTERNAL) {
if (modifiers == ClassFileConstants.AccStatic) {
importDeclaration.setStatic(true);
} else {
importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
break;
default :
if (modifiers == ClassFileConstants.AccStatic) {
importDeclaration.setStatic(true);
} else {
importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
}
}
} else {
ModifierKeyword keyword = null;
switch (modifiers) {
case ClassFileConstants.AccStatic:
keyword = ModifierKeyword.STATIC_KEYWORD;
break;
case ClassFileConstants.AccModule:
keyword = ModifierKeyword.MODULE_KEYWORD;
break;
}
if (keyword != null) {
Modifier newModifier = this.ast.newModifier(keyword);
newModifier.setSourceRange(importReference.modifiersSourceStart, keyword.toString().length());
importDeclaration.modifiers().add(newModifier);
} else {
importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
}
}
}
if (this.resolveBindings) {
Expand Down
Loading

0 comments on commit a922863

Please sign in to comment.