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

Annotation Matcher with inner Enums as Attribute Values #4867

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -158,6 +158,7 @@ private boolean argumentValueMatches(String matchOnArgumentName, Expression arg,
return false;
}

//todo the problem is, that varType is null for inner Enums even fa.getType() is null
JavaType.Variable varType = fa.getName().getFieldType();
if (varType != null) {
JavaType.FullyQualified owner = TypeUtils.asFullyQualified(varType.getOwner());
Expand Down
324 changes: 287 additions & 37 deletions rewrite-java/src/test/java/org/openrewrite/java/trait/AnnotatedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.trait;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RewriteTest;
Expand All @@ -24,42 +25,291 @@

class AnnotatedTest implements RewriteTest {

@Test
void attributes() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example").asVisitor(a -> SearchResult.found(a.getTree(),
a.getDefaultAttribute("name")
.map(lit -> lit.getValue(String.class))
.orElse("unknown"))
)
)),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
String name() default "";
}
"""
),
java(
"""
@Example("test")
@Example(value = "test")
@Example(name = "test")
class Test {
}
""",
"""
/*~~(test)~~>*/@Example("test")
/*~~(test)~~>*/@Example(value = "test")
/*~~(test)~~>*/@Example(name = "test")
class Test {
}
"""
)
);
@Nested
class PrimitiveValues {
@Test
void attributes() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example").asVisitor(a -> SearchResult.found(a.getTree(),
a.getDefaultAttribute("name")
.map(lit -> lit.getValue(String.class))
.orElse("unknown"))
)
)),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
String name() default "";
}
"""
),
java(
"""
@Example("test")
@Example(value = "test")
@Example(name = "test")
class Test {
}
""",
"""
/*~~(test)~~>*/@Example("test")
/*~~(test)~~>*/@Example(value = "test")
/*~~(test)~~>*/@Example(name = "test")
class Test {
}
"""
)
);
}

@Test
void match() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=\"test\")").asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
String name() default "";
}
"""
),
java(
"""
@Example(name = "test")
@Example(name = "other")
class Test {
}
""",
"""
/*~~>*/@Example(name = "test")
@Example(name = "other")
class Test {
}
"""
)
);
}

@Test
void withField() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=Test.TEST)").asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
String name() default "";
}
"""
),
java(
"""
@Example(name = Test.TEST)
@Example(name = "other")
class Test {
static final String TEST = "test";
}
""",
"""
/*~~>*/@Example(name = Test.TEST)
@Example(name = "other")
class Test {
static final String TEST = "test";
}
"""
)
);
}
}

@Nested
class ClassValues {
@Test
void normalClass() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=FindMe.class)").asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
Class<?> name() default "";
}
class FindMe {}
class Other {}
"""
),
java(
"""
@Example(name = FindMe.class)
@Example(name = Other.class)
class Test {
}
""",
"""
/*~~>*/@Example(name = FindMe.class)
@Example(name = Other.class)
class Test {
}
"""
)
);
}

@Test
void innerCass() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=Example.FindMe.class)").asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String value() default "";
Class<?> name() default "";
class FindMe {}
class Other{}
}
"""
),
java(
"""
@Example(name = Example.FindMe.class)
@Example(name = Example.Other.class)
class Test {
}
""",
"""
/*~~>*/@Example(name = Example.FindMe.class)
@Example(name = Example.Other.class)
class Test {
}
"""
)
);
}
}

@Nested
class EnumValues {
@Test
void sameFile() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=Values.FindMe)")
.asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String second() default "";
Values name() default "";
}
enum Values {FindMe,OTHER}
"""
),
java(
"""
@Example(name = Values.FindMe, second="test")
@Example(name = Values.OTHER)
class Test {
}
""",
"""
/*~~>*/@Example(name = Values.FindMe, second="test")
@Example(name = Values.OTHER)
class Test {
}
"""
)
);
}

@Test
void otherFile() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=Values.FindMe)")
.asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String second() default "";
Values name() default "";
}
"""
),
java(
"""
enum Values {FindMe,OTHER}
"""
),
java(
"""
@Example(name = Values.FindMe, second="test")
@Example(name = Values.OTHER)
class Test {
}
""",
"""
/*~~>*/@Example(name = Values.FindMe, second="test")
@Example(name = Values.OTHER)
class Test {
}
"""
)
);
}

@Test
void innerEnum() {
rewriteRun(
spec -> spec.recipe(RewriteTest.toRecipe(() ->
annotated("@Example(name=Example.Values.FindMe)")
.asVisitor(a -> SearchResult.found(a.getTree())))),
java(
"""
import java.lang.annotation.Repeatable;
@Repeatable
@interface Example {
String second() default "";
Values name() default "";
enum Values {FindMe,OTHER}
}
"""
),
java(
"""
@Example(name = Example.Values.FindMe, second="test")
@Example(name = Example.Values.OTHER)
class Test {
}
""",
"""
/*~~>*/@Example(name = Example.Values.FindMe, second="test")
@Example(name = Example.Values.OTHER)
class Test {
}
"""
)
);
}
}
}
Loading