【问题标题】:Vavr annotation procession tool doesn't kick-inVavr 注释处理工具不启动
【发布时间】:2020-12-11 16:02:19
【问题描述】:

我正在尝试从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.version0.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 found 2,这表明注释处理不起作用并且尚未生成相应的模式。如果我错了,请纠正我。

我正在使用 Intellij 2020.1 并在其中启用了注释处理

【问题讨论】:

  • 您能否将示例项目与为您复制案例的依赖项共享。 0.10.3 依赖版本似乎存在一些问题。对我来说,只有 0.9.2 包含 Tuple 类。

标签: java maven intellij-idea vavr


【解决方案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

这意味着该类确实已生成。这是您需要使用的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多