Skip to content

Commit 71d8121

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Copy explicit serialVersionUID from @AutoOneOf classes to their generated subclasses.
Previously the subclasses did not have an explicit `serialVersionUID`, so their implicit ones were likely to change if any details of the code-generation changed. This change BREAKS SERIAL COMPATIBILITY for `@AutoOneOf` classes that had an explicit `serialVersionUID`. Also, don't bother generating the shared `Parent_` class in the degenerate case of an `@AutoOneOf` class with no alternatives. These changes reduce the number of warnings from `CompileWithEclipseTest` from 11 to 3. RELNOTES=If an `@AutoOneOf` class has a `serialVersionUID` this is now copied to its generated subclasses. THIS BREAKS SERIAL COMPATIBILITY for `@AutoOneOf` classes with explicit `serialVersionUID` declarations, though those were already liable to be broken by arbitrary changes to the generated AutoOneOf code. PiperOrigin-RevId: 364387942
1 parent f14d633 commit 71d8121

File tree

7 files changed

+36
-13
lines changed

7 files changed

+36
-13
lines changed

value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ final void defineSharedVarsForType(
433433
vars.equals = methodsToGenerate.containsKey(ObjectMethod.EQUALS);
434434
vars.hashCode = methodsToGenerate.containsKey(ObjectMethod.HASH_CODE);
435435
vars.equalsParameterType = equalsParameterType(methodsToGenerate);
436+
vars.serialVersionUID = getSerialVersionUID(type);
436437
}
437438

438439
/** Returns the spelling to be used in the generated code for the given list of annotations. */
@@ -833,8 +834,9 @@ public Boolean visitArray(List<? extends AnnotationValue> list, Void p) {
833834
}
834835

