一、eclipse中安装
1、直接下载lombok.jar包或者在pom.xml中导入依赖。
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
2、运行Lombok.jar: Java -jar D:\software\lombok.jar
D:\software\lombok.jar这是windows下lombok.jar所在的位置
3、确认完eclipse的安装路径后,点击install/update按钮,即可安装完成
4、安装完成之后,请确认eclipse安装路径下是否多了一个lombok.jar包,并且其
配置文件eclipse.ini中是否 添加了如下内容:
-javaagent:lombok.jar
-Xbootclasspath/a:lombok.jar
如果上面的答案均为true,那么恭喜你已经安装成功,否则将缺少的部分添加到相应的位置即可
5、重启eclipse或myeclipse
如果安装失败,则直接将maven中的jar包复制到eclipse安装目录中
1、路径如下:
2、在eclipse.in文件最后加入下面两行:
-Xbootclasspath/a:lombok.jar
-javaagent:lombok.jar
3、-javaagent:xxx.jar 的jar名称 需要与根目录下的jar名一致,
不一致,可能会出现eclipse无法启动的情况。重启eclipse,进行代码测试。
二、使用
1、编译代码后在标注了注解的类上会自动生成想要的方法。
a、原始文件:
package zengxibo.test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude="height")
public class Person {
private String name;
private Integer age;
private Integer height;
}
b、使用反编译工具反编译\target\test-classes\zengxibo\test文件中的Person.class如下:
package zengxibo.test;
public class Person
{
private String name;
private Integer age;
private Integer height;
public String getName()
{
return this.name;
}
public Integer getAge()
{
return this.age;
}
public Integer getHeight()
{
return this.height;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(Integer age)
{
this.age = age;
}
public void setHeight(Integer height)
{
this.height = height;
}
public boolean equals(Object o)
{
if (o == this) {
return true;
}
if (!(o instanceof Person)) {
return false;
}
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
}
Object this$name = getName();Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
return false;
}
Object this$age = getAge();Object other$age = other.getAge();
if (this$age == null ? other$age != null : !this$age.equals(other$age)) {
return false;
}
Object this$height = getHeight();Object other$height = other.getHeight();return this$height == null ? other$height == null : this$height.equals(other$height);
}
protected boolean canEqual(Object other)
{
return other instanceof Person;
}
public int hashCode()
{
int PRIME = 59;int result = 1;Object $name = getName();result = result * 59 + ($name == null ? 43 : $name.hashCode());Object $age = getAge();result = result * 59 + ($age == null ? 43 : $age.hashCode());Object $height = getHeight();result = result * 59 + ($height == null ? 43 : $height.hashCode());return result;
}
public Person(String name, Integer age, Integer height)
{
this.name = name;this.age = age;this.height = height;
}
public String toString()
{
return "Person(name=" + getName() + ", age=" + getAge() + ")";
}
public Person() {}
}
三、注解具体含义:
1、参考:
a、https://blog.youkuaiyun.com/v2sking/article/details/73431364