13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework . sbm . support . openrewrite .java ;
16
+ package org .openrewrite .java ;
17
17
18
+ import org .openrewrite .java .AddOrUpdateAnnotationAttribute ;
19
+ import org .openrewrite .java .tree .JavaType ;
18
20
import org .springframework .sbm .java .OpenRewriteTestSupport ;
19
21
import org .junit .jupiter .api .Test ;
20
- import org .openrewrite .ExecutionContext ;
21
22
import org .openrewrite .InMemoryExecutionContext ;
22
- import org .openrewrite .java .JavaIsoVisitor ;
23
23
import org .openrewrite .java .tree .J ;
24
24
25
+ import java .util .List ;
26
+
25
27
import static org .assertj .core .api .Assertions .assertThat ;
26
28
27
- public class AddOrReplaceAnnotationAttributeTest {
29
+ public class AddOrUpdateAnnotationAttributeTest {
28
30
29
31
30
32
@ Test
@@ -33,9 +35,9 @@ void addBooleanAttributeToAnnotationWithoutAttributes() {
33
35
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
34
36
35
37
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
36
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "forRemoval" , true , Boolean . class );
38
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "forRemoval" , " true" , true );
37
39
38
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
40
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
39
41
40
42
assertThat (refactoredCu ).isEqualTo ("@Deprecated(forRemoval = true) public class Foo {}" );
41
43
}
@@ -46,8 +48,8 @@ void addStringAttributeToAnnotationWithoutAttributes() {
46
48
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
47
49
48
50
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
49
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "since" , "2020" , String . class );
50
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
51
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "since" , "2020" , true );
52
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
51
53
52
54
assertThat (refactoredCu ).isEqualTo ("@Deprecated(since = \" 2020\" ) public class Foo {}" );
53
55
}
@@ -57,8 +59,8 @@ void changeAnnotationAttributeValue() {
57
59
String code = "@Deprecated(forRemoval = false) public class Foo {}" ;
58
60
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
59
61
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
60
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "forRemoval" , true , Boolean . class );
61
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
62
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "forRemoval" , " true" , false );
63
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
62
64
assertThat (refactoredCu ).isEqualTo ("@Deprecated(forRemoval = true) public class Foo {}" );
63
65
}
64
66
@@ -67,8 +69,8 @@ void changeAnnotationAttributeValueOfAnnotationWithAttributes() {
67
69
String code = "@Deprecated(forRemoval = false, since = \" 2020\" ) public class Foo {}" ;
68
70
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
69
71
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
70
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "forRemoval" , true , Boolean . class );
71
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
72
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "forRemoval" , " true" , false );
73
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
72
74
assertThat (refactoredCu ).isEqualTo ("@Deprecated(forRemoval = true, since = \" 2020\" ) public class Foo {}" );
73
75
}
74
76
@@ -77,8 +79,8 @@ void changeAnnotationAttributeValueOfAnnotationWithAttributes2() {
77
79
String code = "@Deprecated(since = \" 2020\" , forRemoval = false) public class Foo {}" ;
78
80
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
79
81
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
80
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "forRemoval" , true , Boolean . class );
81
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
82
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "forRemoval" , " true" , false );
83
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
82
84
assertThat (refactoredCu ).isEqualTo ("@Deprecated(since = \" 2020\" , forRemoval = true) public class Foo {}" );
83
85
}
84
86
@@ -87,9 +89,9 @@ void addAttributeToAnnotationWithAttributes() {
87
89
String code = "@Deprecated(forRemoval = true) public class Foo {}" ;
88
90
J .CompilationUnit compilationUnit = OpenRewriteTestSupport .createCompilationUnit (code );
89
91
J .Annotation annotation = compilationUnit .getClasses ().get (0 ).getLeadingAnnotations ().get (0 );
90
- JavaIsoVisitor < ExecutionContext > javaIsoVisitor = new AddOrReplaceAnnotationAttribute ( annotation , "since" , "2020" , String . class );
91
- String refactoredCu = javaIsoVisitor .visit ( compilationUnit , new InMemoryExecutionContext ()).print ();
92
- assertThat (refactoredCu ).isEqualTo ("@Deprecated(forRemoval = true, since = \" 2020\" ) public class Foo {}" );
92
+ AddOrUpdateAnnotationAttribute javaIsoVisitor = new AddOrUpdateAnnotationAttribute ((( JavaType . Class ) annotation . getType ()). getFullyQualifiedName () , "since" , "2020" , false );
93
+ String refactoredCu = javaIsoVisitor .run ( List . of ( compilationUnit ) , new InMemoryExecutionContext ()).getResults (). get ( 0 ). getAfter (). printAll ();
94
+ assertThat (refactoredCu ).isEqualTo ("@Deprecated(since = \" 2020\" , forRemoval = true ) public class Foo {}" );
93
95
}
94
96
95
97
0 commit comments