[JAVA] I tried the AutoValue library in Intellij

What is this

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.

Intellij Preferences

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();
}

  1. Click File → Settings

    setting.png

  2. Select Build, Execution, Depeloyment → Compiler → Annotation Processors

  3. Select a project and edit the Production sources directory and Test source directory.

    annotation.PNG

  4. After closing Settings, compile from the Build menu to generate AutoValue_Animal.java under src / main / generated.

  5. Right-click the generated folder and select Generated Source Root from the Mark Directory as menu to add it to your build path.

    mark.png

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.

Try to move

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());
    }

Personal view

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

I tried the AutoValue library in Intellij
I tried installing the Docker Integration plugin in IntelliJ
I tried the new era in Java
I tried to organize the session in Rails
I tried using the profiler of IntelliJ IDEA
I tried to organize the cases used in programming
I tried the new feature profiler of IntelliJ IDEA 2019.2.
I tried the VueJS tutorial!
I tried the FizzBuzz problem
I tried to implement the Euclidean algorithm in Java
I tried metaprogramming in Java
I tried using the GitHub repository as a library server
I tried using the CameraX library with Android Java Fragment
I tried putting Domino11 in CentOS7
I tried the Java framework "Quarkus"
[Rails] I tried deleting the application
I tried to summarize the words that I often see in docker-compose.yml
I tried to sort the data in descending order, ascending order / Rails
I tried using Elasticsearch API in Java
I tried a calendar problem in Ruby
I tried to summarize the methods used
[Java] Use cryptography in the standard library
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried embedding a formula in Javadoc
I tried running the Angular sample in Auth0 Quick Start with Docker
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to make full use of the CPU core in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried using Docker for the first time
[For beginners] I tried using DBUnit in Eclipse
Introducing the library
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[For beginners] I tried using JUnit 5 in Eclipse
3 ways to import the library in Android Studio
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to implement deep learning in Java
I made a Ruby extension library in C
I tried Spring.
[API] I tried using the zip code search API
I tried touching Docker for the first time
I tried tomcat
Set the source of the library set as a dependency in IntelliJ as a separate module of the project
I tried to output multiplication table in Java
I tried youtubeDataApi.
I tried refactoring ①
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried FizzBuzz.
I tried JHipster 5.1
I tried Firebase Local Emulator Suite in Flutter
I want to get the value in Ruby
I stumbled when I tried using neo4j in the jenv environment, so make a note
I was addicted to the NoSuchMethodError in Cloud Endpoints
I tried Mastodon's Toot and Streaming API in Java
I tried using the Server Push function of Servlet 4.0
I want to embed any TraceId in the log