【问题标题】:Lombok annotations for inner static classes内部静态类的 Lombok 注释
【发布时间】:2020-05-02 03:01:30
【问题描述】:

在开始之前,在寻找此问题的答案时,我查看了以下未回答我的问题: * Is it possible to add @Builder and @AllArgsConstructor in static inner classes [Lombok]?

在我的测试中,我创建了一个内部类来保存我的测试用例:

@AllArgsConstructor
@Getter
static class TestData {
    private final String testCase;
    private final String value1;
    private final String value2;
    private final int value3;
}

同样在这些测试中,我有一个包含所有这些测试用例的列表:

    private static final List<TestData> testData = Collections.unmodifiableList(Arrays.asList(
            new TestData("test case 1", "value1", "value2", 1),
            new TestData("test case 2", "value2", "value2", 2),
            new TestData("test case 3", "value2", "value1", 3),
            new TestData("test case 4", "value2", "value2", 4),
    ));

但是,当我编译代码时,出现以下错误:

error: constructor TestData in class TestData cannot be applied to given types;
            new TestData("test case 1", "value1", "value2", 1),
            ^
  required: no arguments
  found: String,String,String,int
  reason: actual and formal argument lists differ in length

编辑 由于它似乎对我的项目不起作用,我附上了我失败的完整示例代码:

public class Testing {

    private static final List<TestData> testData = Collections.unmodifiableList(Arrays.asList(
            new TestData("test case 1", "value1", "value2", 1),
            new TestData("test case 2", "value2", "value2", 2),
            new TestData("test case 3", "value2", "value1", 3),
            new TestData("test case 4", "value2", "value2", 4)
    ));

    @Test
    public void aTest() {
        for (final TestData data : testData) {
            System.out.println("***********************");
            System.out.println(data.getTestCase());
            System.out.println(data.getValue1());
            System.out.println(data.getValue2());
            System.out.println(data.getValue3());
        }
    }

    @AllArgsConstructor
    @Getter
    static class TestData {
        private final String testCase;
        private final String value1;
        private final String value2;
        private final int value3;
    }
}

这是我的 build.gradle 文件:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.10'

    annotationProcessor 'org.projectlombok:lombok:1.18.10'

    testCompile 'junit:junit:4.12'
    testCompile 'org.projectlombok:lombok:1.18.10'
}

EDIT2 您可以在此 GitHub 存储库中找到我在我的机器(以及我的一些大学)上运行的项目中的代码示例: https://github.com/yonatankarp/stackoverflow_lombok

【问题讨论】:

  • 这很奇怪。请放心,我经常使用带有嵌套类的 Lombok 并且那里没有问题。我想,你应该在 github 或类似的地方创建一个完整的项目(包括 gradlew),以便有人可以轻松克隆并自己尝试。
  • 感谢您的建议 - 添加了问题的链接
  • 在测试代码中使用Lombok注解似乎有问题。我不知道这是 gradle 特定的,还是链接到 IDE。
  • 就我而言,添加公共访问说明符使其可访问。似乎 lombok @data 注释仅适用于公共类。但是看看 Gradle 的答案,它似乎与 Maven 插件有关。

标签: java junit lombok


【解决方案1】:

我不知道您的设置有什么问题,但我可以重现并解决您的问题。

请注意,您的问题与嵌套(*)类无关,Lombok AFAIK 根本没有运行。

这是我的build.gradle

plugins {
    id 'java'
    id "io.freefair.lombok" version "4.0.1"
}

lombok {
        version = "1.18.10"
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

我删除了对lombok 的所有提及,并添加并配置了一个正确执行此操作的插件。

我不知道出了什么问题。


(*) TestData 是“嵌套的”,但不是“内部的”。不知道,为什么,但这就是 Oracle 所说的:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

【讨论】:

  • 很有趣,似乎可行 - 不知道为什么我当前的设置不支持它 - 但感谢您的帮助(以及关于内部类和嵌套类的说明)
【解决方案2】:

有一个 IntelliJ 配置,您可以在其中启用注释处理。

首选项 -> 编译器 -> 注释处理器 -> 启用注释处理

正如 Chetan Komakula 所说,您的代码也可以在我的 IntelliJ 上运行。

Obs:我认为您不需要静态字段,我认为您可以使用 @BeforeAll 或 @BeforeEach 方法来初始化您的 testData 数组。但这是一种改进。

希望对你有帮助。

【讨论】:

  • Enable annotation processing 已经被标记并且似乎适用于外部类 - 但是对于内部类,我遇到了问题。另外,我已经为 Lombok 安装了 IntelliJ 插件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多