Skip to content

Commit

Permalink
Show @Serial addition when chained with adding field
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 24, 2024
1 parent 56503f5 commit 5c43d94
Showing 1 changed file with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ void addSerialAnnotation() {
java(
"""
import java.io.Serializable;
class Example implements Serializable {
private static final long serialVersionUID = 1L;
}
""",
"""
import java.io.Serial;
import java.io.Serializable;
class Example implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
Expand All @@ -57,14 +57,41 @@ class Example implements Serializable {
}

@Test
void shouldNoopIfAlreadyPresent() {
void shouldAddToNewFieldWhenChained() {
rewriteRun(
spec -> spec.recipes(
new AddSerialVersionUidToSerializable(),
new AddSerialAnnotationToSerialVersionUID()),
//language=java
java(
"""
import java.io.Serializable;
class Example implements Serializable {
}
""",
"""
import java.io.Serial;
import java.io.Serializable;
class Example implements Serializable {
@Serial
private static final long serialVersionUID = 1;
}
"""
)
);
}

@Test
void shouldNoopIfAlreadyPresent() {
rewriteRun(
//language=java
java(
"""
import java.io.Serializable;
import java.io.Serial;
class Example implements Serializable {
String var1 = "first variable";
@Serial
Expand Down Expand Up @@ -97,7 +124,7 @@ void shouldNotAnnotateOnJava11() {
java(
"""
import java.io.Serializable;
class Example implements Serializable {
private static final long serialVersionUID = 1L;
}
Expand All @@ -114,14 +141,14 @@ void shouldNotAnnotateOtherFields() {
java(
"""
import java.io.Serializable;
class Example implements Serializable {
static final long serialVersionUID = 1L;
private final long serialVersionUID = 1L;
private static long serialVersionUID = 1L;
private static final int serialVersionUID = 1L;
private static final long foo = 1L;
void doSomething() {
long serialVersionUID = 1L;
}
Expand All @@ -138,7 +165,7 @@ void shouldAnnotatedFieldsInInnerClasses() {
java(
"""
import java.io.Serializable;
class Outer implements Serializable {
private static final long serialVersionUID = 1;
static class Inner implements Serializable {
Expand All @@ -149,7 +176,7 @@ static class Inner implements Serializable {
"""
import java.io.Serial;
import java.io.Serializable;
class Outer implements Serializable {
@Serial
private static final long serialVersionUID = 1;
Expand Down

0 comments on commit 5c43d94

Please sign in to comment.