eclipse集成lombok注解不起作用


安装步骤:

  步骤一:lombok的下载地址为:https://projectlombok.org/download,jar包很小。这里也把依赖写出来:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.2</version>
        <scope>provided</scope>
    </dependency>

  步骤二:双击jar包,jar包内的安装器会自动运行寻找eclipse

  步骤三:选择需要安装的eclipse,然后点击安装(Install/Update),下一步会提示安装成功。

  这就完成了安装,是不是很简单,我们看一下,安装过程具体操作了什么:

  原来是做了两部分,第一,把jar包复制到了eclipse安装目录,第二在eclipse.ini文件中加入了一句话:-javaagent:D:\eclipse_4.4\eclipse\lombok.jar

  接下来就是测试一下了,但是博主在这遇到了问题,耗费了一点时间来解决。

测试步骤:

  重启eclipse,将jar包导入到工程,写测试类,在这遇到了错误:

  报错,这说明注解没起作用,这就纳闷了,我重新按步骤做了一遍,然后切换了lombok版本,这些方法都试了,结果还是不行,偶然注意到了下面这句:

  对,就是安装成功的提示,后面标红的没问题,这是具体jar工作的的一环,但是前面标红这句,我看了一下eclipse.ini文件,还真没有,我抱着试试原则,结果就是这个原因,注解起作用了。

  最后看一下源文件和编译后的文件。

Test.java:

package test; import lombok.Data; @Data public class Test { private String name; private String age; public static void main(String[] args) { Test se = new Test(); se.setName("zhangsan"); se.setAge("16"); System.out.println(se.getAge()); System.out.println(se.getName()); } }

Test.class:

package test; public class Test { private String name; private String age; public static void main(String[] args) { Test se = new Test(); se.setName("zhangsan"); se.setAge("16"); System.out.println(se.getAge()); System.out.println(se.getName()); } public String getName() { return this.name; } public String getAge() { return this.age; } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Test)) { return false; } else { Test other = (Test) o; if (!other.canEqual(this)) { return false; } else { String this$name = this.getName(); String other$name = other.getName(); if (this$name == null) { if (other$name != null) { return false; } } else if (!this$name.equals(other$name)) { return false; } String this$age = this.getAge(); String other$age = other.getAge(); if (this$age == null) { if (other$age != null) { return false; } } else if (!this$age.equals(other$age)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Test; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null ? 43 : $name.hashCode()); String $age = this.getAge(); result1 = result1 * 59 + ($age == null ? 43 : $age.hashCode()); return result1; } public String toString() { return "Test(name=" + this.getName() + ", age=" + this.getAge() + ")"; } }

 具体怎么使用,得看文档了:https://projectlombok.org/api/lombok/package-summary.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM