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.
Who wants to see this
public class FooId1 {
private final String value = "1234";
}
// to_string.j.c.FooId1@27c170f0
It's easy, and depending on the situation, this is enough
@lombok.ToString
public class FooId2 {
private final String value = "1234";
}
// FooId2(value=1234))
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)
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)
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)
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))
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`
ʻEnum` is fine with this
public enum FooStatus1 {
APPLYING, NOT_APPLYING
}
// NOT_APPLYING
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.
Similarly, there is no demand for this
class FooId1 {
private final String value = '1234'
}
// to_string.g.c.FooId1@3159c4b8
It's not priced! ?? Virtually nothing has changed, right? ??
@groovy.transform.ToString
class FooId2 {
private final String value = '1234'
}
// to_string.g.c.FooId2()
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()
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)
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)
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)
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)
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))
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`
ʻEnum` is fine with this
public enum FooStatus1 {
APPLYING, NOT_APPLYING
}
// NOT_APPLYING
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()
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.
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
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!
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
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
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)
[" spam "," ham "]
{" Spam "}
can be written as " spam "
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?
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
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
v3
and v6
do not appear as completion candidates, and even if you write, v3
and v6
are a little yellow!
Thank you ʻIntelliJ`!
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.
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