Summary of ToString behavior with Java and Groovy annotations

this is?

My job is src.main is Java and src.test is Groovy, but I wrote src.main in Groovy with a little different requirement, so I got hooked around @ToString, so I summarized it. Note I don't know how meaningful the summary is

It's a system that creates a class with Groovy, runs it, and uses it while outputting variables to the terminal and log file with ʻAOP, but it wasn't properly ToString` at all, so I checked it.

Java Java has the annotation @ lombok.ToString (requires lombok)

lombok.ToString


public @interface ToString {
	boolean includeFieldNames() default true;

	String[] exclude() default {};

	String[] of() default {};

	boolean callSuper() default false;

	boolean doNotUseGetters() default false;
}

It is an excerpt excluding the javadoc part of the source, but the main point is the above setting items

Class Normally, it is used as ʻimport, but this time I want to specify it and write it like @ lombok.ToString` to reduce the number of lines of code posted.

1 No annotation

Who wants to see this

public class FooId1 {
    private final String value = "1234";
}
// to_string.j.c.FooId1@27c170f0

2 Just attach

It's easy, and depending on the situation, this is enough

@lombok.ToString
public class FooId2 {
    private final String value = "1234";
}
// FooId2(value=1234))

3 Eliminate field names

I'll give you an example later, but this is better if you have a class structure with many nests.

@lombok.ToString(includeFieldNames = false)
public class FooId3 {
    private final String value = "1234";
}
// FooId3(1234)

4 Used when there is an accessor

It is unlikely that you will write a strange accessor by handwriting, but it seems to be an accident and posted as a small story

@lombok.ToString
public class FooId4 {
    private final String value = "1234";

    public String getValue() {
        return "5678";
    }
}
// FooId4(value=5678)

5 Do not use accessors

If you write this, you can prevent accidents.

@lombok.ToString(doNotUseGetters = true)
public class FooId5 {
    private final String value = "1234";

    public String getValue() {
        return "5678";
    }
}
// FooId5(value=1234)

Nesting 2'2 example

If you just attach it, it's personally redundant

@lombok.ToString
public class Foo2 {
    private final FooId2 id = new FooId2();
    private final FooName2 name = new FooName2();
}
// Foo2(id=FooId2(value=1234), name=FooName2(value=John Doe))

Nesting 3'3 example

For the Java class, this is personally the best

@lombok.ToString(includeFieldNames = false)
public class Foo3 {
    private final FooId3 id = new FooId3();
    private final FooName3 name = new FooName3();
}
// Foo3(FooId3(1234), FooName3(John Doe))

Enum I also checked ʻEnum`

1 No annotation

ʻEnum` is fine with this

public enum FooStatus1 {
    APPLYING, NOT_APPLYING
}
// NOT_APPLYING

2 Just attach

It's nice to have the class name, but it doesn't include the essential part

@lombok.ToString
public enum FooStatus2 {
    APPLYING, NOT_APPLYING
}
// FooStatus2()

Groovy Groovy has the annotation @ groovy.transform.ToString, which is provided by the language.

lombok.ToString


public @interface ToString {
    String[] excludes() default {};

    String[] includes() default {};

    boolean includeSuper() default false;

    boolean includeSuperProperties() default false;

    boolean includeNames() default false;

    boolean includeFields() default false;

    boolean ignoreNulls() default false;

    boolean includePackage() default true;

    boolean cache() default false;
}

Similarly, only the setting items are excerpted

Class I didn't understand Groovy until I tried various things.

1 No annotation

Similarly, there is no demand for this

class FooId1 {
    private final String value = '1234'
}
// to_string.g.c.FooId1@3159c4b8

2 Just attach

It's not priced! ?? Virtually nothing has changed, right? ??

@groovy.transform.ToString
class FooId2 {
    private final String value = '1234'
}
// to_string.g.c.FooId2()

3 Try to make it public

I thought that private was bad, so I tried to make it public, but it did not solve

@groovy.transform.ToString
class FooId3 {
    public final String value = '1234'
}
// to_string.g.c.FooId3()

4 Remove the access modifier

Solved for some reason But I want to make it private

@groovy.transform.ToString
class FooId4 {
    final String value = '1234'
}
// to_string.g.c.FooId4(1234)

Explicitly include 5 fields

It seems that the field is not included by default when looking at various things

@groovy.transform.ToString(includeFields = true)
class FooId5 {
    private final String value = '1234'
}
// to_string.g.c.FooId5(1234)

6 Including field name

Can also be included

@groovy.transform.ToString(includeFields = true, includeNames = true)
class FooId6 {
    private final String value = '1234'
}
// to_string.g.c.FooId6(value:1234)

