Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ public J visitAnnotation(AnnotationTree node, Space fmt) {
List<JRightPadded<Expression>> expressions;
if (node.getArguments().size() == 1) {
ExpressionTree arg = node.getArguments().get(0);
if (arg instanceof JCAssign) {
if (endPos(arg) < 0) {
expressions = singletonList(convert(((JCAssign) arg).rhs, t -> sourceBefore(")")));
if (arg instanceof JCAssign assign && assign.lhs instanceof JCIdent) {
// javac's `Annotate#enterAnnotation` builds an elided `value =` with `make.at(rhs.pos)`
if (assign.lhs.pos == assign.rhs.pos) {
expressions = singletonList(convert(assign.rhs, t -> sourceBefore(")")));
} else {
expressions = singletonList(convert(arg, t -> sourceBefore(")")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ public J visitAnnotation(AnnotationTree node, Space fmt) {
List<JRightPadded<Expression>> expressions;
if (node.getArguments().size() == 1) {
ExpressionTree arg = node.getArguments().get(0);
if (arg instanceof JCAssign) {
if (endPos(arg) < 0) {
expressions = singletonList(convert(((JCAssign) arg).rhs, t -> sourceBefore(")")));
if (arg instanceof JCAssign assign && assign.lhs instanceof JCIdent) {
// javac's `Annotate#enterAnnotation` builds an elided `value =` with `make.at(rhs.pos)`
if (assign.lhs.pos == assign.rhs.pos) {
expressions = singletonList(convert(assign.rhs, t -> sourceBefore(")")));
} else {
expressions = singletonList(convert(arg, t -> sourceBefore(")")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ public J visitAnnotation(AnnotationTree node, Space fmt) {
List<JRightPadded<Expression>> expressions;
if (node.getArguments().size() == 1) {
ExpressionTree arg = node.getArguments().get(0);
if (arg instanceof JCAssign) {
if (endPos(arg) < 0) {
expressions = singletonList(convert(((JCAssign) arg).rhs, t -> sourceBefore(")")));
if (arg instanceof JCAssign assign && assign.lhs instanceof JCIdent) {
// javac's `Annotate#enterAnnotation` builds an elided `value =` with `make.at(rhs.pos)`
if (assign.lhs.pos == assign.rhs.pos) {
expressions = singletonList(convert(assign.rhs, t -> sourceBefore(")")));
} else {
expressions = singletonList(convert(arg, t -> sourceBefore(")")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public class A {}
);
}

@Test
void annotationWithDefaultArgumentNamedLikeTheAttribute() {
rewriteRun(
java(
"""
class A {
static final String value = "ALL";

@SuppressWarnings(value)
void m() {
}
}
"""
)
);
}

@Test
void annotationWithArgument() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,60 @@ public record JavaRecord(String name, @Deprecated int age) {
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/8723")
@Test
void namedAttributeAnnotationOnComponent() {
rewriteRun(
java(
"""
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.PARAMETER, ElementType.RECORD_COMPONENT})
@interface A {
String value();
}
"""
),
java(
"""
public record MyRecord(@A(value = "a") String name) {
}
"""
),
java(
"""
public record MyRecordWithBlanks( @A( /* don't trip */ value = "a" ) String name) {
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/8723")
@Test
void namedAttributeAnnotationOnComponentFollowedByAdditionalSource() {
rewriteRun(
java(
"""
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.PARAMETER, ElementType.RECORD_COMPONENT})
@interface A {
String value();
}
"""
),
java(
"""
public record MyRecord(@A(value = "a") String name, int age) {
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/6401")
@Test
void annotationsAndRecords() {
Expand Down
Loading