Skip to content

Commit

Permalink
ShortenFullyQualifiedTypeReferences and nested types
Browse files Browse the repository at this point in the history
By default, don't import nested types, but only containing type.

Issue: openrewrite#3094
  • Loading branch information
knutwannheden committed Apr 17, 2023
1 parent 93caaf2 commit 8c28189
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ class T {
);
}

@Test
void nestedTypeReference() {
rewriteRun(
java(
"""
class T {
java.util.Map.Entry<String, String> mapEntry;
}
""",
"""
import java.util.Map;
class T {
Map.Entry<String, String> mapEntry;
}
"""
)
);
}

@Test
void withinStaticFieldAccess() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public J visitImport(J.Import impoort, ExecutionContext ctx) {
@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
JavaType type = fieldAccess.getType();
if (type instanceof JavaType.Class) {
if (type instanceof JavaType.Class && ((JavaType.Class) type).getOwningClass() == null) {
String simpleName = fieldAccess.getSimpleName();
if (type.equals(importedTypes.get(simpleName))) {
return fieldAccess.getName().withPrefix(fieldAccess.getPrefix());
Expand Down

0 comments on commit 8c28189

Please sign in to comment.