Skip to content

Commit

Permalink
Adds the transient modifier to the cache field. This tells serialization
Browse files Browse the repository at this point in the history
to avoid serializing such fields. Not having this causes serialization
to be nondeterministic, depending on whether or not the memoized accessor has
been called.

This is more robust than depending on the @lazyinit annotation, which the processor sometimes can't find.

RELNOTES=none

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=220301585
  • Loading branch information
aoeui authored and ronshapiro committed Nov 6, 2018
1 parent b85dbf6 commit 1820660
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
import static javax.lang.model.element.Modifier.TRANSIENT;
import static javax.lang.model.element.Modifier.VOLATILE;
import static javax.lang.model.type.TypeKind.VOID;
import static javax.lang.model.util.ElementFilter.methodsIn;
Expand Down Expand Up @@ -324,11 +325,11 @@ private boolean pullDownMethodAnnotation(AnnotationMirror annotation) {

/**
* Builds a {@link FieldSpec} for use in property caching. Field will be {@code private
* volatile} and have the given type and name. If the @LazyInit annotation is available it is
* added as well.
* transient volatile} and have the given type and name. If the @LazyInit annotation is
* available it is added as well.
*/
private FieldSpec buildCacheField(TypeName type, String name) {
FieldSpec.Builder builder = FieldSpec.builder(type, name, PRIVATE, VOLATILE);
FieldSpec.Builder builder = FieldSpec.builder(type, name, PRIVATE, TRANSIENT, VOLATILE);
if (lazyInitAnnotation.isPresent()) {
builder.addAnnotation(lazyInitAnnotation.get());
builder.addAnnotation(SUPPRESS_WARNINGS);
Expand Down

0 comments on commit 1820660

Please sign in to comment.