How to add the 'Any Type' questionmark in JavaPoet?
Asked Answered
U

1

11

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"));
...
Unfolded answered 18/3, 2015 at 8:44 Comment(1)
you can use <Persoon, ? extends <any_class_name or Object>>Inclement
O
13

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

Orella answered 18/3, 2015 at 11:37 Comment(2)
Could you please provide an example?Swifter
This: ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class) gives a Class<?>.Sext

© 2022 - 2024 — McMap. All rights reserved.