gpt4 book ai didi

java - 如何使用 JavaPoet 构造自定义类型的枚举

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

是否可以使用JavaPoet生成以下枚举类?

public enum EnumName {

import com.sth.sth.SomeClass1;
import com.sth.sth.SomeClass2;

ITEM1(new CustomType<SomeClass1>("string1", "string 2", SomeClass1.class)),
ITEM2(new CustomType<SomeClass2>("string1", "string 2", SomeClass2.class));

EnumName(CustomType customType) {
this.customType = customType;
}

private final CustomType customType;

public CustomType getCustomType() {
return customType;
}
}

我可以创建一切,但我在 ITEM 内的初始化 block 上遇到了巨大的困难。

我当前的代码是

TypeSpec.Builder typeSpecBuilder = TypeSpec.enumBuilder("EnumName")
.addModifiers(PUBLIC)
.addField(TypeVariableName.get("CustomType"), "customType", PRIVATE, FINAL)
.addMethod(MethodSpec.constructorBuilder()
.addParameter(TypeVariableName.get("CustomType"), "customType")
.addStatement("this.$N = $N", "customType", "customType")
.build())
.addMethod(MethodSpec.methodBuilder("getCustomType")
.addModifiers(PUBLIC)
.addStatement("return customType")
.returns(TypeVariableName.get("CustomType"))
.build());
for (Model model : models) {
typeSpecBuilder.addEnumConstant(prepareName(model), HERE I DO NOT KNOW HOW TO BUILD TypeSpec that would create what i need);
}

我能够创建这个

public enum EnumName {

ITEM1("ITEM1"){
@Override
public CustomType getCustomType(){
return new CustomType<SomeClass1>("string1", "string 2", SomeClass1.class));
}
},
ITEM2("ITEM2"){
@Override
public CustomType getCustomType(){
return new CustomType<SomeClass2>("string1", "string 2", SomeClass2.class));
}
};

EnumName(customTypeName customTypeName) {
this.customTypeName = customTypeName;
}

private final String customTypeName;

public String getCustomTypeName() {
return customTypeName;
}

通过像这样调整周期:

for (Model model : models) {
typeSpecBuilder.addEnumConstant(prepareName(model), TypeSpec.anonymousClassBuilder("$S", prepareName(model))
.addMethod(MethodSpec.methodBuilder("getCustomType")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addStatement(getInitStatement(model))
.returns(TypeVariableName.get("CustomType"))
.build())
.build());
}

这部分没问题,但我不知道如何为这些类生成导入。

我更喜欢第一个选项

ITEM1(new CustomType<SomeClass1>("string1", "string 2", SomeClass1.class))

但如果无法完成,有人可以建议如何为第二个示例生成导入并调整周期?

非常感谢您的建议。

最佳答案

如果有人对 future 感兴趣,那就找到解决方案了。

String fullyQualifiedClassName = getClassName();
if (fullyQualifiedClassName == null) {
fullyQualifiedClassName = "Object"; //important if class was not found we need set to object -> otherwise there will not be imports for some reason
}
TypeName typeName = ClassName.bestGuess(fullyQualifiedClassName);
for (Model model : models) {
typeSpecBuilder.addEnumConstant(prepareName(model), TypeSpec.anonymousClassBuilder("new CustomType<$T>($S, $S, $T.class)", typeName, string1, string2, typeName));
}

关于java - 如何使用 JavaPoet 构造自定义类型的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58136538/

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