Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply best practices #4

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public String getDisplayName() {
@Override
public String getDescription() {
//language=markdown
return "Prefer the lombok annotation `@NoArgsConstructor` over explicitly written out constructors.\n"
+ "This recipe does not create annotations for implicit constructors.";
return "Prefer the lombok annotation `@NoArgsConstructor` over explicitly written out constructors.\n" +
"This recipe does not create annotations for implicit constructors.";
}

@Override
Expand All @@ -66,11 +66,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex

AccessLevel accessLevel = LombokUtils.getAccessLevel(message.getModifiers());

J.ClassDeclaration annotatedClass = getAnnotation(accessLevel).apply(
return getAnnotation(accessLevel).apply(
updateCursor(classDeclAfterVisit),
classDeclAfterVisit.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));

return annotatedClass;
}

private JavaTemplate getAnnotation(AccessLevel accessLevel) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/timoa/lombok/ConvertSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

//we only want to annotate fields and not e.g. method parameters, so we require a lass declaration to be close in the cursor.
if (getCursor().getPathAsStream().limit(4).noneMatch(e -> e instanceof J.ClassDeclaration))
if (getCursor().getPathAsStream().limit( 4 ).noneMatch( e -> e instanceof J.ClassDeclaration )) {
return multiVariable;
}

