Skip to content
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

Fields generated by @Memoized should be excluded automatically #181

Open
ansman opened this issue Jun 8, 2017 · 4 comments
Open

Fields generated by @Memoized should be excluded automatically #181

ansman opened this issue Jun 8, 2017 · 4 comments

Comments

@ansman
Copy link
Contributor

ansman commented Jun 8, 2017

This class:

public abstract class Foo {
    @Memoized
    public String someMethod() {
        return "foo";
    }
}

Will generate a field called someMethod that unfortunately isn't transient so PaperParcel complains. Ideally they should be excluded automatically.

@grandstaish
Copy link
Owner

Are you able to decompile the bytecode into Java and post it here? (I don't have any means of doing this myself because I'm currently in the process of moving overseas)

@ansman
Copy link
Contributor Author

ansman commented Jun 9, 2017

This class:

@AutoValue
public abstract class Foo {
    @Memoized
    public String foo() {
        return "foo";
    }
}

generates

@Generated("com.google.auto.value.processor.AutoValueProcessor")
 abstract class $AutoValue_Foo extends Foo {

  $AutoValue_Foo(
 ) {
  }

  @Override
  public String toString() {
    return "Foo{"
        + "}";
  }

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof Foo) {
      return true;
    }
    return false;
  }

  @Override
  public int hashCode() {
    int h = 1;
    return h;
  }

}

and

@Generated("com.google.auto.value.extension.memoized.MemoizeExtension")
final class AutoValue_Foo extends $AutoValue_Foo {
  private volatile String foo;

  AutoValue_Foo() {
    super();
  }

  @Override
  public String foo() {
    if (foo == null) {
      synchronized (this) {
        if (foo == null) {
          foo = super.foo();
          if (foo == null) {
            throw new NullPointerException("foo() cannot return null");
          }
        }
      }
    }
    return foo;
  }
}

The field always has the same name as the method.

If the field is nullable or primitive this is generated:

@Generated("com.google.auto.value.extension.memoized.MemoizeExtension")
final class AutoValue_Foo extends $AutoValue_Foo {
  private volatile String foo;

  private volatile boolean foo$Memoized;

  AutoValue_Foo() {
    super();
  }

  @Override
  @Nullable
  public String foo() {
    if (!foo$Memoized) {
      synchronized (this) {
        if (!foo$Memoized) {
          foo = super.foo();
          foo$Memoized = true;
        }
      }
    }
    return foo;
  }
}

@grandstaish
Copy link
Owner

grandstaish commented Jun 11, 2017

It's a workaround, but for now you could exclude all volatile fields from being parcelled. You can do it globally via the ProcessorConfig:

@ProcessorConfig(
  options = @PaperParcel.Options(
    excludeModifiers = { Modifier.TRANSIENT, Modifier.STATIC, Modifier.VOLATILE }
  )
)

@ansman
Copy link
Contributor Author

ansman commented Jun 11, 2017

Yeah, that's what I did now. Ideally this issue should be fixed: google/auto#406

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants