Auto Value is a Java library which helps you to generate value types
correctly. A value type is class without identity: two instances are considered
interchangeable as long as they have equal field values. Examples:
Uri… but you also tend to create a great many of these yourself.
You know the kind: they’re the ones where you have to implement
hashCode(), and usually
Before Auto Value
Before Auto Value, creating a value type is not easy. In order to implement it
correctly, you need to declare all the fields manually, mark them as
and only expose the getters; you need to implement
(often handled by IDE), and keep them up to date when attributes
changed; you also need to mark the class as
final to prevent subclassing,
which guarantees the equality.
A classical value type looks like:
When using Auto Value, everything is easier. You just need to write the following code, and Google Auto Value takes care the rest:
Behind the scenes, Auto Value generates all the private fields, the constructor,
toString() for you. The generated class always
starts with “AutoValue_“, more explicitly, it naming convention is
Pretty cool. But what is the real benefit of using Auto Value?
Here’s the explanation from Auto Value: Auto Value is the only solution to the value class problem in Java having all of the following characteristics:
- API-invisible (callers cannot become dependent on your choice to use it)
- No runtime dependencies
- Negligible cost to performance
- Very few limitations on what your class can do
- Extralinguistic “magic” kept to an absolute minimum (uses only standard Java platform technologies, in the manner they were intended)
I would also summary it as a comparison table:
|Auto attr declaration||N||Y|
|Auto update toString()||N||Y|
|Auto update hashCode()||N||Y|
|Auto update equals()||N||Y|
(*) If you implement it correctly.
Solution 1. In Maven dependencies, you need to declare 2 dependencies for AutoValue: auto-value-annotations and auto-value. The first one, auto-value-annotations is used for the AutoValue annotations; and the second one, auto-value is used for annotation processing (code generation).
The second dependency is declared as provided because the AutoValue processor is only used during compilation, and not used at runtime.
Solution 2. Use annoation processor path from the Maven compiler plugin. In this way the processor is separated from the actual project dependencies.
More information can be seen here: Stack Overflow: Maven 3 - How to add annotation processor dependency?
When using it, you can do:
In this post, we’ve seen the basics of Google Auto Value library. How to use it
to create value class with little effort. It generates the
toString() for you. We also checked the Maven setup. And finally,
we learnt a builder pattern for creating complex value class. Hope you enjoy
this one, see you next time!
The source code is available on GitHub: https://github.com/mincong-h/auto-value-demo