code-generationjavapoetparameter-list

Build method with open parameter list with javapoet


is it possible with javapoet to create a method with an open parameter list? To create a method with a String[] parameter is no problem:

curEnumBuilder.addMethod(MethodSpec.methodBuilder("myMethod")
                .addParameter(String[].class, "params", Modifier.FINAL)
                .addModifiers(Modifier.PUBLIC)
                .returns(String.class)
                .build());

but I want to create:

public String myMethod(final String... params)

Solution

  • Adding "varargs(true)" to the method builder generates what I want