Skip to content

Commit

Permalink
Fix omitting addOnly and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
SiBorea committed Jan 15, 2025
1 parent 1a5ac95 commit ae8acf4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package org.openrewrite.java;

import org.junit.jupiter.api.Test;
import static org.openrewrite.java.Assertions.java;
import org.openrewrite.test.RewriteTest;

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

class AddOrUpdateAnnotationAttributeTest implements RewriteTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
import lombok.With;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import static org.openrewrite.Tree.randomId;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;

import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static org.openrewrite.Tree.randomId;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddOrUpdateAnnotationAttribute extends Recipe {
Expand All @@ -42,37 +46,37 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Some annotations accept arguments. This recipe sets an existing argument to the specified value, " +
"or adds the argument if it is not already set.";
"or adds the argument if it is not already set.";
}

@Option(displayName = "Annotation type",
description = "The fully qualified name of the annotation.",
example = "org.junit.Test")
description = "The fully qualified name of the annotation.",
example = "org.junit.Test")
String annotationType;

@Option(displayName = "Attribute name",
description = "The name of attribute to change. If omitted defaults to 'value'.",
required = false,
example = "timeout")
description = "The name of attribute to change. If omitted defaults to 'value'.",
required = false,
example = "timeout")
@Nullable
String attributeName;

@Option(displayName = "Attribute value",
description = "The value to set the attribute to. Set to `null` to remove the attribute.",
example = "500")
description = "The value to set the attribute to. Set to `null` to remove the attribute.",
example = "500")
@Nullable
String attributeValue;

@Option(displayName = "Add only",
description = "When set to `true` will not change existing annotation attribute values.")
description = "When set to `true` will not change existing annotation attribute values.")
@Nullable
Boolean addOnly;

@Option(displayName = "Append array",
description = "If the attribute is an array, setting this option to `true` will append the value(s). " +
"In conjunction with `addOnly`, it is possible to control duplicates: " +
"`addOnly=true`, always append. " +
"`addOnly=false`, only append if the value is not already present.")
description = "If the attribute is an array, setting this option to `true` will append the value(s). " +
"In conjunction with `addOnly`, it is possible to control duplicates: " +
"`addOnly=true`, always append. " +
"`addOnly=false`, only append if the value is not already present.")
@Nullable
Boolean appendArray;

Expand All @@ -95,22 +99,22 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {

if (attributeName == null || "value".equals(attributeName)) {
return JavaTemplate.builder("#{}")
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), newAttributeValue);
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), newAttributeValue);
} else {
String newAttributeValueResult = newAttributeValue;
if (((JavaType.FullyQualified) Objects.requireNonNull(a.getAnnotationType().getType())).getMethods().stream().anyMatch(method -> method.getReturnType().toString().equals("java.lang.String[]"))) {
String attributeValueCleanedUp = attributeValue.replaceAll("\\s+", "").replaceAll("[\\s+{}\"]", "");
List<String> attributeList = Arrays.asList(attributeValueCleanedUp.contains(",") ? attributeValueCleanedUp.split(",") : new String[]{attributeValueCleanedUp});
newAttributeValueResult = attributeList.stream()
.map(String::valueOf)
.collect(Collectors.joining("\", \"", "{\"", "\"}"));
.map(String::valueOf)
.collect(Collectors.joining("\", \"", "{\"", "\"}"));
}
return JavaTemplate.builder("#{} = #{}")
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), attributeName, newAttributeValueResult);
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), attributeName, newAttributeValueResult);
}
} else {
// First assume the value exists amongst the arguments and attempt to update it
Expand Down Expand Up @@ -149,7 +153,7 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
jLiteralList.add(new J.Literal(randomId(), Space.EMPTY, Markers.EMPTY, newAttributeListValue, newAttributeListValue, null, JavaType.Primitive.String));
}
return changed ? as.withAssignment(((J.NewArray) as.getAssignment()).withInitializer(jLiteralList))
.withMarkers(as.getMarkers().add(new AlreadyAppended(randomId(), newAttributeValue))) : as;
.withMarkers(as.getMarkers().add(new AlreadyAppended(randomId(), newAttributeValue))) : as;
}
int m = 0;
for (int i = 0; i < Objects.requireNonNull(jLiteralList).size(); i++) {
Expand Down Expand Up @@ -190,9 +194,12 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
}
return as.withAssignment(value.withValue(newAttributeValue).withValueSource(newAttributeValue));
} else if (exp instanceof J.FieldAccess) {
if (Boolean.TRUE.equals(addOnly)) {
return it;
}
as = JavaTemplate.builder("#{} = #{}")
.build()
.apply(new Cursor(getCursor(), as), as.getCoordinates().replace(), var.getSimpleName(), newAttributeValue);
.build()
.apply(new Cursor(getCursor(), as), as.getCoordinates().replace(), var.getSimpleName(), newAttributeValue);
return as;
}
}
Expand All @@ -212,9 +219,9 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
// Make the attribute name explicit, before we add the new value below
//noinspection ConstantConditions
return ((J.Annotation) JavaTemplate.builder("value = #{}")
.contextSensitive()
.build()
.apply(getCursor(), finalA.getCoordinates().replaceArguments(), it)
.contextSensitive()
.build()
.apply(getCursor(), finalA.getCoordinates().replaceArguments(), it)
).getArguments().get(0);
}
} else if (it instanceof J.FieldAccess) {
Expand All @@ -230,15 +237,15 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
}
//noinspection ConstantConditions
return ((J.Annotation) JavaTemplate.apply(newAttributeValue, getCursor(), finalA.getCoordinates().replaceArguments()))
.getArguments().get(0);
.getArguments().get(0);
} else {
// Make the attribute name explicit, before we add the new value below
//noinspection ConstantConditions
return ((J.Annotation) JavaTemplate.builder("value = #{any()}")
.contextSensitive()
.build()
.apply(getCursor(), finalA.getCoordinates().replaceArguments(), it))
.getArguments().get(0);
.contextSensitive()
.build()
.apply(getCursor(), finalA.getCoordinates().replaceArguments(), it))
.getArguments().get(0);
}
}
return it;
Expand All @@ -254,10 +261,10 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
String effectiveName = (attributeName == null) ? "value" : attributeName;
//noinspection ConstantConditions
J.Assignment as = (J.Assignment) ((J.Annotation) JavaTemplate.builder("#{} = #{}")
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), effectiveName, newAttributeValue))
.getArguments().get(0);
.contextSensitive()
.build()
.apply(getCursor(), a.getCoordinates().replaceArguments(), effectiveName, newAttributeValue))
.getArguments().get(0);
a = a.withArguments(ListUtils.concat(as, a.getArguments()));
a = maybeAutoFormat(original, a, ctx);
}
Expand Down

0 comments on commit ae8acf4

Please sign in to comment.