Skip to content

Commit

Permalink
Allow FindAnnotations annotationPattern to not start with '@'
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Nov 2, 2023
1 parent a5f1772 commit 8bdf967
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
Expand All @@ -39,7 +41,6 @@ class FindAnnotationsTest implements RewriteTest {
@Test
void matchMetaAnnotation() {
rewriteRun(
// JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()).logCompilationWarningsAndErrors(true).build(),
spec -> spec.recipe(new FindAnnotations("@javax.annotation.Nonnull", true))
.parser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())),
java(
Expand Down Expand Up @@ -94,18 +95,18 @@ public class MyExtension implements Extension {}
);
}

@Test
void matchesSimpleFullyQualifiedAnnotation() {
@ParameterizedTest
@ValueSource(strings = {"@java.lang.Deprecated", "java.lang.Deprecated"})
void matchesSimpleFullyQualifiedAnnotation(String annotationPattern) {
rewriteRun(
spec -> spec.recipe(new FindAnnotations("@java.lang.Deprecated", null)),
spec -> spec.recipe(new FindAnnotations(annotationPattern, null)),
java(
"@Deprecated public class A {}",
"/*~~>*/@Deprecated public class A {}"
)
);
}


@Test
void matchesWildcard() {
rewriteRun(
Expand Down Expand Up @@ -296,7 +297,7 @@ void findAnnotationWithClassTypeArgument() {
java(
"""
package com.foo;
@Example(Foo.class)
public class Foo {}
""",
Expand Down
2 changes: 1 addition & 1 deletion rewrite-java/src/main/antlr/AnnotationSignatureParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ parser grammar AnnotationSignatureParser;
options { tokenVocab=AnnotationSignatureLexer; }

annotation
: AT annotationName ( LPAREN ( elementValuePairs | elementValue )? RPAREN )?
: AT? annotationName ( LPAREN ( elementValuePairs | elementValue )? RPAREN )?
;

annotationName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ literal


atn:
[4, 1, 22, 77, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 26, 8, 0, 1, 0, 3, 0, 29, 8, 0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 5, 2, 36, 8, 2, 10, 2, 12, 2, 39, 9, 2, 1, 3, 1, 3, 1, 3, 5, 3, 44, 8, 3, 10, 3, 12, 3, 47, 9, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 3, 6, 57, 8, 6, 1, 7, 1, 7, 1, 7, 5, 7, 62, 8, 7, 10, 7, 12, 7, 65, 9, 7, 1, 8, 1, 8, 1, 8, 5, 8, 70, 8, 8, 10, 8, 12, 8, 73, 9, 8, 1, 9, 1, 9, 1, 9, 0, 0, 10, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 2, 0, 11, 11, 20, 20, 1, 0, 1, 5, 74, 0, 20, 1, 0, 0, 0, 2, 30, 1, 0, 0, 0, 4, 32, 1, 0, 0, 0, 6, 40, 1, 0, 0, 0, 8, 48, 1, 0, 0, 0, 10, 52, 1, 0, 0, 0, 12, 56, 1, 0, 0, 0, 14, 58, 1, 0, 0, 0, 16, 66, 1, 0, 0, 0, 18, 74, 1, 0, 0, 0, 20, 21, 5, 18, 0, 0, 21, 28, 3, 2, 1, 0, 22, 25, 5, 6, 0, 0, 23, 26, 3, 6, 3, 0, 24, 26, 3, 10, 5, 0, 25, 23, 1, 0, 0, 0, 25, 24, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 29, 5, 7, 0, 0, 28, 22, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 1, 1, 0, 0, 0, 30, 31, 3, 4, 2, 0, 31, 3, 1, 0, 0, 0, 32, 37, 5, 22, 0, 0, 33, 34, 7, 0, 0, 0, 34, 36, 5, 22, 0, 0, 35, 33, 1, 0, 0, 0, 36, 39, 1, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 5, 1, 0, 0, 0, 39, 37, 1, 0, 0, 0, 40, 45, 3, 8, 4, 0, 41, 42, 5, 10, 0, 0, 42, 44, 3, 8, 4, 0, 43, 41, 1, 0, 0, 0, 44, 47, 1, 0, 0, 0, 45, 43, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 7, 1, 0, 0, 0, 47, 45, 1, 0, 0, 0, 48, 49, 5, 22, 0, 0, 49, 50, 5, 12, 0, 0, 50, 51, 3, 10, 5, 0, 51, 9, 1, 0, 0, 0, 52, 53, 3, 12, 6, 0, 53, 11, 1, 0, 0, 0, 54, 57, 3, 18, 9, 0, 55, 57, 3, 14, 7, 0, 56, 54, 1, 0, 0, 0, 56, 55, 1, 0, 0, 0, 57, 13, 1, 0, 0, 0, 58, 63, 3, 16, 8, 0, 59, 60, 5, 8, 0, 0, 60, 62, 5, 9, 0, 0, 61, 59, 1, 0, 0, 0, 62, 65, 1, 0, 0, 0, 63, 61, 1, 0, 0, 0, 63, 64, 1, 0, 0, 0, 64, 15, 1, 0, 0, 0, 65, 63, 1, 0, 0, 0, 66, 71, 5, 22, 0, 0, 67, 68, 7, 0, 0, 0, 68, 70, 5, 22, 0, 0, 69, 67, 1, 0, 0, 0, 70, 73, 1, 0, 0, 0, 71, 69, 1, 0, 0, 0, 71, 72, 1, 0, 0, 0, 72, 17, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 74, 75, 7, 1, 0, 0, 75, 19, 1, 0, 0, 0,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 31, 5, 7, 0, 0, 30, 24, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 1, 1, 0, 0, 0, 32, 33, 3, 4, 2, 0, 33, 3, 1, 0, 0, 0, 34, 39, 5, 22, 0, 0, 35, 36, 7, 0, 0, 0, 36, 38, 5, 22, 0, 0, 37, 35, 1, 0, 0, 0, 38, 41, 1, 0, 0, 0, 39, 37, 1, 0, 0, 0, 39, 40, 1, 0, 0, 0, 40, 5, 1, 0, 0, 0, 41, 39, 1, 0, 0, 0, 42, 47, 3, 8, 4, 0, 43, 44, 5, 10, 0, 0, 44, 46, 3, 8, 4, 0, 45, 43, 1, 0, 0, 0, 46, 49, 1, 0, 0, 0, 47, 45, 1, 0, 0, 0, 47, 48, 1, 0, 0, 0, 48, 7, 1, 0, 0, 0, 49, 47, 1, 0, 0, 0, 50, 51, 5, 22, 0, 0, 51, 52, 5, 12, 0, 0, 52, 53, 3, 10, 5, 0, 53, 9, 1, 0, 0, 0, 54, 55, 3, 12, 6, 0, 55, 11, 1, 0, 0, 0, 56, 59, 3, 18, 9, 0, 57, 59, 3, 14, 7, 0, 58, 56, 1, 0, 0, 0, 58, 57, 1, 0, 0, 0, 59, 13, 1, 0, 0, 0, 60, 65, 3, 16, 8, 0, 61, 62, 5, 8, 0, 0, 62, 64, 5, 9, 0, 0, 63, 61, 1, 0, 0, 0, 64, 67, 1, 0, 0, 0, 65, 63, 1, 0, 0, 0, 65, 66, 1, 0, 0, 0, 66, 15, 1, 0, 0, 0, 67, 65, 1, 0, 0, 0, 68, 73, 5, 22, 0, 0, 69, 70, 7, 0, 0, 0, 70, 72, 5, 22, 0, 0, 71, 69, 1, 0, 0, 0, 72, 75, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 17, 1, 0, 0, 0, 75, 73, 1, 0, 0, 0, 76, 77, 7, 1, 0, 0, 77, 19, 1, 0, 0, 0, 8, 21, 27, 30, 39, 47, 58, 65, 73]
Loading

0 comments on commit 8bdf967

Please sign in to comment.