Skip to content

Infer type from collections and maps when type annotation not specified #196

@remkop

Description

@remkop

Example:

class Args {
    @Option(names = "-o")
    List<Integer> options;
    
    @Option(name = "-P")
    Map<TimeUnit, Long> duration;

    @Parameters
    List<URL> urls;
}

None of these fields have the type attribute specified. Picocli can infer the type from the generic type:

Field optionsField = Test.class.getDeclaredField("options");
ParameterizedType optionListType = (ParameterizedType) optionsField.getGenericType();
Class<?> cls = (Class<?>) optionListType.getActualTypeArguments()[0];
System.out.println(cls); // class java.lang.Integer

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions