If you read Effective Java 2nd Edition and 3rd Edition -Qiita, google's Autovalue / google / auto) was introduced, so I tried it.
Tutorial If you follow the steps, you can try it all, but since Intellij environment settings were required, that is the main ..
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tada.suzu</groupId>
<artifactId>autoValueTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>autoValueTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
I don't care because the version of JUnit is just the same as the initial value when I created a new maven project.
If you keep the default, a compile error will occur, so set it.
package tada.suzu;
import com.google.auto.value.AutoValue;
@AutoValue
abstract class Animal {
static Animal create(String name, int numberOfLegs) {
//Where AutoValue_An error will occur if there is no class called Animal
return new AutoValue_Animal(name, numberOfLegs);
}
abstract String name();
abstract int numberOfLegs();
}
Click File → Settings
Select Build, Execution, Depeloyment → Compiler → Annotation Processors
Select a project and edit the Production sources directory and Test source directory.
After closing Settings, compile from the Build menu to generate AutoValue_Animal.java under src / main / generated.
Right-click the generated folder and select Generated Source Root from the Mark Directory as menu to add it to your build path.
With the above, the compilation error disappears and it is ready to work.
Well, it was almost written in stackoverflow, so I personally just make a note of how to use it.
I just pasted what was written in the document
public void testAnimal() {
Animal dog = Animal.create("dog", 4);
assertEquals("dog", dog.name());
assertEquals(4, dog.numberOfLegs());
// You probably don't need to write assertions like these; just illustrating.
assertTrue(Animal.create("dog", 4).equals(dog));
assertFalse(Animal.create("cat", 4).equals(dog));
assertFalse(Animal.create("dog", 2).equals(dog));
assertEquals("Animal{name=dog, numberOfLegs=4}", dog.toString());
}
At first I wondered if I had to be aware of the class that is automatically generated from the annotation AutoValue_Animal.java, but when the number of variables increases, the annotation automatically equals (), hashcode (), toString (). It may be appreciated that the method is regenerated. If you just want equals (), hashcode (), toString (), it's not much different from using Lombok or Kotlin. It seems that constructors with arguments and setters / getters are not generated, so it seems good to use them properly according to their purpose.
By the way, I wonder if there is an opportunity to consider whether or not to use it at work.
Recommended Posts