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

标签 java javapoet

是否可以使用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/

相关文章:

java - KeyListener 无法与 JFrame Canvas 一起使用

java - Android:SeekBar 小部件中的自定义 Drawable 正在绘制别名图像

java - C 中结构之外的变量名

JavaPoet + Android Studio "addModifiers(Modifier) cannot be applied to Modifier"

Javapoet/JavaWriter 附加到现有类

java - 设置/更改文件的 ctime 或 "Change time"属性

java - Java字节码有 "indirect jump"吗?

Javapoet:引用其他生成的类

android-studio - Android Studio 使用 JavaPoet 库显示非常具体的错误错误

JavaPoet - 具有嵌套的 ParameterizedTypeName 的 ParameterizedTypeName