Skip to content

Commit fd9d527

Browse files
committed
migrate recipe as-is
1 parent 3f52b88 commit fd9d527

File tree

5 files changed

+493
-0
lines changed

5 files changed

+493
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2024 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.migrate.lombok;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.JavaParser;
25+
import org.openrewrite.java.JavaTemplate;
26+
import org.openrewrite.java.tree.J;
27+
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.Set;
31+
import java.util.stream.Collectors;
32+
import java.util.stream.Stream;
33+
34+
import static java.util.Comparator.comparing;
35+
36+
@Value
37+
@EqualsAndHashCode(callSuper = false)
38+
public class UseDataParameterized extends Recipe {
39+
@Override
40+
public String getDisplayName() {
41+
return "Summarize class annotations into @Data";
42+
}
43+
44+
@Override
45+
public String getDescription() {
46+
return "Summarize class annotations into @Data.";
47+
}
48+
49+
List<String> exceptions;
50+
51+
@Override
52+
public TreeVisitor<?, ExecutionContext> getVisitor() {
53+
return new Summarizer(exceptions == null ? Collections.emptyList(): exceptions);
54+
}
55+
56+
private static class Summarizer extends JavaIsoVisitor<ExecutionContext> {
57+
58+
Set<String> annotationsToReplace = Stream
59+
.of(
60+
"ToString",
61+
"EqualsAndHashCode",
62+
"Getter",
63+
"Setter",
64+
"RequiredArgsConstructor")
65+
.collect(Collectors.toSet());
66+
67+
Set<String> needed;
68+
69+
public Summarizer(List<String> exceptions) {
70+
needed = annotationsToReplace
71+
.stream()
72+
.filter(a -> !exceptions.contains(a))
73+
.collect(Collectors.toSet());
74+
}
75+
76+
@Override
77+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
78+
79+
J.ClassDeclaration visited = super.visitClassDeclaration(classDecl, ctx);
80+
81+
Set<String> namesOfRemainingAnotations = visited.getLeadingAnnotations()
82+
.stream()
83+
.map(J.Annotation::getSimpleName)
84+
.collect(Collectors.toSet());
85+
Set<String> namesOfRemovedAnnotations = classDecl.getLeadingAnnotations()
86+
.stream()
87+
.map(J.Annotation::getSimpleName)
88+
.filter(a -> !namesOfRemainingAnotations.contains(a))
89+
.collect(Collectors.toSet());
90+
91+
if (visited != classDecl && namesOfRemovedAnnotations.containsAll(needed)) {
92+
93+
maybeRemoveImport("lombok.ToString");
94+
maybeRemoveImport("lombok.EqualsAndHashCode");
95+
maybeRemoveImport("lombok.Getter");
96+
maybeRemoveImport("lombok.Setter");
97+
maybeRemoveImport("lombok.RequiredArgsConstructor");
98+
maybeAddImport("lombok.Data");
99+
100+
JavaTemplate template = JavaTemplate.builder("@Data\n")
101+
.imports("lombok.Data")
102+
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
103+
.build();
104+
105+
return template.apply(
106+
updateCursor(visited),
107+
visited.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
108+
}
109+
return classDecl;
110+
}
111+
112+
@Override
113+
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
114+
return annotationsToReplace.contains(annotation.getSimpleName())
115+
&& annotation.getArguments() == null //no arguments of any kind. Too strict?
116+
//should only trigger on class annotation
117+
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration
118+
? null
119+
: annotation;
120+
}
121+
122+
}
123+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright 2024 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+
17+
---
18+
type: specs.openrewrite.org/v1beta/recipe
19+
name: org.openrewrite.java.migrate.lombok.UseDataNegligently
20+
displayName: Summarize class annotations into @Data negligently
21+
description: >-
22+
Summarize class annotations into @Data even if @ToString or @EqualsAndHashCode are missing. You might need to manually clean up the result!.
23+
recipeList:
24+
- org.openrewrite.java.migrate.lombok.UseDataParameterized:
25+
exceptions:
26+
- EqualsAndHashCode
27+
- ToString
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Copyright 2024 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+
17+
---
18+
type: specs.openrewrite.org/v1beta/recipe
19+
name: org.openrewrite.java.migrate.lombok.UseData
20+
displayName: Summarize class annotations into @Data
21+
description: >-
22+
Summarize class annotations into @Data.
23+
recipeList:
24+
- org.openrewrite.java.migrate.lombok.UseDataParameterized:
25+
exceptions: []
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2024 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.migrate.lombok;
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 UseDataNegligentlyTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.recipeFromResources("org.openrewrite.java.migrate.lombok.UseDataNegligently")
31+
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)
32+
.classpath("lombok"));
33+
}
34+
35+
@DocumentExample
36+
@Test
37+
void replaceOneFieldData() {
38+
rewriteRun(// language=java
39+
java(
40+
"""
41+
import lombok.Getter;
42+
import lombok.Setter;
43+
import lombok.RequiredArgsConstructor;
44+
45+
@Getter
46+
@Setter
47+
@RequiredArgsConstructor
48+
class A {}
49+
""",
50+
"""
51+
import lombok.Data;
52+
53+
@Data
54+
class A {}
55+
"""
56+
)
57+
);
58+
}
59+
60+
@Test
61+
void replaceWhenOptionalAnnotationsPresent() {
62+
rewriteRun(// language=java
63+
java(
64+
"""
65+
import lombok.ToString;
66+
import lombok.EqualsAndHashCode;
67+
import lombok.Getter;
68+
import lombok.Setter;
69+
import lombok.RequiredArgsConstructor;
70+
71+
@ToString
72+
@EqualsAndHashCode
73+
@Getter
74+
@Setter
75+
@RequiredArgsConstructor
76+
class A {}
77+
""",
78+
"""
79+
import lombok.Data;
80+
81+
@Data
82+
class A {}
83+
"""
84+
)
85+
);
86+
}
87+
88+
@Test
89+
void otherAnnotationsAround() {
90+
rewriteRun(// language=java
91+
java(
92+
"""
93+
import lombok.*;
94+
import lombok.extern.java.Log;
95+
96+
@NoArgsConstructor
97+
@ToString
98+
@EqualsAndHashCode
99+
@Getter
100+
@Setter
101+
@RequiredArgsConstructor
102+
@Log
103+
class A {}
104+
""",
105+
"""
106+
import lombok.Data;
107+
import lombok.NoArgsConstructor;
108+
import lombok.extern.java.Log;
109+
110+
@Data
111+
@NoArgsConstructor
112+
@Log
113+
class A {}
114+
"""
115+
)
116+
);
117+
}
118+
119+
@Test
120+
void oneAnnotationMissing() {
121+
rewriteRun(// language=java
122+
java(
123+
"""
124+
import lombok.*;
125+
import lombok.extern.java.Log;
126+
127+
@NoArgsConstructor
128+
@ToString
129+
@EqualsAndHashCode
130+
@Getter
131+
@RequiredArgsConstructor
132+
@Log
133+
class A {}
134+
"""
135+
)
136+
);
137+
}
138+
139+
}

0 commit comments

Comments
 (0)