Skip to content

Commit 17de874

Browse files
authored
Add explicit imports for Record classes past Java 14+ (#630)
* Add explicit imports for record classes past Java 14+ * Disable unlikely failing test for now * No need to add unconditionally
1 parent a8d3aff commit 17de874

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-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());
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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.Disabled;
19+
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.DocumentExample;
21+
import org.openrewrite.Issue;
22+
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.test.RecipeSpec;
24+
import org.openrewrite.test.RewriteTest;
25+
26+
import static org.openrewrite.java.Assertions.java;
27+
28+
class ExplicitRecordImportTest implements RewriteTest {
29+
30+
@Override
31+
public void defaults(RecipeSpec spec) {
32+
spec.recipe(new ExplicitRecordImport())
33+
//language=java
34+
.parser(JavaParser.fromJavaVersion().dependsOn("""
35+
package com.acme.music;
36+
public class Record {
37+
String name;
38+
}
39+
"""
40+
)
41+
);
42+
}
43+
44+
@DocumentExample
45+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540")
46+
@Test
47+
void addImportFromSamePackage() {
48+
rewriteRun(
49+
//language=java
50+
java(
51+
"""
52+
package com.acme.music;
53+
54+
public class Test {
55+
Record record;
56+
}
57+
""",
58+
"""
59+
package com.acme.music;
60+
61+
import com.acme.music.Record;
62+
63+
public class Test {
64+
Record record;
65+
}
66+
"""
67+
)
68+
);
69+
}
70+
71+
72+
@Test
73+
@Disabled("Not handled yet; deemed unlikely")
74+
void noChangeIfAlreadyFullyQualified() {
75+
rewriteRun(
76+
//language=java
77+
java(
78+
"""
79+
package com.acme.music;
80+
81+
public class Test {
82+
com.acme.music.Record record;
83+
}
84+
"""
85+
)
86+
);
87+
}
88+
89+
90+
@Test
91+
void noChangeIfAlreadyImported() {
92+
rewriteRun(
93+
//language=java
94+
java(
95+
"""
96+
package com.acme.music;
97+
98+
import com.acme.music.Record;
99+
100+
public class Test {
101+
Record record;
102+
}
103+
"""
104+
)
105+
);
106+
}
107+
108+
@Test
109+
void noImportAddedForJavaLangRecord() {
110+
rewriteRun(
111+
//language=java
112+
java(
113+
"""
114+
package foo.bar;
115+
116+
public class Test {
117+
Record record;
118+
}
119+
"""
120+
)
121+
);
122+
}
123+
}

0 commit comments

Comments
 (0)