Skip to content

Commit a51bd93

Browse files
Fossurgithub-actions[bot]timtebeek
authored
Annotation processing (#2)
* Add class annotations recipes * Move files to annotation package * Add licences * Update src/main/java/org/openrewrite/dropwizard/annotation/AddAnnotationIfAnnotationExistsRecipe.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/dropwizard/annotation/AddAnnotationIfAnnotationExistsRecipeTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/dropwizard/annotation/AddAnnotationIfAnnotationExistsRecipeTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/dropwizard/annotation/AddAnnotationIfAnnotationExistsRecipeTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix tests * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Change package and apply formatter * Use explicit classpath * Limit leading indentation * Use AnnotationService instead of only looking at leading annotations * Fix shouldAddAnnotationToAnyParentClass, use straight annotation text as arguments * Fix imports * Remove recipe from naming * Add Method Annotation processing recipes, Add annotation transformer * Add missing licences * Fix style * Apply formatter to tests * Drop unused classes * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Drop unused AddClassAnnotation.java * Implement `CodahaleTimedToMicrometerTimed` as declarative recipe * Drop unused OneToOneTransformer.java * Drop unused SecurityAnnotationTransformer.java * Remove unnecessary security converters for now * Remove unnecessary security dependencies --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <timtebeek@gmail.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 3e4ff73 commit a51bd93

File tree

4 files changed

+192
-1
lines changed

4 files changed

+192
-1
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ dependencies {
2525

2626
testImplementation("org.openrewrite:rewrite-test")
2727

28+
testRuntimeOnly("io.dropwizard.metrics:metrics-annotation:4.1.+")
29+
testRuntimeOnly("org.springframework.boot:spring-boot-starter-actuator:2.5.+")
2830
testRuntimeOnly("javax.persistence:javax.persistence-api:2.2")
31+
testRuntimeOnly("org.projectlombok:lombok:1.18.+")
2932
}
3033

3134
recipeDependencies {

src/main/java/org/openrewrite/java/dropwizard/annotation/AddClassAnnotationIfAnnotationExists.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public String getDescription() {
5757
return "Adds annotation if class has any of the specified target annotations.";
5858
}
5959

60-
6160
@Override
6261
public TreeVisitor<?, ExecutionContext> getVisitor() {
6362
return new AddClassAnnotationVisitor(annotationToAdd, annotateInnerClasses) {

src/main/resources/META-INF/rewrite/dropwizard-to-spring-boot.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- org.openrewrite.java.dropwizard.MigrateResourcesToSpringJersey
2727
- org.openrewrite.java.dropwizard.MigrateTests
2828
- org.openrewrite.java.dropwizard.CodeCleanup
29+
- org.openrewrite.java.dropwizard.annotation.micrometer.CodahaleTimedToMicrometerTimed
2930
---
3031
# Core Application Setup
3132
type: specs.openrewrite.org/v1beta/recipe
@@ -169,3 +170,22 @@ recipeList:
169170
- org.openrewrite.java.ShortenFullyQualifiedTypeReferences
170171
# TODO - org.openrewrite.java.dropwizard.method.AddMissingAbstractMethods
171172
- org.openrewrite.java.RemoveUnusedImports
173+
---
174+
type: specs.openrewrite.org/v1beta/recipe
175+
name: org.openrewrite.java.dropwizard.annotation.micrometer.CodahaleTimedToMicrometerTimed
176+
displayName: Replace `@Timed` (Dropwizard) with `@Timed` (Micrometer)
177+
description: Replaces Dropwizard's `@Timed` annotation with Micrometer's `@Timed` annotation, preserving name (mapped to value), absolute, and description attributes.
178+
recipeList:
179+
- org.openrewrite.java.ChangeAnnotationAttributeName:
180+
annotationType: com.codahale.metrics.annotation.Timed
181+
oldAttributeName: name
182+
newAttributeName: value
183+
- org.openrewrite.java.RemoveAnnotationAttribute:
184+
annotationType: com.codahale.metrics.annotation.Timed
185+
attributeName: durationUnit
186+
- org.openrewrite.java.RemoveAnnotationAttribute:
187+
annotationType: com.codahale.metrics.annotation.Timed
188+
attributeName: rateUnit
189+
- org.openrewrite.java.ChangeType:
190+
oldFullyQualifiedTypeName: com.codahale.metrics.annotation.Timed
191+
newFullyQualifiedTypeName: io.micrometer.core.annotation.Timed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.dropwizard.annotation.micrometer;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.java.JavaParser;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
26+
class CodahaleTimedToMicrometerTimedTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.recipeFromResources("org.openrewrite.java.dropwizard.annotation.micrometer.CodahaleTimedToMicrometerTimed")
31+
.parser(
32+
JavaParser.fromJavaVersion()
33+
.logCompilationWarningsAndErrors(true)
34+
.classpath("metrics-annotation", "micrometer-core"));
35+
}
36+
37+
@DocumentExample
38+
@Test
39+
void transformsSimpleTimed() {
40+
rewriteRun(
41+
//language=java
42+
java(
43+
"""
44+
package com.example;
45+
46+
import com.codahale.metrics.annotation.Timed;
47+
48+
class TestClass {
49+
@Timed
50+
public void timedMethod() {
51+
}
52+
}
53+
""",
54+
"""
55+
package com.example;
56+
57+
import io.micrometer.core.annotation.Timed;
58+
59+
class TestClass {
60+
@Timed
61+
public void timedMethod() {
62+
}
63+
}
64+
"""
65+
)
66+
);
67+
}
68+
69+
@Test
70+
void transformsTimedWithName() {
71+
rewriteRun(
72+
//language=java
73+
java(
74+
"""
75+
package com.example;
76+
77+
import com.codahale.metrics.annotation.Timed;
78+
79+
class TestClass {
80+
@Timed(name = "customMetricName")
81+
public void timedMethod() {
82+
}
83+
}
84+
""",
85+
"""
86+
package com.example;
87+
88+
import io.micrometer.core.annotation.Timed;
89+
90+
class TestClass {
91+
@Timed(value = "customMetricName")
92+
public void timedMethod() {
93+
}
94+
}
95+
"""
96+
)
97+
);
98+
}
99+
100+
@Test
101+
void transformsTimedWithMultipleAttributes() {
102+
rewriteRun(
103+
//language=java
104+
java(
105+
"""
106+
package com.example;
107+
108+
import com.codahale.metrics.annotation.Timed;
109+
110+
class TestClass {
111+
@Timed(name = "customMetricName", absolute = true, description = "Method execution time")
112+
public void timedMethod() {
113+
}
114+
}
115+
""",
116+
"""
117+
package com.example;
118+
119+
import io.micrometer.core.annotation.Timed;
120+
121+
class TestClass {
122+
@Timed(value = "customMetricName", absolute = true, description = "Method execution time")
123+
public void timedMethod() {
124+
}
125+
}
126+
"""
127+
)
128+
);
129+
}
130+
131+
@Test
132+
void ignoresUnmappedAttributes() {
133+
rewriteRun(
134+
//language=java
135+
java(
136+
"""
137+
package com.example;
138+
139+
import com.codahale.metrics.annotation.Timed;
140+
import java.util.concurrent.TimeUnit;
141+
142+
class TestClass {
143+
@Timed(
144+
name = "customMetricName",
145+
rateUnit = TimeUnit.SECONDS,
146+
durationUnit = TimeUnit.MILLISECONDS
147+
)
148+
public void timedMethod() {
149+
}
150+
}
151+
""",
152+
"""
153+
package com.example;
154+
155+
import io.micrometer.core.annotation.Timed;
156+
157+
import java.util.concurrent.TimeUnit;
158+
159+
class TestClass {
160+
@Timed(
161+
value = "customMetricName")
162+
public void timedMethod() {
163+
}
164+
}
165+
"""
166+
)
167+
);
168+
}
169+
}

0 commit comments

Comments
 (0)