我正在尝试从here复制一个简单的对象分解示例。 。我已将以下依赖项添加到我的项目中:
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-match</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-match-processor</artifactId>
<version>${vavr.version}</version>
</dependency>
...其中 vavr.version
为 0.10.3
并从上述源复制粘贴了示例:
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.match.annotation.Patterns;
import io.vavr.match.annotation.Unapply;
import lombok.AllArgsConstructor;
import lombok.Getter;
import static io.vavr.API.$;
import static io.vavr.API.Match;
import static io.vavr.API.Match.*;
public class Example {
@Getter
@AllArgsConstructor
public static class Employee {
private String name;
private String id;
}
@Patterns
public static class Demo {
@Unapply
static Tuple2<String, String> Employee(Employee Employee) {
return Tuple.of(Employee.getName(), Employee.getId());
}
}
public static void main(String[] args) {
Employee person = new Employee("Carl", "89696D8");
String result = Match(person).of(
Case(Demo.Employee($("Carl"), $()), (name, id) -> ""),
Case($(), () -> "notfound")
);
}
}
然而,第一个 Case
产生编译错误 Expected 1 argument but find 2
这表明注释处理不起作用,并且相应的模式也没有起作用已生成。如果我错了请纠正我。
我正在使用 Intellij 2020.1 并在其中启用了注释处理
最佳答案
原因是您在这一行中使用了无效的生成类名称:
Case(Demo.Employee($("Carl"), $()), (name, id) -> "")
应替换为:
Case(Example_DemoPatterns.$Employee($("Carl"), $()), (name, id) -> name + " " + id)
自 Demo
是一个静态嵌套类,它需要以 Example_
为前缀因为这就是生成器的工作原理,所以需要添加后缀 Patterns
.
Here是一个完整的、可构建的示例,您可以克隆并尝试它。它包含 Maven 和 gradle 的示例。要验证它是如何工作的,请将提到的行更改为例如:
Case(Example_DemoPatternsWhatever.$Employee($("Carl"), $()), (name, id) -> name + " " + id)
然后运行:
mvn exec:java
它失败了(没有生成这样的类)但是当你运行 tree target/
时你会得到类似的东西:
target
├── classes
├── generated-sources
│ └── annotations
│ └── Example_DemoPatterns.java
└── maven-status
└── maven-compiler-plugin
└── compile
└── default-compile
├── createdFiles.lst
└── inputFiles.lst
这意味着该类确实已生成。这是您需要使用的类。
关于java - Vavr 注释处理工具未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65254661/