7 Do not include FQCN

If you think about the package configuration properly on a certain scale, it will be too long if there is FQCN, so I want to erase it

@groovy.transform.ToString(includeFields = true, includePackage = false)
class FooId7 {
    private final String value = '1234'
}
// FooId7(1234)

Nesting 6'6 example

Is it too long to have FQCN and the field name?

@groovy.transform.ToString(includeFields = true, includeNames = true)
class Foo6 {
    private final FooId6 id = new FooId6()
    private final FooName6 name = new FooName6()
}
// to_string.g.c.Foo6(id:to_string.g.c.FooId6(value:1234), name:to_string.g.c.FooName6(value:Jane Doe))

Nesting 7'7 example

For the Groovy class, this is personally the best

@groovy.transform.ToString(includeFields = true, includePackage = false)
class Foo7 {
    private final FooId7 id = new FooId7()
    private final FooName7 name = new FooName7()
}
// Foo7(FooId7(1234), FooName7(Jane Doe))

Enum Exactly the same as Java for ʻEnum`

1 No annotation

ʻEnum` is fine with this

public enum FooStatus1 {
    APPLYING, NOT_APPLYING
}
// NOT_APPLYING

2 Just attach

It's nice to have the class name, but it doesn't include the essential part

@lombok.ToString
public enum FooStatus2 {
    APPLYING, NOT_APPLYING
}
// FooStatus2()

Summary

Correspondence table

Organize only the parts that are likely to correspond

item Java Groovy Remarks
Field name includeFieldNames()
default true
includeNames()
default false
The opposite
Includes some fields of()
default {}
includes()
default {}
I have no plans to specify this item
The example is omitted
Excluding some fields exclude()
default {}
excludes()
default {}
Same as above
Behavior at the time of inheritance callSuper()
default false
includeSuper()
default false
Since it does not inherit, the example is omitted.
FQCN Does not appear includePackage()
default true
In a sense the opposite

There is still an item in @ groovy.transform.ToString, but there is no corresponding item in @ lombok.ToString and I am not interested in it now, so I will omit it.

Java repost

Class is the same specification method as Foo, ʻEnum` is unified without annotation

@lombok.ToString(includeFieldNames = false)
public class Foo {
    private final FooId id = new FooId();
    private final FooName name = new FooName();
    private final FooStatus fooStatus = FooStatus.NOT_APPLYING;
    private final BarStatus barStatus = BarStatus.NOT_APPLYING;
}
// Foo(FooId(1234), FooName(John Doe), NOT_APPLYING, NOT_APPLYING)

I think that only the most refreshing and important points are displayed

Groovy reposted

Similarly, Class has the same specification method as Foo, and ʻEnum` is unified without annotation.

@groovy.transform.ToString(includeFields = true, includePackage = false)
class Foo {
    private final FooId id = new FooId()
    private final FooName name = new FooName()
    private final FooStatus fooStatus = FooStatus.NOT_APPLYING
    private final BarStatus barStatus = BarStatus.NOT_APPLYING
}

// Foo(FooId(1234), FooName(Jane Doe), NOT_APPLYING, NOT_APPLYING)

It was exactly the same!

bonus

Use @ lombok.ToString for Groovy Class

I usually make a class with Java, but when I make a class with Groovy and copy only the annotation part from java, it gets mixed.

It's meaningless from the conclusion

@lombok.ToString
class FooId1 {
    private final String value = '1234'
}

// to_string.r.g.FooId1@29ca901e

Use @ groovy.transform.ToString for Java Class

I don't think the opposite can happen, but I tried it

After all it is meaningless from the conclusion

@groovy.transform.ToString
public class FooId1 {
    private final String value = "1234";
}

// to_string.r.j.FooId1@27c170f0

How to specify an array of Java annotations

Is this more about annotations in general than @ToString?

For Java, specify{x, y, ...} I knew this

@lombok.ToString(includeFieldNames = false, of = {"spam", "egg"})
public class Python {
    private final String spam = "spam";
    private final String ham = "ham";
    private final String egg = "egg";
}
// Python(spam, egg)

How to specify an array of Groovy annotations

I didn't understand this at first, and it was very confusing due to the behavior of the access modifier of groovy.transform.ToString.

For Groovy, specify[x, y, ...]

@groovy.transform.ToString(includePackage = false, includes = ['spam', 'egg'])
class Python {
    private final String spam = 'spam'
    private final String ham = 'ham'
    private final String egg = 'egg'
}
// Python()

...!? I haven't been warned of anything, but it feels like it's no good, but what's this!

