gpt4 book ai didi

java - 内部静态类的 Lombok 注释

转载 作者:行者123 更新时间:2023-12-02 00:12:37 25 4
gpt4 key购买 nike

在开始之前,在寻找这个问题的答案时,我查看了以下问题,但它没有回答我的问题:* 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'
}

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

最佳答案

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

请注意,您的问题与嵌套(*)类无关,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

关于java - 内部静态类的 Lombok 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com