Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2446,7 +2446,7 @@ private JRightPadded<Statement> convertTopLevelStatement(SourceUnit unit, ASTNod
if (sourceStartsWith("as")) {
alias = padLeft(sourceBefore("as"), new J.Identifier(randomId(), whitespace(), Markers.EMPTY, emptyList(), name(), null, null));
}
return maybeSemicolon(new J.Import(randomId(), importPrefix, Markers.EMPTY, statik, qualid, alias));
return maybeSemicolon(new J.Import(randomId(), importPrefix, Markers.EMPTY, statik, null, qualid, alias));
}

RewriteGroovyVisitor groovyVisitor = new RewriteGroovyVisitor(node, new RewriteGroovyClassVisitor(unit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ public J visitImport(ImportTree node, Space fmt) {
return new J.Import(randomId(), fmt, Markers.EMPTY,
new JLeftPadded<>(node.isStatic() ? sourceBefore("static") : EMPTY,
node.isStatic(), Markers.EMPTY),
JLeftPadded.build(false),
convert(node.getQualifiedIdentifier()),
null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ public J visitImport(ImportTree node, Space fmt) {
return new J.Import(randomId(), fmt, Markers.EMPTY,
new JLeftPadded<>(node.isStatic() ? sourceBefore("static") : EMPTY,
node.isStatic(), Markers.EMPTY),
JLeftPadded.build(false),
convert(node.getQualifiedIdentifier()), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ public J visitImport(ImportTree node, Space fmt) {
return new J.Import(randomId(), fmt, Markers.EMPTY,
new JLeftPadded<>(node.isStatic() ? sourceBefore("static") : EMPTY,
node.isStatic(), Markers.EMPTY),
JLeftPadded.build(false),
convert(node.getQualifiedIdentifier()), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ public J visitImport(ImportTree node, Space fmt) {
return new J.Import(randomId(), fmt, Markers.EMPTY,
new JLeftPadded<>(node.isStatic() ? sourceBefore("static") : EMPTY,
node.isStatic(), Markers.EMPTY),
new JLeftPadded<>(node.isModule() ? sourceBefore("module") : EMPTY,
node.isModule(), Markers.EMPTY),
convert(node.getQualifiedIdentifier()), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ public J visitImport(ImportTree node, Space fmt) {
return new J.Import(randomId(), fmt, Markers.EMPTY,
new JLeftPadded<>(node.isStatic() ? sourceBefore("static") : EMPTY,
node.isStatic(), Markers.EMPTY),
JLeftPadded.build(false),
convert(node.getQualifiedIdentifier()),
null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.java.MinimumJava25;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

Expand Down Expand Up @@ -239,4 +240,130 @@ void semicolonAfterImports() {
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportBasic() {
rewriteRun(
java(
"""
import module java.base;

public class A {
String s = "test";
List<String> list;
}
"""
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportMultiple() {
rewriteRun(
java(
"""
import module java.base;
import module java.sql;

public class A {
Set<String> s;
Connection conn;
}
""",
spec -> spec.afterRecipe(cu -> assertThat(cu.getImports().stream()
.filter(J.Import::isModule)
.map(J.Import::getQualid)
.map(J.FieldAccess::toString))
.containsExactly(
"java.base",
"java.sql"
))
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportWithRegularImports() {
rewriteRun(
java(
"""
import module java.base;
import java.util.HashMap;
import static java.util.Collections.emptyList;

public class A {
String s;
HashMap<String, Integer> map;
List<String> list = emptyList();
}
"""
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportShadowing() {
rewriteRun(
java(
"""
import module java.base;
// List is ambiguous here because java.awt.List is also in java.base
import java.util.List;
import java.awt.*;

public class A {
List<Date> list;
Color color;
}
"""
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportAggregator() {
rewriteRun(
java(
"""
import module java.se;

public class A {
String s;
List<String> list;
Connection conn;
Path path;
}
"""
)
);
}

@Issue("https://openjdk.org/jeps/511")
@MinimumJava25
@Test
void moduleImportOrdering() {
rewriteRun(
java(
"""
import java.util.List;
import module java.base;
import static java.util.Collections.emptyList;
import module java.sql;
import java.util.*;

public class A {}
"""
)
);
}
}
27 changes: 27 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class AddImport<P> extends JavaIsoVisitor<P> {
@Nullable
private final String member;

@EqualsAndHashCode.Include
private final boolean module;

@EqualsAndHashCode.Include
private final boolean onlyIfReferenced;

Expand All @@ -84,6 +87,18 @@ public AddImport(String type, @Nullable String member, boolean onlyIfReferenced)
this.typeName = lastDotIdx != -1 ? type.substring(lastDotIdx + 1) : type;
this.fullyQualifiedName = type;
this.member = member;
this.module = false;
this.onlyIfReferenced = onlyIfReferenced;
alias = null;
}

public AddImport(String type, @Nullable String member, boolean module, boolean onlyIfReferenced) {
int lastDotIdx = type.lastIndexOf('.');
this.packageName = lastDotIdx != -1 ? type.substring(0, lastDotIdx) : null;
this.typeName = lastDotIdx != -1 ? type.substring(lastDotIdx + 1) : type;
this.fullyQualifiedName = type;
this.member = member;
this.module = module;
this.onlyIfReferenced = onlyIfReferenced;
alias = null;
}
Expand All @@ -93,6 +108,16 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
this.typeName = typeName.replace('.', '$');
this.fullyQualifiedName = packageName == null ? typeName : packageName + "." + typeName;
this.member = member;
this.module = false;
this.onlyIfReferenced = onlyIfReferenced;
this.alias = alias;
}
public AddImport(@Nullable String packageName, String typeName, @Nullable String member, boolean module, @Nullable String alias, boolean onlyIfReferenced) {
this.packageName = packageName;
this.typeName = typeName.replace('.', '$');
this.fullyQualifiedName = packageName == null ? typeName : packageName + "." + typeName;
this.member = member;
this.module = module;
this.onlyIfReferenced = onlyIfReferenced;
this.alias = alias;
}
Expand Down Expand Up @@ -145,6 +170,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
Markers.EMPTY,
new JLeftPadded<>(member == null ? Space.EMPTY : Space.SINGLE_SPACE,
member != null, Markers.EMPTY),
new JLeftPadded<>(module ? Space.SINGLE_SPACE : Space.EMPTY,
module, Markers.EMPTY),
TypeTree.build(fullyQualifiedName +
(member == null ? "" : "." + member)).withPrefix(Space.SINGLE_SPACE),
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ public J visitImport(J.Import import_, PrintOutputCapture<P> p) {
if (import_.isStatic()) {
visitSpace(import_.getPadding().getStatic().getBefore(), Space.Location.STATIC_IMPORT, p);
p.append("static");
} else if (import_.isModule()) {
visitSpace(import_.getPadding().getModule().getBefore(), Space.Location.MODULE_IMPORT, p);
p.append("module");
}
visit(import_.getQualid(), p);
afterSyntax(import_, p);
Expand Down
15 changes: 13 additions & 2 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,22 @@ public void maybeAddImport(String fullyQualifiedName, @Nullable String member, b
int lastDotIdx = fullyQualifiedName.lastIndexOf('.');
String packageName = lastDotIdx != -1 ? fullyQualifiedName.substring(0, lastDotIdx) : null;
String typeName = lastDotIdx != -1 ? fullyQualifiedName.substring(lastDotIdx + 1) : fullyQualifiedName;
maybeAddImport(packageName, typeName, member, null, onlyIfReferenced);
maybeAddImport(packageName, typeName, member, false, null, onlyIfReferenced);
}

public void maybeAddImport(String fullyQualifiedName, boolean module, boolean onlyIfReferenced) {
int lastDotIdx = fullyQualifiedName.lastIndexOf('.');
String packageName = lastDotIdx != -1 ? fullyQualifiedName.substring(0, lastDotIdx) : null;
String typeName = lastDotIdx != -1 ? fullyQualifiedName.substring(lastDotIdx + 1) : fullyQualifiedName;
maybeAddImport(packageName, typeName, null, module, null, onlyIfReferenced);
}

public void maybeAddImport(@Nullable String packageName, String typeName, @Nullable String member, @Nullable String alias, boolean onlyIfReferenced) {
JavaVisitor<P> visitor = service(ImportService.class).addImportVisitor(packageName, typeName, member, alias, onlyIfReferenced);
maybeAddImport(packageName, typeName, member, false, alias, onlyIfReferenced);
}

public void maybeAddImport(@Nullable String packageName, String typeName, @Nullable String member, boolean module, @Nullable String alias, boolean onlyIfReferenced) {
JavaVisitor<P> visitor = service(ImportService.class).addImportVisitor(packageName, typeName, member, module, alias, onlyIfReferenced);
if (!getAfterVisit().contains(visitor)) {
doAfterVisit(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public class ImportService {
public <P> JavaVisitor<P> addImportVisitor(@Nullable String packageName,
String typeName,
@Nullable String member,
boolean module,
@Nullable String alias,
boolean onlyIfReferenced) {
return new AddImport<>(packageName, typeName, member, alias, onlyIfReferenced);
return new AddImport<>(packageName, typeName, member, module, alias, onlyIfReferenced);
}

public <J2 extends J> JavaVisitor<ExecutionContext> shortenAllFullyQualifiedTypeReferences() {
Expand Down
37 changes: 28 additions & 9 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,7 @@ public If withThenPart(JRightPadded<Statement> thenPart) {
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class Import implements Statement, Comparable<Import> {

@Nullable
@NonFinal
transient WeakReference<Padding> padding;
Expand All @@ -2885,6 +2886,9 @@ final class Import implements Statement, Comparable<Import> {

JLeftPadded<Boolean> statik;

@Nullable
JLeftPadded<Boolean> module;

@With
@Getter
FieldAccess qualid;
Expand All @@ -2896,28 +2900,35 @@ public boolean isStatic() {
return statik.getElement();
}

public Import withStatic(boolean statik) {
public J.Import withStatic(boolean statik) {
return getPadding().withStatic(this.statik.withElement(statik));
}

public boolean isModule() {
return module != null ? module.getElement() : false;
}

public J.Import withModule(boolean module) {
return this.module != null ?
getPadding().withModule(this.module.withElement(module)) :
getPadding().withModule(JLeftPadded.build(module));
}

public J.@Nullable Identifier getAlias() {
if (alias == null) {
return null;
}
return alias.getElement();
return alias != null ? alias.getElement() : null;
}

public J.Import withAlias(J.@Nullable Identifier alias) {
if (this.alias == null) {
if (alias == null) {
return this;
}
return new J.Import(null, id, prefix, markers, statik, qualid, JLeftPadded
return new J.Import(null, id, prefix, markers, statik, module, qualid, JLeftPadded
.build(alias)
.withBefore(Space.format(" ")));
}
if (alias == null) {
return new J.Import(null, id, prefix, markers, statik, qualid, null);
return new J.Import(null, id, prefix, markers, statik, module, qualid, null);
}
return getPadding().withAlias(this.alias.withElement(alias));
}
Expand Down Expand Up @@ -3071,15 +3082,23 @@ public JLeftPadded<Boolean> getStatic() {
}

public Import withStatic(JLeftPadded<Boolean> statik) {
return t.statik == statik ? t : new Import(t.id, t.prefix, t.markers, statik, t.qualid, t.alias);
return t.statik == statik ? t : new Import(t.id, t.prefix, t.markers, statik, t.module, t.qualid, t.alias);
}

public JLeftPadded<Boolean> getModule() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at least the getter here also needs to be @Nullable.

return t.module;
}

public Import withModule(JLeftPadded<Boolean> module) {
return t.module == module ? t : new Import(t.id, t.prefix, t.markers, t.statik, module, t.qualid, t.alias);
}

public @Nullable JLeftPadded<J.Identifier> getAlias() {
return t.alias;
}

public Import withAlias(@Nullable JLeftPadded<J.Identifier> alias) {
return t.alias == alias ? t : new Import(t.id, t.prefix, t.markers, t.statik, t.qualid, alias);
return t.alias == alias ? t : new Import(t.id, t.prefix, t.markers, t.statik, t.module, t.qualid, alias);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ public enum Location {
METHOD_INVOCATION_PREFIX,
METHOD_SELECT_SUFFIX,
MODIFIER_PREFIX,
MODULE_IMPORT,
MULTI_CATCH_PREFIX,
NAMED_VARIABLE_SUFFIX,
NEW_ARRAY_INITIALIZER,
Expand Down
Loading