-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/org/openrewrite/java/migrate/lang/ExplicitRecordImport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lang; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaSourceFile; | ||
import org.openrewrite.java.tree.JavaType; | ||
|
||
public class ExplicitRecordImport extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Add explicit import for `Record` classes"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Add explicit import for `Record` classes when upgrading past Java 14+, to avoid conflicts with `java.lang.Record`."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new UsesType<>("*..Record", false), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { | ||
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class); | ||
if (javaSourceFile != null) { | ||
for (JavaType type : cu.getTypesInUse().getTypesInUse()) { | ||
if (type instanceof JavaType.FullyQualified) { | ||
JavaType.FullyQualified ref = (JavaType.FullyQualified) type; | ||
if ("Record".equals(ref.getClassName()) && !ref.getPackageName().startsWith("java.lang")) { | ||
maybeAddImport(ref.getFullyQualifiedName()); | ||
} | ||
} | ||
} | ||
} | ||
return cu; | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/test/java/org/openrewrite/java/migrate/lang/ExplicitRecordImportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate.lang; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.Issue; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class ExplicitRecordImportTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ExplicitRecordImport()) | ||
//language=java | ||
.parser(JavaParser.fromJavaVersion().dependsOn(""" | ||
package com.acme.music; | ||
public class Record { | ||
String name; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@DocumentExample | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/540") | ||
@Test | ||
void addImportFromSamePackage() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
package com.acme.music; | ||
public class Test { | ||
Record record; | ||
} | ||
""", | ||
""" | ||
package com.acme.music; | ||
import com.acme.music.Record; | ||
public class Test { | ||
Record record; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
|
||
@Test | ||
@Disabled("Not handled yet; deemed unlikely") | ||
void noChangeIfAlreadyFullyQualified() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
package com.acme.music; | ||
public class Test { | ||
com.acme.music.Record record; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
|
||
@Test | ||
void noChangeIfAlreadyImported() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
package com.acme.music; | ||
import com.acme.music.Record; | ||
public class Test { | ||
Record record; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noImportAddedForJavaLangRecord() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
package foo.bar; | ||
public class Test { | ||
Record record; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |