Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the 'Any Type' questionmark in JavaPoet?

I'm generating code with JavaPoet.

Somewhere in the generated code I want to add a method which has the following argument.

...
    public B someMethod(final AbstractObjectBuilder<Persoon,?> builder) {
       ...
    }
...

So my JavaPoet code should look something like this

    //This does not compile, since I don't know what to put as last argument (questionmark)
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(AbstractObjectBuilder.class, propertyType,?);

ParameterSpec parameterSpec = ParameterSpec.builder(parameterizedTypeName, name+"Builder", Modifier.FINAL).build();

MethodSpec modMethod = MethodSpec.methodBuilder(name)
                        .addModifiers(Modifier.PUBLIC)
                        .addParameter(parameterSpec)
                        .returns(TypeVariableName.get("B"));
...
like image 226
Pieter Degraeuwe Avatar asked Mar 18 '15 08:03

Pieter Degraeuwe


1 Answers

Foo<?> is a short form of Foo<? extends Object>. Use WildcardTypeName for this.

like image 190
Jesse Wilson Avatar answered Nov 08 '22 21:11

Jesse Wilson