Skip to content

Commit

Permalink
refactor: Operator wrapping on end of line (#593)
Browse files Browse the repository at this point in the history
* refactor: Operator wrapping on end of line

Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.OperatorWrap?organizationId=T3BlblJld3JpdGU%3D#defaults=W3sidmFsdWUiOiJFT0wiLCJuYW1lIjoid3JhcE9wdGlvbiJ9XQ==

Co-authored-by: Moderne <[email protected]>

* Rename variables according to best practices

---------

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
timtebeek and TeamModerne authored Sep 21, 2024
1 parent e4dde38 commit 0af9942
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public J visitVariableDeclarations(VariableDeclarations multiVariable, Execution
return multiVariable;
}
VariableDeclarations mv = multiVariable;
if (blockCursor.getParent() != null && blockCursor.getParent().getValue() instanceof ClassDeclaration
&& multiVariable.getVariables().size() == 1
&& fieldName.equals(multiVariable.getVariables().get(0).getName().getSimpleName())) {
if (blockCursor.getParent() != null && blockCursor.getParent().getValue() instanceof ClassDeclaration &&
multiVariable.getVariables().size() == 1 &&
fieldName.equals(multiVariable.getVariables().get(0).getName().getSimpleName())) {

mv = (VariableDeclarations) new RemoveAnnotationVisitor(new AnnotationMatcher("@" + AUTOWIRED)).visitNonNull(multiVariable, p);
if (mv != multiVariable && multiVariable.getTypeExpression() != null) {
Expand Down Expand Up @@ -161,10 +161,10 @@ public J visitBlock(Block block, ExecutionContext p) {
ClassDeclaration classDecl = (ClassDeclaration) n;
JavaType.FullyQualified typeFqn = TypeUtils.asFullyQualified(type.getType());
if (typeFqn != null && classDecl.getKind() == ClassDeclaration.Kind.Type.Class && className.equals(classDecl.getSimpleName())) {
JavaTemplate.Builder template = JavaTemplate.builder(""
+ classDecl.getSimpleName() + "(" + typeFqn.getClassName() + " " + fieldName + ") {\n"
+ "this." + fieldName + " = " + fieldName + ";\n"
+ "}\n"
JavaTemplate.Builder template = JavaTemplate.builder("" +
classDecl.getSimpleName() + "(" + typeFqn.getClassName() + " " + fieldName + ") {\n" +
"this." + fieldName + " = " + fieldName + ";\n" +
"}\n"
).contextSensitive();
FullyQualified fq = TypeUtils.asFullyQualified(type.getType());
if (fq != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private static class BeanMethodsNotPublicVisitor extends JavaIsoVisitor<Executio
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

if (m.getAllAnnotations().stream().anyMatch(BEAN_ANNOTATION_MATCHER::matches)
&& Boolean.FALSE.equals(TypeUtils.isOverride(method.getMethodType()))) {
if (m.getAllAnnotations().stream().anyMatch(BEAN_ANNOTATION_MATCHER::matches) &&
Boolean.FALSE.equals(TypeUtils.isOverride(method.getMethodType()))) {
// remove public modifier and copy any associated comments to the method
doAfterVisit(new ChangeMethodAccessLevelVisitor<>(new MethodMatcher(method), null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private String exceptRegex() {
return except == null || except.isEmpty()
? ""
: "(?!(" + String.join("|", except) + "))";
return except == null || except.isEmpty() ?
"" :
"(?!(" + String.join("|", except) + "))";
}
}
14 changes: 7 additions & 7 deletions src/main/java/org/openrewrite/java/spring/ExpandProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,29 @@ private Yaml.Documents removeEmptyFirstLine(Yaml.Documents docs, ExecutionContex
return (Yaml.Documents) new YamlIsoVisitor<ExecutionContext>() {
boolean doneTrimming;
@Override
public Yaml.Scalar visitScalar(Yaml.Scalar scalar, ExecutionContext executionContext) {
public Yaml.Scalar visitScalar(Yaml.Scalar scalar, ExecutionContext ctx) {
doneTrimming = true;
return scalar.withPrefix(trimNewlineBeforeComment(scalar.getPrefix()));
}

@Override
public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext executionContext) {
public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
doneTrimming = true;
return entry.withPrefix(trimNewlineBeforeComment(entry.getPrefix()));
}

@Override
public Yaml.Sequence.Entry visitSequenceEntry(Yaml.Sequence.Entry entry, ExecutionContext executionContext) {
public Yaml.Sequence.Entry visitSequenceEntry(Yaml.Sequence.Entry entry, ExecutionContext ctx) {
doneTrimming = true;
return entry.withPrefix(trimNewlineBeforeComment(entry.getPrefix()));
}

@Override
public @Nullable Yaml visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable Yaml visit(@Nullable Tree tree, ExecutionContext ctx) {
if (doneTrimming) {
return (Yaml) tree;
}
return super.visit(tree, executionContext);
return super.visit(tree, ctx);
}

private String trimNewlineBeforeComment(String prefix) {
Expand All @@ -114,8 +114,8 @@ private String trimNewlineBeforeComment(String prefix) {
}
};
return sourceFileMask != null ?
Preconditions.check(new FindSourceFiles(sourceFileMask), visitor)
: visitor;
Preconditions.check(new FindSourceFiles(sourceFileMask), visitor) :
visitor;
}

private static class ExpandEntriesVisitor extends YamlVisitor<ExecutionContext> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);
if (c.getKind() == J.ClassDeclaration.Kind.Type.Interface) {
boolean hasRepoAnnotation = c.getLeadingAnnotations().stream().anyMatch(annotation -> {
if (annotation.getArguments() == null || annotation.getArguments().isEmpty()
|| annotation.getArguments().get(0) instanceof J.Empty) {
if (annotation.getArguments() == null || annotation.getArguments().isEmpty() ||
annotation.getArguments().get(0) instanceof J.Empty) {
JavaType.FullyQualified type = TypeUtils.asFullyQualified(annotation.getType());
return type != null && ANNOTATION_REPOSITORY.equals(type.getFullyQualifiedName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ private Optional<J.Assignment> requestMethodArgument(J.Annotation annotation) {
return Optional.empty();
}
return annotation.getArguments().stream()
.filter(arg -> arg instanceof J.Assignment
&& ((J.Assignment) arg).getVariable() instanceof J.Identifier
&& "method".equals(((J.Identifier) ((J.Assignment) arg).getVariable()).getSimpleName()))
.filter(arg -> arg instanceof J.Assignment &&
((J.Assignment) arg).getVariable() instanceof J.Identifier &&
"method".equals(((J.Identifier) ((J.Assignment) arg).getVariable()).getSimpleName()))
.map(J.Assignment.class::cast)
.findFirst();
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/openrewrite/java/spring/RenameBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public String getDescription() {
* @return a recipe for this methodDeclaration if it declares a bean, or null if it does not declare a bean
*/
public static @Nullable RenameBean fromDeclaration(J.MethodDeclaration methodDeclaration, String newName) {
return methodDeclaration.getMethodType() == null ? null
: fromDeclaration(methodDeclaration, newName, methodDeclaration.getMethodType().getReturnType().toString());
return methodDeclaration.getMethodType() == null ? null :
fromDeclaration(methodDeclaration, newName, methodDeclaration.getMethodType().getReturnType().toString());
}

/**
Expand All @@ -106,8 +106,8 @@ public String getDescription() {
* @return a recipe for this classDeclaration if it declares a bean, or null if it does not declare a bean
*/
public static @Nullable RenameBean fromDeclaration(J.ClassDeclaration classDeclaration, String newName) {
return classDeclaration.getType() == null ? null
: fromDeclaration(classDeclaration, newName, classDeclaration.getType().toString());
return classDeclaration.getType() == null ? null :
fromDeclaration(classDeclaration, newName, classDeclaration.getType().toString());
}

/**
Expand Down Expand Up @@ -182,12 +182,12 @@ public BeanSearchResult(boolean isBean, @Nullable String beanName) {
}

private TreeVisitor<?, ExecutionContext> precondition() {
return type == null
? Preconditions.or(
return type == null ?
Preconditions.or(
new FindAnnotations("@" + FQN_QUALIFIER, false).getVisitor(),
new FindAnnotations("@" + FQN_BEAN, false).getVisitor(),
new FindAnnotations("@" + FQN_COMPONENT, true).getVisitor())
: Preconditions.or(new UsesType<>(type, false), new DeclaresType<>(type));
new FindAnnotations("@" + FQN_COMPONENT, true).getVisitor()) :
Preconditions.or(new UsesType<>(type, false), new DeclaresType<>(type));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
}

J.VariableDeclarations parameter = (J.VariableDeclarations) m.getParameters().get(0);
if (!(parameter.getTypeExpression() instanceof J.ParameterizedType)
|| ((J.ParameterizedType) parameter.getTypeExpression()).getTypeParameters() == null) {
if (!(parameter.getTypeExpression() instanceof J.ParameterizedType) ||
((J.ParameterizedType) parameter.getTypeExpression()).getTypeParameters() == null) {
return m;
}
String chunkTypeParameter = ((J.ParameterizedType) parameter.getTypeExpression()).getTypeParameters().get(0).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
}

private boolean isJobRepositoryParameter(Statement statement) {
return statement instanceof J.VariableDeclarations
&& TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
return statement instanceof J.VariableDeclarations &&
TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
"org.springframework.batch.core.repository.JobRepository");
}

private boolean isJobBuilderFactoryParameter(Statement statement) {
return statement instanceof J.VariableDeclarations
&& TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
return statement instanceof J.VariableDeclarations &&
TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
"org.springframework.batch.core.configuration.annotation.JobBuilderFactory");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
// Remove StepBuilderFactory field if StepBuilderFactory.get(..) is used further down
if (!FindMethods.find(classDeclaration, STEP_BUILDER_FACTORY_GET).isEmpty()) {
cd = cd.withBody(cd.getBody().withStatements(ListUtils.map(cd.getBody().getStatements(), statement -> {
if (statement instanceof J.VariableDeclarations
&& ((J.VariableDeclarations) statement).getTypeExpression() != null) {
if (statement instanceof J.VariableDeclarations &&
((J.VariableDeclarations) statement).getTypeExpression() != null) {
if (TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getTypeExpression().getType(),
"org.springframework.batch.core.configuration.annotation.StepBuilderFactory")) {
return null;
Expand Down Expand Up @@ -118,14 +118,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut
}

private boolean isJobRepositoryParameter(Statement statement) {
return statement instanceof J.VariableDeclarations
&& TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
return statement instanceof J.VariableDeclarations &&
TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
"org.springframework.batch.core.repository.JobRepository");
}

private boolean isJobBuilderFactoryParameter(Statement statement) {
return statement instanceof J.VariableDeclarations
&& TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
return statement instanceof J.VariableDeclarations &&
TypeUtils.isOfClassType(((J.VariableDeclarations) statement).getType(),
"org.springframework.batch.core.configuration.annotation.StepBuilderFactory");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex

// Strip calls to new DefaultBatchConfigurer()
List<Statement> statements = md.getBody().getStatements();
if (statements.size() == 1
&& statements.get(0) instanceof J.Return
&& new MethodMatcher(DEFAULT_BATCH_CONFIGURER + " <constructor>(..)")
if (statements.size() == 1 &&
statements.get(0) instanceof J.Return &&
new MethodMatcher(DEFAULT_BATCH_CONFIGURER + " <constructor>(..)")
.matches(((J.Return) statements.get(0)).getExpression())) {
maybeRemoveImport(BATCH_CONFIGURER);
maybeRemoveImport(DEFAULT_BATCH_CONFIGURER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl,
ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getExtends() != null
&& TypeUtils.isOfClassType(cd.getExtends().getType(), fullyQualifiedClassName)) {
if (cd.getExtends() != null &&
TypeUtils.isOfClassType(cd.getExtends().getType(), fullyQualifiedClassName)) {
cd = cd.withExtends(null);
updateCursor(cd);
// This is an interesting one... JobExecutionListenerSupport implements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private J.ClassDeclaration addConfigurationAnnotation(J.ClassDeclaration c) {
maybeAddImport(FQN_CONFIGURATION);
return JavaTemplate.builder("@" + CONFIGURATION_SIMPLE_NAME)
.imports(FQN_CONFIGURATION)
.javaParser(JavaParser.fromJavaVersion().dependsOn("package " + CONFIGURATION_PACKAGE
+ "; public @interface " + CONFIGURATION_SIMPLE_NAME + " {}"))
.javaParser(JavaParser.fromJavaVersion().dependsOn("package " + CONFIGURATION_PACKAGE +
"; public @interface " + CONFIGURATION_SIMPLE_NAME + " {}"))
.build().apply(
getCursor(),
c.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
// If class arguments are not found then search for an array of type arguments
if (conditionalOnBeanCandidates.isEmpty()) {
for (Expression arg : a.getArguments()) {
if (arg instanceof J.Assignment
&& ((J.Assignment) arg).getAssignment() instanceof J.NewArray
&& "type".equals(((J.Identifier) ((J.Assignment) arg).getVariable()).getSimpleName())) {
if (arg instanceof J.Assignment &&
((J.Assignment) arg).getAssignment() instanceof J.NewArray &&
"type".equals(((J.Identifier) ((J.Assignment) arg).getVariable()).getSimpleName())) {
J.NewArray na = (J.NewArray) ((J.Assignment) arg).getAssignment();
if (na.getInitializer() != null) {
for (Expression l : na.getInitializer()) {
Expand Down
Loading

0 comments on commit 0af9942

Please sign in to comment.