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

feat: use @Data #646

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
@@ -0,0 +1,123 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.lombok;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;

@Value
@EqualsAndHashCode(callSuper = false)
public class UseDataParameterized extends Recipe {
@Override
public String getDisplayName() {
return "Summarize class annotations into @Data";
}

@Override
public String getDescription() {
return "Summarize class annotations into @Data.";
}

List<String> exceptions;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new Summarizer(exceptions == null ? Collections.emptyList(): exceptions);
}

private static class Summarizer extends JavaIsoVisitor<ExecutionContext> {

Set<String> annotationsToReplace = Stream
.of(
"ToString",
"EqualsAndHashCode",
"Getter",
"Setter",
"RequiredArgsConstructor")
.collect(Collectors.toSet());

Set<String> needed;

public Summarizer(List<String> exceptions) {
needed = annotationsToReplace
.stream()
.filter(a -> !exceptions.contains(a))
.collect(Collectors.toSet());
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {

J.ClassDeclaration visited = super.visitClassDeclaration(classDecl, ctx);

Set<String> namesOfRemainingAnotations = visited.getLeadingAnnotations()
.stream()
.map(J.Annotation::getSimpleName)
.collect(Collectors.toSet());
Set<String> namesOfRemovedAnnotations = classDecl.getLeadingAnnotations()
.stream()
.map(J.Annotation::getSimpleName)
.filter(a -> !namesOfRemainingAnotations.contains(a))
.collect(Collectors.toSet());

if (visited != classDecl && namesOfRemovedAnnotations.containsAll(needed)) {

maybeRemoveImport("lombok.ToString");
maybeRemoveImport("lombok.EqualsAndHashCode");
maybeRemoveImport("lombok.Getter");
maybeRemoveImport("lombok.Setter");
maybeRemoveImport("lombok.RequiredArgsConstructor");
maybeAddImport("lombok.Data");

JavaTemplate template = JavaTemplate.builder("@Data\n")
.imports("lombok.Data")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build();

return template.apply(
updateCursor(visited),
visited.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
}
return classDecl;
}

@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
return annotationsToReplace.contains(annotation.getSimpleName())
&& annotation.getArguments() == null //no arguments of any kind. Too strict?
//should only trigger on class annotation
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration
? null
Comment on lines +114 to +118
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return annotationsToReplace.contains(annotation.getSimpleName())
&& annotation.getArguments() == null //no arguments of any kind. Too strict?
//should only trigger on class annotation
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration
? null
return annotationsToReplace.contains(annotation.getSimpleName()) &&
annotation.getArguments() == null //no arguments of any kind. Too strict?
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration ?
null :
annotation;

Comment on lines +114 to +118
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return annotationsToReplace.contains(annotation.getSimpleName())
&& annotation.getArguments() == null //no arguments of any kind. Too strict?
//should only trigger on class annotation
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration
? null
return annotationsToReplace.contains(annotation.getSimpleName()) &&
annotation.getArguments() == null //no arguments of any kind. Too strict?
&& getCursor().getParent().getValue() instanceof J.ClassDeclaration ?
null :
annotation;

: annotation;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright 2024 the original author or authors.
# <p>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.lombok.UseDataNegligently
displayName: Summarize class annotations into @Data negligently
description: >-
Summarize class annotations into @Data even if @ToString or @EqualsAndHashCode are missing. You might need to manually clean up the result!.
recipeList:
- org.openrewrite.java.migrate.lombok.UseDataParameterized:
exceptions:
- EqualsAndHashCode
- ToString
25 changes: 25 additions & 0 deletions src/main/resources/META-INF/rewrite/use-lombok-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright 2024 the original author or authors.
# <p>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.lombok.UseData
displayName: Summarize class annotations into @Data
description: >-
Summarize class annotations into @Data.
recipeList:
- org.openrewrite.java.migrate.lombok.UseDataParameterized:
exceptions: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.lombok;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class UseDataNegligentlyTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.java.migrate.lombok.UseDataNegligently")
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)
.classpath("lombok"));
}

@DocumentExample
@Test
void replaceOneFieldData() {
rewriteRun(// language=java
java(
"""
import lombok.Getter;
import lombok.Setter;
import lombok.RequiredArgsConstructor;

@Getter
@Setter
@RequiredArgsConstructor
class A {}
""",
"""
import lombok.Data;

@Data
class A {}
"""
)
);
}

@Test
void replaceWhenOptionalAnnotationsPresent() {
rewriteRun(// language=java
java(
"""
import lombok.ToString;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.RequiredArgsConstructor;

@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor
class A {}
""",
"""
import lombok.Data;

@Data
class A {}
"""
)
);
}

@Test
void otherAnnotationsAround() {
rewriteRun(// language=java
java(
"""
import lombok.*;
import lombok.extern.java.Log;

@NoArgsConstructor
@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor
@Log
class A {}
""",
"""
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.java.Log;

@Data
@NoArgsConstructor
@Log
class A {}
"""
)
);
}

@Test
void oneAnnotationMissing() {
rewriteRun(// language=java
java(
"""
import lombok.*;
import lombok.extern.java.Log;

@NoArgsConstructor
@ToString
@EqualsAndHashCode
@Getter
@RequiredArgsConstructor
@Log
class A {}
"""
)
);
}

}
Loading
Loading