Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of @ Data in Lombok

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the use of @ Data in Lombok". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the use of @ Data in Lombok"?

Lombok

Let's start with a brief introduction to Lombok. The official introduction is as follows:

Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.

Lombok can make Java code concise and fast by adding some "handlers".

Lombok provides a series of annotations to help us simplify the code, such as:

The annotation name function @ Setter automatically adds all attribute-related set methods @ Getter in the class automatically adds all attribute-related get methods @ Builder in the class so that the class can generate a class constructor through the builder (builder mode) construction object @ RequiredArgsConstructor. No parameter construction @ ToString is prohibited from overriding the toString () method @ EqualsAndHashCode of this class. The equals () and hashCode () methods @ Data of this class are equivalent to @ Setter, @ Getter, @ RequiredArgsConstructor, @ ToString, @ EqualsAndHashCode above.

It seems that these annotations are normal and have some optimizations to our code, so why is there a hole in the @ Data annotation?

Internal implementation of @ Data annotations

From the table above, we can see that @ Data contains the function of @ EqualsAndHashCode, so how on earth does it override the equals () and hashCode () methods?

We define a class TestA:

@ Datapublic class TestA {String oldName;}

We decompiled the compiled class file:

Public class TestA {String oldName; public TestA () {} public String getOldName () {return this.oldName;} public void setOldName (String oldName) {this.oldName = oldName;} public boolean equals (Object o) {/ / determine whether it is the same object if (o = = this) {return true } / / determine whether it is the same class else if (! (o instanceof TestA)) {return false;} else {TestA other = (TestA) o; if (! other.canEqual (this)) {return false } else {/ / compare the attributes in the class (note that only the attributes in the current class are compared) Object this$oldName = this.getOldName (); Object other$oldName = other.getOldName (); if (this$oldName = = null) {if (other$oldName! = null) {return false }} else if (! this$oldName.equals (other$oldName)) {return false;} return true;} protected boolean canEqual (Object other) {return other instanceof TestA;} public int hashCode () {int PRIME = true; int result = 1 Object $oldName= this.getOldName (); int result = result * 59 ($oldName= = null? 43: $oldName.hashCode ()); return result;} public String toString () {return "TestA (oldName=" this.getOldName () ");}}

For its equals () method, when it makes a property comparison, it only compares the properties in the current class. If you don't believe me, let's create another class, TestB, which is a subclass of TestA:

@ Datapublic class TestB extends TestA {private String name; private int age;}

We decompiled the compiled class file:

Public class TestB extends TestA {private String name; private int age; public TestB () {} public String getName () {return this.name;} public int getAge () {return this.age;} public void setName (String name) {this.name = name;} public void setAge (int age) {this.age = age } public boolean equals (Object o) {if (o = = this) {return true;} else if (! (o instanceof TestB)) {return false;} else {TestB other = (TestB) o; if (! other.canEqual (this)) {return false } else {/ / Note here, it really only compares the properties in the current class, and does not compare the attributes in the parent class Object this$name = this.getName (); Object other$name = other.getName () If (this$name = = null) {if (other$name = = null) {return this.getAge () = = other.getAge ();}} else if (this$name.equals (other$name)) {return this.getAge () = = other.getAge () } return false;}} protected boolean canEqual (Object other) {return other instanceof TestB;} public int hashCode () {int PRIME = true; int result = 1; Object $name = this.getName (); int result = result * 59 ($name = = null? 43: $name.hashCode ()) Result = result * 59 this.getAge (); return result;} public String toString () {return "TestB (name=" this.getName () ", age=" this.getAge () ");}}

According to the understanding of the code, if two subclass objects have the same properties in the subclass and different properties in the parent class, when using the equals () method, the two objects will still be considered the same, so test it:

Public static void main (String [] args) {TestB T1 = new TestB (); TestB T2 = new TestB (); t1.setOldName ("123"); t2.setOldName ("12345"); String name = "1"; t1.name = name; t2.name = name; int age = 1; t1.age = age; t2.age = age System.out.println (t1.equals (T2)); System.out.println (t2.equals (T1)); System.out.println (t1.hashCode ()); System.out.println (t2.hashCode ()); System.out.println (T1 = = T2); System.out.println (Objects.equals (T1, T2));}

The result is:

True

True

6373

6373

False

True

Problem summary

For a class whose parent class is Object and annotated with @ EqualsAndHashCode (callSuper = true), the equals () method generated by Lombok returns true only if two objects are the same object, otherwise it is always false, regardless of whether their properties are the same or not.

This behavior is not as expected most of the time, and equals () loses its meaning. Even if we expect equals () to work this way, the rest of the attribute comparison code is cumbersome and greatly reduces branch coverage of the code.

Solution method

If you use @ Data, you don't have an inheritance relationship, similar to Kotlin.

Override equals () yourself, and Lombok does not generate methods that are explicitly overridden.

Explicitly use @ EqualsAndHashCode (callSuper = true), and the Lombok will prevail as explicitly specified.

The @ Data trampling record of Lombok

The interview asks you the function of @ Data annotation, and the answer is to generate get/set/toString.

Is that true?

In fact, the @ Data annotation serves the purpose of

Get/set

Parameterless constructor

ToString

Hashcode

Equals

@ Data automatically generates hashcode and equals methods, which most people forget

Prove

Idea uses alt+6 to view the specific properties and methods of a class

At this point, I believe you have a deeper understanding of "what is the use of @ Data in Lombok". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report