J.VariableDeclarations.NamedVariable variable = multiVariable.getVariables().get(0);
Optional<Finding> field = fieldsToDecorate.stream()
Expand All @@ -165,7 +166,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
maybeAddImport("lombok.Setter");
maybeAddImport("lombok.AccessLevel");
System.out.println("annotating " + variable);
return annotated;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/io/github/timoa/lombok/LombokUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LombokUtils {
public static boolean isEffectivelyGetter(J.MethodDeclaration method) {
boolean takesNoParameters = method.getParameters().get(0) instanceof J.Empty;
boolean singularReturn = method.getBody() != null //abstract methods can be null
&& method.getBody().getStatements().size() == 1
&& method.getBody().getStatements().size() == 1 //
&& method.getBody().getStatements().get(0) instanceof J.Return;

if (takesNoParameters && singularReturn) {
Expand All @@ -30,8 +30,7 @@ public static boolean isEffectivelyGetter(J.MethodDeclaration method) {
if (returnExpression instanceof J.Identifier) {
J.Identifier identifier = (J.Identifier) returnExpression;
JavaType.Variable fieldType = identifier.getFieldType();
boolean typeMatch = method.getType().equals(fieldType.getType());
return typeMatch;
return method.getType().equals(fieldType.getType()); //type match
}
}
return false;
Expand All @@ -51,7 +50,7 @@ public static boolean isEffectivelySetter(J.MethodDeclaration method) {
String paramName = param.getName().toString();

boolean singularStatement = method.getBody() != null //abstract methods can be null
&& method.getBody().getStatements().size() == 1
&& method.getBody().getStatements().size() == 1 //
&& method.getBody().getStatements().get(0) instanceof J.Assignment;

if (!singularStatement) {
Expand All @@ -75,8 +74,7 @@ public static String deriveGetterMethodName(JavaType.Variable fieldType) {

final String fieldName = fieldType.getName();

boolean alreadyStartsWithIs = fieldName.length() >= 3
&& fieldName.substring(0, 3).matches("is[A-Z]");
boolean alreadyStartsWithIs = fieldName.length() >= 3 && fieldName.substring(0, 3).matches("is[A-Z]");

if (isPrimitiveBoolean)
if (alreadyStartsWithIs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;

import static java.util.Comparator.comparing;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/timoa/lombok/NormalizeGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.*;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -155,12 +155,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor(MethodAcc acc) {
return new TreeVisitor<Tree, ExecutionContext>() {

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

for (RenameRecord rr : acc.renameRecords) {
String methodPattern = String.format("%s %s()", rr.pathToClass_, rr.methodName_);
tree = new ChangeMethodName(methodPattern, rr.newMethodName_, true, null)
.getVisitor().visit(tree, executionContext);
.getVisitor().visit(tree, ctx);
}
return tree;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/timoa/lombok/NormalizeSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.ChangeMethodName;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -166,12 +166,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor(MethodAcc acc) {
return new TreeVisitor<Tree, ExecutionContext>() {

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

for (RenameRecord rr : acc.renameRecords) {
String methodPattern = String.format("%s %s(%s)", rr.pathToClass_, rr.methodName_, rr.parameterType_);
tree = new ChangeMethodName(methodPattern, rr.newMethodName_, true, null)
.getVisitor().visit(tree, executionContext);
.getVisitor().visit(tree, ctx);
}
return tree;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/timoa/lombok/SummarizeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.NlsRewrite;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
Expand All @@ -36,12 +35,12 @@
@EqualsAndHashCode(callSuper = false)
public class SummarizeData extends Recipe {
@Override
public @NlsRewrite.DisplayName String getDisplayName() {
public String getDisplayName() {
return "Summarize class annotations into @Data";
}

@Override
public @NlsRewrite.Description String getDescription() {
public String getDescription() {
return "Summarize class annotations into @Data.";
}

Expand All @@ -63,6 +62,7 @@ private static class Summarizer extends JavaIsoVisitor<ExecutionContext> {
"RequiredArgsConstructor")
.collect(Collectors.toSet());

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {

J.ClassDeclaration visited = super.visitClassDeclaration(classDecl, ctx);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/github/timoa/lombok/SummarizeGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.TreeVisitingPrinter;
import org.openrewrite.java.tree.J;

import static java.util.Comparator.comparing;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/github/timoa/lombok/SummarizeSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.TreeVisitingPrinter;
import org.openrewrite.java.tree.J;

import static java.util.Comparator.comparing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTemplate;

@Value
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/timoa/lombok/log/ConvertJBoss.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTemplate;

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTemplate;

@Value
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/timoa/lombok/log/ConvertSlf4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTemplate;

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTemplate;

@Value
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/github/timoa/lombok/log/LogVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
}

switchImports();
J.ClassDeclaration annotatedClass = getLombokTemplate().apply(
return getLombokTemplate().apply(
updateCursor(visitClassDeclaration),
visitClassDeclaration.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
return annotatedClass;
}

protected abstract JavaTemplate getLombokTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.NlsRewrite.Description;
import org.openrewrite.NlsRewrite.DisplayName;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand All @@ -14,16 +14,16 @@
import java.util.StringJoiner;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class NormalizeBigDecimalCreation extends Recipe {

@Override
public @DisplayName String getDisplayName() {
public String getDisplayName() {
return "Use Strings for BigDecimal instantiation";
}

@Override
public @Description String getDescription() {
public String getDescription() {
return new StringJoiner("\n")
.add("Converts BigDecimal instantiation with a double literal to instantiation with a String. ")
.add("CAUTION: This Recipe can change semantics. ")
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/github/timoa/misc/UseRanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.time.LocalDate;
import org.openrewrite.java.template.RecipeDescriptor;

import java.math.BigDecimal;
import java.time.LocalDate;

@RecipeDescriptor(
name = "Use Guava Ranges",
Expand Down Expand Up @@ -71,6 +71,7 @@ boolean after(BigDecimal from, BigDecimal candidate, BigDecimal to) {
return Range.closed(from, to).contains(candidate);
}
}

@RecipeDescriptor(
name = "Replace `from.compareTo(candidate) < 0 && candidate.compareTo(to) < 0` with a guava `Range.open(from, to).contains(candidate)`",
description = "Replace a hand crafted range check for membership in an open interval ( candidate € (from, to) ) with a guava range expression`."
Expand Down Expand Up @@ -152,6 +153,7 @@ boolean after(BigDecimal from, BigDecimal candidate, BigDecimal to) {
return Range.closedOpen(from, to).contains(candidate);
}
}

@RecipeDescriptor(
name = "Replace `from.compareTo(candidate) < 0 && candidate.compareTo(to) <= 0` with a guava `Range.openClosed(from, to).contains(candidate)`",
description = "Replace a hand crafted range check for membership in an interval that is open to the left ( candidate € (from, to] ) with a guava range expression`."
Expand Down Expand Up @@ -233,6 +235,7 @@ boolean after(LocalDate from, LocalDate candidate, LocalDate to) {
return Range.closed(from, to).contains(candidate);
}
}

@RecipeDescriptor(
name = "Replace `from.compareTo(candidate) < 0 && candidate.compareTo(to) < 0` with a guava `Range.open(from, to).contains(candidate)`",
description = "Replace a hand crafted range check for membership in an open interval ( candidate € (from, to) ) with a guava range expression`."
Expand Down Expand Up @@ -314,6 +317,7 @@ boolean after(LocalDate from, LocalDate candidate, LocalDate to) {
return Range.closedOpen(from, to).contains(candidate);
}
}

@RecipeDescriptor(
name = "Replace `from.compareTo(candidate) < 0 && candidate.compareTo(to) <= 0` with a guava `Range.openClosed(from, to).contains(candidate)`",
description = "Replace a hand crafted range check for membership in an interval that is open to the left ( candidate € (from, to] ) with a guava range expression`."
Expand Down Expand Up @@ -357,4 +361,4 @@ boolean after(LocalDate from, LocalDate candidate, LocalDate to) {



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.openrewrite.java.Assertions.java;

public class ConvertAnyLogTest implements RewriteTest {
class ConvertAnyLogTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.openrewrite.java.Assertions.java;

public class NormalizeBigDecimalCreationTest implements RewriteTest {
class NormalizeBigDecimalCreationTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/github/timoa/misc/UseRangesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ boolean booleanExpression() {
)
);
}
}
}