内部静态的 Lombok 注释 类

Lombok annotations for inner static classes

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

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

@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/Whosebug_lombok

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

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

正如 Chetan Komakula 所述,您的代码也适用于我的 IntelliJ。

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

希望对您有所帮助。

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

请注意,您的问题与嵌套 (*) 类 无关,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 是 "nested",但不是 "inner"。不知道,为什么,但这就是 Oracle 所说的:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html