835836
/**
836-
* Returns a string like {@code "1234L"} if {@code type instanceof Serializable} and defines
837-
* {@code serialVersionUID = 1234L}; otherwise {@code ""}.
837+
* Returns a string like {@code "private static final long serialVersionUID = 1234L"} if {@code
838+
* type instanceof Serializable} and defines {@code serialVersionUID = 1234L}; otherwise {@code
839+
* ""}.
838840
*/
839841
final String getSerialVersionUID(TypeElement type) {
840842
TypeMirror serializable = elementUtils().getTypeElement(Serializable.class.getName()).asType();
@@ -846,7 +848,7 @@ final String getSerialVersionUID(TypeElement type) {
846848
if (field.getModifiers().containsAll(Arrays.asList(Modifier.STATIC, Modifier.FINAL))
847849
&& field.asType().getKind() == TypeKind.LONG
848850
&& value != null) {
849-
return value + "L";
851+
return "private static final long serialVersionUID = " + value + "L;";
850852
} else {
851853
errorReporter.reportError(
852854
field, "serialVersionUID must be a static final long compile-time constant");

value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfTemplateVars.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ abstract class AutoValueOrOneOfTemplateVars extends TemplateVars {
8181
* wildcard, for example {@code <?, ?>}.
8282
*/
8383
String wildcardTypes;
84+
85+
/**
86+
* The text of the complete serialVersionUID declaration, or empty if there is none. When
87+
* non-empty, it will be something like {@code private static final long serialVersionUID =
88+
* 123L;}.
89+
*/
90+
String serialVersionUID;
8491
}

value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ private void defineVarsForType(
431431
propertyMethodAnnotationMap(type, propertyMethods);
432432
vars.props =
433433
propertySet(propertyMethodsAndTypes, annotatedPropertyFields, annotatedPropertyMethods);
434-
vars.serialVersionUID = getSerialVersionUID(type);
435434
// Check for @AutoValue.Builder and add appropriate variables if it is present.
436435
maybeBuilder.ifPresent(
437436
builder -> {

value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class AutoValueTemplateVars extends AutoValueOrOneOfTemplateVars {
5656
*/
5757
String gwtCompatibleAnnotation;
5858

59-
/** The text of the serialVersionUID constant, or empty if there is none. */
60-
String serialVersionUID;
61-
6259
/** The simple name of the generated subclass. */
6360
String subclass;
6461
/**

value/src/main/java/com/google/auto/value/processor/autooneof.vm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,23 @@ final class $generatedClass {
9494

9595
#end
9696

97+
#if (!$props.empty)
9798
// Parent class that each implementation will inherit from.
9899
private abstract static class Parent_$formalTypes extends $origClass$actualTypes {
99100

100-
#foreach ($p in $props)
101+
$serialVersionUID
102+
103+
#foreach ($p in $props)
101104

102105
@`java.lang.Override`
103106
$p.access $p.type ${p.getter}() {
104107
throw new UnsupportedOperationException(${kindGetter}().toString());
105108
}
106109

107-
#end
110+
#end
108111

109112
}
113+
#end
110114

111115
#foreach ($p in $props)
112116

@@ -120,6 +124,8 @@ final class $generatedClass {
120124
// Implementation when the contained property is "${p}".
121125
private static final class Impl_$p$formalTypes extends Parent_$actualTypes {
122126

127+
$serialVersionUID
128+
123129
#if ($p.type == "void")
124130

125131
// There is only one instance of this class.

value/src/main/java/com/google/auto/value/processor/autovalue.vm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes {
177177
}
178178
#end
179179

180-
#if (!$serialVersionUID.empty)
181-
private static final long serialVersionUID = $serialVersionUID;
182-
#end
180+
$serialVersionUID
183181

184182
#if ($builderTypeName != "")
185183

value/src/test/java/com/google/auto/value/processor/AutoOneOfCompilationTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public void success() {
4343
"import java.io.Serializable;",
4444
"",
4545
"@AutoOneOf(TaskResult.Kind.class)",
46-
"public abstract class TaskResult<V, T extends Throwable> {",
46+
"public abstract class TaskResult<V, T extends Throwable> implements Serializable {",
47+
" private static final long serialVersionUID = 1234L;",
48+
"",
4749
" public enum Kind {VALUE, EXCEPTION, EMPTY}",
4850
" public abstract Kind getKind();",
4951
"",
@@ -92,6 +94,8 @@ public void success() {
9294
" // Parent class that each implementation will inherit from.",
9395
" private abstract static class Parent_<V, T extends Throwable> "
9496
+ "extends TaskResult<V, T> {",
97+
" private static final long serialVersionUID = 1234L;",
98+
"",
9599
" @Override",
96100
" public V value() {",
97101
" throw new UnsupportedOperationException(getKind().toString());",
@@ -111,6 +115,8 @@ public void success() {
111115
" // Implementation when the contained property is \"value\".",
112116
" private static final class Impl_value<V, T extends Throwable> "
113117
+ "extends Parent_<V, T> {",
118+
" private static final long serialVersionUID = 1234L;",
119+
"",
114120
" private final V value;",
115121
"",
116122
" Impl_value(V value) {",
@@ -152,6 +158,8 @@ public void success() {
152158
" // Implementation when the contained property is \"exception\".",
153159
" private static final class Impl_exception<V, T extends Throwable> "
154160
+ "extends Parent_<V, T> {",
161+
" private static final long serialVersionUID = 1234L;",
162+
"",
155163
" private final Throwable exception;",
156164
"",
157165
" Impl_exception(Throwable exception) {",
@@ -193,13 +201,19 @@ public void success() {
193201
" // Implementation when the contained property is \"empty\".",
194202
" private static final class Impl_empty<V, T extends Throwable> "
195203
+ "extends Parent_<V, T> {",
204+
" private static final long serialVersionUID = 1234L;",
205+
"",
196206
" static final Impl_empty<?, ?> INSTANCE = new Impl_empty<>();",
197207
"",
198208
" private Impl_empty() {}",
199209
"",
200210
" @Override",
201211
" public void empty() {}",
202212
"",
213+
" private Object readResolve() {",
214+
" return INSTANCE;",
215+
" }",
216+
"",
203217
" @Override",
204218
" public String toString() {",
205219
" return \"TaskResult{empty}\";",

0 commit comments

Comments
 (0)