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

Auto-generated SubjectFactory implementations via an annotation processor #251

Closed
cgruber opened this issue Aug 10, 2016 · 2 comments
Closed

Comments

@cgruber
Copy link
Contributor

cgruber commented Aug 10, 2016

SubjectFactory exists to permit some strongly-typed flexibility (assertAbout, as well as a previously deleted, but soon returning iterating-subject wrapper). However, it's entirely computable boilerplate. It should be generated, and subjects decorated with @TruthSubject in order to signal the need to generate it.

The style will be similar to @AutoFactory in that a subject implementor will create a static method to return the SubjectFactory, and will simply return the generated one.

The contract will not be changed, so existing SubjectFactory-returning methods will work, and an implementor is free to follow the convention but create their own SubjectFactory for odd cases such as stateful factories, but for the default case, reducing the burden of the subject writer is important.

The general form will be:

@TruthSubject
public final class FooSubject extends Subject {
  // ...

  public static class FooSubjectFactory foos() {
    return new FooSubjectFactory();
  }
}

Since it is, at this point, an optional convenience, complexity will be kept low (e.g., the naming convention of the factory name will be simply FooSubject.getSimpleName() + "Factory"

Additional validation will be performed, in the initial version, which will confirm that there exists a public static method that returns the generated type, registering a compiler error if it is not.

Future validations may be added to this, to clean up subject writing, or to generate
other types that might be needed, such as an iterating subject wrapper, and this processor
will form the anchor-point for those features.

@cgruber cgruber added this to the Release 1.1 milestone Aug 10, 2016
@cpovirk
Copy link
Member

cpovirk commented Sep 19, 2018

We've simplified SubjectFactory (nowadays, Subject.Factory) to the point that it's usually just a method reference nowadays, so that part of this issue got resolved in that way instead.

We should still have lots of static analysis for custom Truth subjects. I'll eventually open some feature requests against Error Prone.

@astubbs
Copy link

astubbs commented Nov 3, 2021

As mentioned in #612 this is now available here: https://github.com/astubbs/truth-generator as a pre-release SNAPSHOT that can be installed locally. Will work on getting it into repo1 soon.

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

3 participants