上下文是:
- IDE:intellij
- 使用 Lombok @Data @AllArgsConstructor 来注释我的模型类
编译正常,但运行以下情况失败。
/** This is the model class */ @Data @AllArgsConstructor class Message { public Long id; public String name; } /** This is a test case */ @Test public void testConstructRef() { BiFunction<Long, String, Message> constructFunc = Message::new; Message msg = constructFunc.apply(1L, "this is a message"); assertEquals(1L, (long) msg.getId()); assertEquals("this is a message", msg.getName()); }
在 IDE 中,Message
构造函数和 getter/setter 都在那里。 编译正常,但运行失败。
所以我的问题是:
- 这是 Intellij 问题还是 lombok 问题还是 java 8 问题?
- 以前有人遇到过这种情况吗?如果我必须使用 lombok 如何修复它? (如果我手动编写构造函数/getter/setter,它可以成功运行。)
顺便说一句,运行错误是:
Error:(40, 63) java: incompatible types: invalid constructor reference
constructor Message in class java8.methodref.TestDemo.Message cannot be applied to given types
required: no arguments
found: java.lang.Long,java.lang.String
reason: actual and formal argument lists differ in length
Error:(43, 40) java: cannot find symbol
symbol: method getId()
location: variable msg of type java8.methodref.TestDemo.Message
Error:(44, 50) java: cannot find symbol
symbol: method getName()
location: variable msg of type java8.methodref.TestDemo.Message
最佳答案
与设置完美配合:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
"IntelliJ IDEA 2016.3 Build #IU-163.7743.44, built on November 17, 2016 JRE: 1.8.0_112-release-408-b2 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o "
关于intellij-idea - 使用 ClassName::new (java 8) 生成类实例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819378/