Skip to content

Commit b7695d8

Browse files
committed
Add explicit imports for record classes past Java 14+
1 parent a8d3aff commit b7695d8

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.lang;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.search.UsesType;
24+
import org.openrewrite.java.tree.J;
25+
import org.openrewrite.java.tree.JavaSourceFile;
26+
import org.openrewrite.java.tree.JavaType;
27+
28+
public class ExplicitRecordImport extends Recipe {
29+
@Override
30+
public String getDisplayName() {
31+
return "Add explicit import for `Record` classes";
32+
}
33+
34+
@Override
35+
public String getDescription() {
36+
return "Add explicit import for `Record` classes when upgrading past Java 14+, to avoid conflicts with `java.lang.Record`.";
37+
}
38+
39+
@Override
40+
public TreeVisitor<?, ExecutionContext> getVisitor() {
41+
return Preconditions.check(
42+
new UsesType<>("*..Record", false),
43+
new JavaIsoVisitor<ExecutionContext>() {
44+
@Override
45+
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
46+
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
47+
if (javaSourceFile != null) {
48+
for (JavaType type : cu.getTypesInUse().getTypesInUse()) {
49+
if (type instanceof JavaType.FullyQualified) {
50+
JavaType.FullyQualified ref = (JavaType.FullyQualified) type;
51+
if ("Record".equals(ref.getClassName()) && !ref.getPackageName().startsWith("java.lang")) {
52+
maybeAddImport(ref.getFullyQualifiedName(), false);
53+
}
54+
}
55+
}
56+
}
57+
return cu;
58+
}
59+
}
60+
);
61+
}
62+
}

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ recipeList:
3434
- org.openrewrite.java.migrate.RemovedRuntimeTraceMethods
3535
- org.openrewrite.java.migrate.RemovedToolProviderConstructor
3636
- org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors
37+
- org.openrewrite.java.migrate.lang.ExplicitRecordImport
3738
- org.openrewrite.java.migrate.lang.UseTextBlocks:
3839
convertStringsWithoutNewlines: false
3940
- org.openrewrite.java.migrate.lang.StringFormatted:
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.lang;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.Issue;
21+
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
23+
import org.openrewrite.test.RewriteTest;
24+
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class ExplicitRecordImportTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec.recipe(new ExplicitRecordImport())
32+
//language=java
33+
.parser(JavaParser.fromJavaVersion().dependsOn("""
34+
package com.acme.music;
35+
public class Record {
36+
String name;
37+
}
38+
"""
39+
)
40+
);
41+
}
42+
43+
@DocumentExample
44+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540")
45+
@Test
46+
void addImportFromSamePackage() {
47+
rewriteRun(
48+
//language=java
49+
java(
50+
"""
51+
package com.acme.music;
52+
53+
public class Test {
54+
Record record;
55+
}
56+
""",
57+
"""
58+
package com.acme.music;
59+
60+
import com.acme.music.Record;
61+
62+
public class Test {
63+
Record record;
64+
}
65+
"""
66+
)
67+
);
68+
}
69+
70+
71+
@Test
72+
void noChangeIfAlreadyFullyQualified() {
73+
rewriteRun(
74+
//language=java
75+
java(
76+
"""
77+
package com.acme.music;
78+
79+
public class Test {
80+
com.acme.music.Record record;
81+
}
82+
"""
83+
)
84+
);
85+
}
86+
87+
88+
@Test
89+
void noChangeIfAlreadyImported() {
90+
rewriteRun(
91+
//language=java
92+
java(
93+
"""
94+
package com.acme.music;
95+
96+
import com.acme.music.Record;
97+
98+
public class Test {
99+
Record record;
100+
}
101+
"""
102+
)
103+
);
104+
}
105+
106+
@Test
107+
void noImportAddedForJavaLangRecord() {
108+
rewriteRun(
109+
//language=java
110+
java(
111+
"""
112+
package foo.bar;
113+
114+
public class Test {
115+
Record record;
116+
}
117+
"""
118+
)
119+
);
120+
}
121+
}

0 commit comments

Comments
 (0)