JavaPoet - 具有嵌套的 ParameterizedTypeName 的 ParameterizedTypeName

JavaPoet - ParameterizedTypeName with nested ParameterizedTypeName

如何创建以下内容:

HashMap<String, Pair<Boolean, Object>> mHashMap = new HashMap();

我有以下内容:

ParameterizedTypeName subType = ParameterizedTypeName.get(Pair.class, Boolean.class, Object.class);
ParameterizedTypeName mainType = ParameterizedTypeName.get(HashMap.class, String.class, Pair.class);
 builder.addField(
            FieldSpec.builder(mainType, "mHashMap", Modifier.PRIVATE, Modifier.FINAL)
                    .initializer("new HashMap<>()")
                    .build()
    );

我现在如何将 subTypemainType 结合起来以获得正确的 mainType

我需要如下内容:

ParameterizedTypeName mainType = ParameterizedTypeName.get(HashMap.class, String.class, subType);

像这样的东西应该可以工作:

ParameterizedTypeName subType = ParameterizedTypeName.get(Pair.class, 
                                                          Boolean.class,
                                                          Object.class);
ClassName string = ClassName.get("java.lang", 
                                 "String");
ClassName hashMap = ClassName.get("java.util", 
                                  "HashMap");
ParameterizedTypeName mainType = ParameterizedTypeName.get(hashMap, 
                                                           string,
                                                           subType);

FieldSpec.builder(mainType, 
                  "mHashMap",
                  Modifier.PRIVATE,
                  Modifier.FINAL)
         .initializer("new HashMap<>()")
         .build();

上面的代码应该生成:

private final HashMap<String, Pair<Boolean, Object>> mHashMap = new HashMap<>();

希望对您有所帮助。