-
Notifications
You must be signed in to change notification settings - Fork 93
Recipe AddSerialAnnotationToserialVersionUID #247
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3149870
Added file for AddSerialAnnotationToserialVersionUID
jbessels 3e46468
Ran gradlew licenseFormat
jbessels c7437e2
Apply suggestions from code review
timtebeek 9fc54af
Merge branch 'main' into main
timtebeek 9c7fdc5
Apply suggestions from code review
timtebeek 65f37fe
Apply formatter and remove offending elements
timtebeek 954c396
Merge branch 'main' into main
timtebeek bbf76ba
Complete implementation for fields
timtebeek 5724216
Add missing newline at end of file
timtebeek 47cfb9b
Only annotate in serializable classes
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/main/java/org/openrewrite/staticanalysis/AddSerialAnnotationToserialVersionUID.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * 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.staticanalysis; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.internal.lang.NonNull; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.search.UsesJavaVersion; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.Statement; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.UnaryOperator; | ||
|
|
||
| public class AddSerialAnnotationToserialVersionUID extends Recipe { | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Add `@Serial` annotation to `serialVersionUID`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Annotation any `serialVersionUID` fields with `@Serial` to indicate it's part of the serialization mechanism."; | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(1); | ||
| } | ||
|
|
||
| @Override | ||
| @NonNull | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check( | ||
| new UsesJavaVersion<>(14), | ||
| new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| @NonNull | ||
| public J.ClassDeclaration visitClassDeclaration(J.@NonNull ClassDeclaration classDecl, @NonNull ExecutionContext ctx) { | ||
| J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx); | ||
| if (c.getKind() != J.ClassDeclaration.Kind.Type.Class) { | ||
| return c; | ||
| } | ||
|
|
||
| AtomicBoolean needsSerialAnnotation = new AtomicBoolean(false); | ||
| c = c.withBody(c.getBody().withStatements(ListUtils.map(c.getBody().getStatements(), new UnaryOperator<Statement>() { | ||
| @Override | ||
| public Statement apply(Statement s) { | ||
| if (!(s instanceof J.VariableDeclarations)) { | ||
| return s; | ||
| } | ||
| J.VariableDeclarations varDecls = (J.VariableDeclarations) s; | ||
| // Yes I know deprecated: varDecls.getAllAnnotations() | ||
| List<J.Annotation> allAnnotations = varDecls.getAllAnnotations(); | ||
| long count = allAnnotations.stream().count(); | ||
| AtomicBoolean hasSerialAnnotation = new AtomicBoolean(false); | ||
| for (J.Annotation annotation : allAnnotations) { | ||
| String simpleName = annotation.getSimpleName(); | ||
| if (simpleName.equals("Serial")) { | ||
| hasSerialAnnotation.set(true); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| for (J.VariableDeclarations.NamedVariable v : varDecls.getVariables()) { | ||
| if ("serialVersionUID".equals(v.getSimpleName())) { | ||
| JavaType type = v.getType(); | ||
|
|
||
| if (type instanceof JavaType.Primitive) { | ||
| if (TypeUtils.asPrimitive(v.getType()) == JavaType.Primitive.Long) { | ||
| if (hasSerialAnnotation.get()) { | ||
| needsSerialAnnotation.set(false); | ||
| } else { | ||
| needsSerialAnnotation.set(true); | ||
| } | ||
| return s; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return s; | ||
| } | ||
| }))); | ||
| if (needsSerialAnnotation.get()) { | ||
| c = JavaTemplate.apply("@Serial", getCursor(), c.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); | ||
| // It HAS to be added. This method seems to be the easiest way to do it. Does NOT work | ||
| maybeAddImport("java.io.Serial"); | ||
| } | ||
| return c; | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
src/test/java/org/openrewrite/staticanalysis/AddSerialAnnotationToserialVersionUIDTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * 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.staticanalysis; | ||
|
|
||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class AddSerialAnnotationToserialVersionUIDTest implements RewriteTest { | ||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new AddSerialAnnotationToserialVersionUID()); | ||
| } | ||
|
|
||
| @Test | ||
| void serialAnnotationAlreadyPresent() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.io.Serializable; | ||
| import java.io.Serial; | ||
|
|
||
| class Example implements Serializable { | ||
| String var1 = "first variable"; | ||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
| int var3 = 666; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void addSerialAnnotation() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.io.Serializable; | ||
| import java.io.Serial; | ||
|
|
||
| class Example implements Serializable { | ||
| String var1 = "first variable"; | ||
| private static final long serialVersionUID = 1L; | ||
| int var3 = 666; | ||
| } | ||
| """, | ||
| """ | ||
| import java.io.Serializable; | ||
| import java.io.Serial; | ||
|
|
||
| class Example implements Serializable { | ||
| String var1 = "first variable"; | ||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
| int var3 = 666; | ||
| String wolvie = "wolverine"; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Disabled | ||
| @Test | ||
| void methodDeclarationsAreNotVisited() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.io.Serializable; | ||
|
|
||
| class Example implements Serializable { | ||
| private String fred; | ||
| private int numberOfFreds; | ||
| void doSomething() { | ||
| long serialVersionUID = 1L; | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Disabled | ||
| @Test | ||
| void serializableInnerClass() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.io.Serializable; | ||
| public class Outer implements Serializable { | ||
| public static class Inner implements Serializable { | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.io.Serializable; | ||
| class Outer implements Serializable { | ||
| private static final long serialVersionUID = 1; | ||
| static class Inner implements Serializable { | ||
| private static final long serialVersionUID = 1; | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.