使用ClassName::new(java8)生成class实例失败
Use ClassName::new (java 8) to generate a class instance failed
上下文是:
- IDE: 智能
- 使用 Lombok @Data @AllArgsConstructor 注释我的模型 class
编译成功,但运行以下情况失败。
/** 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,如何修复它? (如果我手动写constructor/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 "
上下文是:
- IDE: 智能
- 使用 Lombok @Data @AllArgsConstructor 注释我的模型 class
编译成功,但运行以下情况失败。
/** 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,如何修复它? (如果我手动写constructor/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 "