-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'openrewrite:main' into lombok/convert-equals-negligently
- Loading branch information
Showing
28 changed files
with
1,689 additions
and
90 deletions.
There are no files selected for viewing
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
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
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
101 changes: 101 additions & 0 deletions
101
src/main/java/org/openrewrite/java/migrate/guava/NoMapsAndSetsWithExpectedSize.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,101 @@ | ||
/* | ||
* Copyright 2025 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.guava; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaCoordinates; | ||
|
||
public class NoMapsAndSetsWithExpectedSize extends Recipe { | ||
|
||
private static final MethodMatcher NEW_HASHMAP = new MethodMatcher("com.google.common.collect.Maps newHashMapWithExpectedSize(int)", false); | ||
private static final MethodMatcher NEW_LINKED_HASHMAP = new MethodMatcher("com.google.common.collect.Maps newLinkedHashMapWithExpectedSize(int)", false); | ||
private static final MethodMatcher NEW_HASHSET = new MethodMatcher("com.google.common.collect.Sets newHashSetWithExpectedSize(int)", false); | ||
private static final MethodMatcher NEW_LINKED_HASHSET = new MethodMatcher("com.google.common.collect.Sets newLinkedHashSetWithExpectedSize(int)", false); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Prefer JDK methods for Maps and Sets of an expected size"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Prefer Java 19+ methods to create Maps and Sets of an expected size instead of using Guava methods."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.and( | ||
new UsesJavaVersion<>(19), | ||
Preconditions.or( | ||
new UsesMethod<>(NEW_HASHMAP), | ||
new UsesMethod<>(NEW_LINKED_HASHMAP), | ||
new UsesMethod<>(NEW_HASHSET), | ||
new UsesMethod<>(NEW_LINKED_HASHSET) | ||
) | ||
), | ||
new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation j = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
if (NEW_HASHMAP.matches(j)) { | ||
maybeRemoveImport("com.google.common.collect.Maps"); | ||
maybeAddImport("java.util.HashMap"); | ||
JavaCoordinates coordinates = j.getCoordinates().replace(); | ||
return JavaTemplate.builder("new HashMap<>(#{any()})") | ||
.imports("java.util.HashMap") | ||
.build() | ||
.apply(getCursor(), coordinates, j.getArguments().toArray()); | ||
} else if (NEW_LINKED_HASHMAP.matches(j)) { | ||
maybeRemoveImport("com.google.common.collect.Maps"); | ||
maybeAddImport("java.util.LinkedHashMap"); | ||
JavaCoordinates coordinates = j.getCoordinates().replace(); | ||
return JavaTemplate.builder("new LinkedHashMap<>(#{any()})") | ||
.imports("java.util.LinkedHashMap") | ||
.build() | ||
.apply(getCursor(), coordinates, j.getArguments().toArray()); | ||
} else if (NEW_HASHSET.matches(j)) { | ||
maybeRemoveImport("com.google.common.collect.Sets"); | ||
maybeAddImport("java.util.HashSet"); | ||
JavaCoordinates coordinates = j.getCoordinates().replace(); | ||
return JavaTemplate.builder("new HashSet<>(#{any()})") | ||
.imports("java.util.HashSet") | ||
.build() | ||
.apply(getCursor(), coordinates, j.getArguments().toArray()); | ||
} else if (NEW_LINKED_HASHSET.matches(j)) { | ||
maybeRemoveImport("com.google.common.collect.Sets"); | ||
maybeAddImport("java.util.LinkedHashSet"); | ||
JavaCoordinates coordinates = j.getCoordinates().replace(); | ||
return JavaTemplate.builder("new LinkedHashSet<>(#{any()})") | ||
.imports("java.util.LinkedHashSet") | ||
.build() | ||
.apply(getCursor(), coordinates, j.getArguments().toArray()); | ||
} | ||
return j; | ||
} | ||
} | ||
); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/main/java/org/openrewrite/java/migrate/lombok/UseNoArgsConstructor.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,82 @@ | ||
/* | ||
* Copyright 2025 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.lombok; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
import static java.util.Comparator.comparing; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UseNoArgsConstructor extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
//language=markdown | ||
return "Use `@NoArgsConstructor` where applicable"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
//language=markdown | ||
return "Prefer the Lombok `@NoArgsConstructor` annotation over explicitly written out constructors."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
if (method.isConstructor() && | ||
method.getParameters().get(0) instanceof J.Empty && | ||
method.getBody() != null && method.getBody().getStatements().isEmpty()) { | ||
J.ClassDeclaration enclosing = getCursor().firstEnclosing(J.ClassDeclaration.class); | ||
AccessLevel accessLevel = LombokUtils.getAccessLevel(method); | ||
doAfterVisit(new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
if (TypeUtils.isOfType(classDecl.getType(), enclosing.getType())) { | ||
String template = "@NoArgsConstructor" + (accessLevel == AccessLevel.PUBLIC ? | ||
"" : "(access = AccessLevel." + accessLevel.name() + ")"); | ||
maybeAddImport("lombok.AccessLevel"); | ||
maybeAddImport("lombok.NoArgsConstructor"); | ||
return JavaTemplate.builder(template) | ||
.imports("lombok.*") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("lombok")) | ||
.build() | ||
.apply(getCursor(), classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
} | ||
return super.visitClassDeclaration(classDecl, ctx); | ||
} | ||
}); | ||
return null; | ||
} | ||
return super.visitMethodDeclaration(method, ctx); | ||
} | ||
}; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/org/openrewrite/java/migrate/lombok/log/LogVisitor.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,106 @@ | ||
/* | ||
* Copyright 2025 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.lombok.log; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.java.*; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
import static java.util.Comparator.comparing; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
class LogVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
private final String logType; | ||
private final String factoryType; | ||
private final MethodMatcher factoryMethodMatcher; | ||
private final String logAnnotation; | ||
@Nullable | ||
private final String fieldName; | ||
|
||
LogVisitor(String logType, String factoryMethodPattern, String logAnnotation, @Nullable String fieldName) { | ||
this.logType = logType; | ||
this.factoryType = factoryMethodPattern.substring(0, factoryMethodPattern.indexOf(' ')); | ||
this.factoryMethodMatcher = new MethodMatcher(factoryMethodPattern); | ||
this.logAnnotation = logAnnotation; | ||
this.fieldName = fieldName; | ||
} | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
J.ClassDeclaration visitClassDeclaration = super.visitClassDeclaration(classDecl, ctx); | ||
if (visitClassDeclaration != classDecl) { | ||
maybeRemoveImport(logType); | ||
maybeRemoveImport(factoryType); | ||
maybeAddImport(logAnnotation); | ||
return JavaTemplate | ||
.builder("@" + logAnnotation.substring(logAnnotation.lastIndexOf('.') + 1) + "\n") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("lombok")) | ||
.imports(logAnnotation) | ||
.build() | ||
.apply( | ||
updateCursor(visitClassDeclaration), | ||
visitClassDeclaration.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
} | ||
return classDecl; | ||
} | ||
|
||
@Override | ||
public J.@Nullable VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { | ||
if (!multiVariable.hasModifier(J.Modifier.Type.Private) || | ||
!multiVariable.hasModifier(J.Modifier.Type.Static) || | ||
!multiVariable.hasModifier(J.Modifier.Type.Final) || | ||
multiVariable.getVariables().size() != 1 || | ||
!TypeUtils.isAssignableTo(logType, multiVariable.getType())) { | ||
return multiVariable; | ||
} | ||
|
||
// name needs to match the name of the field that lombok creates | ||
J.VariableDeclarations.NamedVariable var = multiVariable.getVariables().get(0); | ||
if (fieldName != null && !fieldName.equals(var.getSimpleName())) { | ||
return multiVariable; | ||
} | ||
|
||
if (!factoryMethodMatcher.matches(var.getInitializer())) { | ||
return multiVariable; | ||
} | ||
|
||
J.ClassDeclaration classDeclaration = getCursor().firstEnclosing(J.ClassDeclaration.class); | ||
if (classDeclaration == null || classDeclaration.getType() == null) { | ||
return multiVariable; | ||
} | ||
|
||
J.MethodInvocation methodCall = (J.MethodInvocation) var.getInitializer(); | ||
if (methodCall.getArguments().size() != 1 || | ||
!getFactoryParameter(classDeclaration.getSimpleName()) | ||
.equals(methodCall.getArguments().get(0).toString())) { | ||
return multiVariable; | ||
} | ||
|
||
if (!"log".equals(var.getSimpleName())) { | ||
doAfterVisit(new ChangeFieldName<>(classDeclaration.getType().getFullyQualifiedName(), var.getSimpleName(), "log")); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected String getFactoryParameter(String className) { | ||
return className + ".class"; | ||
} | ||
} |
Oops, something went wrong.