Skip to content

Commit

Permalink
Merge pull request #134 from openrewrite/serializable-uid-context-free
Browse files Browse the repository at this point in the history
Context-free template for `AddSerialVersionUidToSerializable`
  • Loading branch information
knutwannheden authored Nov 22, 2023
2 parents 039558f + 31b492d commit e9190fa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Set<String> getTags() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
final JavaTemplate template = JavaTemplate.builder("private static final long serialVersionUID = 1;").contextSensitive().build();
final JavaTemplate template = JavaTemplate.builder("private static final long serialVersionUID = 1;").build();

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
Expand Down Expand Up @@ -100,11 +100,10 @@ private J.VariableDeclarations maybeFixVariableDeclarations(J.VariableDeclaratio
if (!J.Modifier.hasModifier(modifiers, J.Modifier.Type.Private)
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Static)
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Final)) {
Space singleSpace = Space.build(" ", Collections.emptyList());
varDecls = varDecls.withModifiers(Arrays.asList(
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null, J.Modifier.Type.Private, Collections.emptyList()),
new J.Modifier(Tree.randomId(), singleSpace, Markers.EMPTY, null, J.Modifier.Type.Static, Collections.emptyList()),
new J.Modifier(Tree.randomId(), singleSpace, Markers.EMPTY, null, J.Modifier.Type.Final, Collections.emptyList())
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Static, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Final, Collections.emptyList())
));
}
if (TypeUtils.asPrimitive(varDecls.getType()) != JavaType.Primitive.Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,17 @@ void serializableInnerClass() {
java(
"""
import java.io.Serializable;
public class Outer {
public class Outer implements Serializable {
public static class Inner implements Serializable {
}
}
""",
"""
import java.io.Serializable;
public class Outer {
public class Outer implements Serializable {
private static final long serialVersionUID = 1;
public static class Inner implements Serializable {
private static final long serialVersionUID = 1;
}
}
"""
Expand Down

0 comments on commit e9190fa

Please sign in to comment.