I was really into it, but when I think about it now, there is no ʻinclude Fields = true`. I've put together just this so I can understand it right away I'm a little funny w

Doesn't it make sense to specify ʻincludes without setting ʻinclude Fields? !! Confusing! !! !!

By the way, the mysterious notation such as ʻincludes ='spam, egg'seems to be an ant. Is this the rule of@ groovy.transform.ToString? Groovy` rules?

Groovy private field

In fact, private in Groovy is almost meaningless So even if you remove private and just add @ groovy.transform.ToString, it's actually okay.

Let's move away from @ groovy.transform.ToString for a moment and organize the behavior of private

class Bar {
    String v1 = 'v1'
    public String v2 = 'v2'
    private String v3 = 'v3'
    static String v4 = 'v4'
    static public String v5 = 'v5'
    static private String v6 = 'v6'
}

Write a class like this

スクリーンショット 2017-07-01 21.25.57.png

If you write like this, you can access it

However, if it is ʻIntelliJ, it will not be complemented and a warning will be issued even if you force it, so I want to write it as private` for the time being

スクリーンショット 2017-07-01 21.24.23.png スクリーンショット 2017-07-01 21.25.21.png

v3 and v6 do not appear as completion candidates, and even if you write, v3 and v6 are a little yellow! Thank you ʻIntelliJ`!

Reference

Actually dissatisfied with the result of Enum

As I reposted in the summary, it becomes a little difficult to understand if ʻEnum`s with the same element name are lined up.

// Foo(FooId(1234), FooName(John Doe), NOT_APPLYING, NOT_APPLYING)

If ʻAPPLYING, NOT_APPLYING, APPLYING` are lined up, you may get "Hmm? What is the second one?"

So the ideal is to look like the one below, but it didn't work out

// Foo(FooId(1234), FooName(John Doe), FooStatus.NOT_APPLYING, BarStatus.NOT_APPLYING)

I can do it if I write like this I don't want to handwrite

public enum BazStatus {
    APPLYING, NOT_APPLYING;

    @Override
    public String toString() {
        return getClass().getSimpleName() + "." + name();
    }
}
// BazStatus.NOT_APPLYING

I have a lot of small complaints other than @ToString, so I want to make annotations, but how do I make annotations that generate such code? Depending on the content, you will also need the ʻInelli J` plugin, and it's still difficult for me to use it as a team at work.

MetaClass is mixed

By the way, I forgot because I didn't see it in this summary, but when I dealt with it during work, something like that was mixed in the result of @ groovy.transform.ToString. However, it was a hindrance because it was just a hash with FQCN and it was uselessly long and I was not interested in the contents because I did not set it myself.

So I wonder if I did a survey of ʻincludes I was confused because I checked while combining access modifiers, ʻincludeFields, ʻincludes`, and array specifications at once.

The end

Recommended Posts

Summary of ToString behavior with Java and Groovy annotations
Summary of Java Math.random and import (Calendar)
[Java] Personal summary of classes and methods (basic)
Summary of Java support 2018
Check the behavior of Java Intrinsic Locks with bpftrace
behavior of didSet and willSet
Summary of FileInputStream and BufferedInputStream
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
Advantages and disadvantages of Java
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
I didn't understand the behavior of Java Scanner and .nextLine ().
Window aggregation of sensor data with Apache Flink and Java 8
Java beginners briefly summarized the behavior of Array and ArrayList
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[For beginners] Summary of java constructor
Install Java and Tomcat with Ansible
Java release date and EOL summary
About fastqc of Biocontainers and Java
Summary of [Java silver study] package
Use JDBC with Java and Scala.
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Output PDF and TIFF with Java 8
[Java] Judgment of identity and equivalence
Summary of object-oriented programming using Java
Encrypt with Java and decrypt with C #
Summary of problems and countermeasures when operating IE with WebDriver of Selenium2
[Code Pipeline x Elastic Beanstalk] Summary of errors and countermeasures for CI / CD Java applications to Elastic Beanstalk with Code Pipeline
I want to display images with REST Controller of Java and Spring!
A brief summary of DI and DI containers
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Monitor Java applications with jolokia and hawtio
After 3 months of Java and Spring training
Link Java and C ++ code with SWIG
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
Let's try WebSocket with Java and javascript!
[Java] Personal summary of conditional statements (basic)
[Java] Reading and writing files with OpenCSV
[Java] Inheritance and structure of HttpServlet class
Summary of hashes and symbols in Ruby
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
[Java] [Spring] Test the behavior of the logger
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
Environment construction summary with rvm and postgresql
Summary of good points and precautions when converting Java Android application to Kotlin
Summary of mutual conversion between Groovy's Default Groovy Methods and Java's Stream API
Effective Java 3rd Edition Chapter 6 enums and annotations