简体   繁体   English

如何使用 JavaPoet 构造具有自定义类型的枚举

[英]How to construct enum with custom type using JavaPoet

Is it possible to generate the following enum class using JavaPoet?是否可以使用 JavaPoet 生成以下枚举 class?

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;
  }
}

i can create everything but i have huge struggle with initialisation block within the ITEMs.我可以创建所有东西,但我在 ITEM 中的初始化块方面遇到了巨大的困难。

My current code is我目前的代码是

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);
}

i was able to create this我能够创造这个

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;
  }

by adjusting for cycle like so:通过像这样调整循环:

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());
}

This is partially ok but i do not know how to generate imports for those classes.这部分没问题,但我不知道如何为这些类生成导入。

I prefer first option我更喜欢第一个选项

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

but if it cannot be done can someone suggest how to generate imports for the second example with adjusted for cycle?但如果无法完成,有人可以建议如何为第二个示例生成导入并调整周期?

Thank you very much for any suggestion.非常感谢您的任何建议。

Ok found solution if anyone will be interested in 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM