Search code examples
javacode-generationjavapoet

Generate field of an interface by JavaPoet


Is there any way to generate a field like this:

public static final TypedKey<String> UM_USR = new TypedKey<String>() {};

I didn't find any way to set the type of UM_USR to a generic interface.


Solution

  • You need to use a ParameterizedTypeName instead of a simple ClassName :

    ParameterizedTypeName paramTypedKey = ParameterizedTypeName.get(ClassName.get(TypedKey.class), ClassName.get(String.class));