Please add support for generics on methods

#2 · closed · 5 comments

View on GitHub ↗

andob

Hello. Thanks for writing this library. It's exactly what I needed. This is [my specific use case](https://www.reddit.com/r/javahelp/comments/ghj2xq/any_annotation_processor_library_that_can/). Some of the methods I have use generic types, for instance, this method displays a list with items of type T to the user. ``` @GenerateMethodInvocationBuilder(style = BuilderStyle.SUPPLY_INSTANCE_WITH_ON_METHOD) class ShowDialogImpl { <T> void withList(@NonNull Context context, @Default("") String title, @Default("true") boolean cancellable, @Default("(model) -> {}") Consumer<T> onOkButtonClicked, @Default("new java.util.ArrayList<>()") List<T> listItems) { //implementation } } ``` When I hit build, I got the following error: ``` ShowDialogImplWithListBuilder.java:16: error: cannot find symbol private Consumer<T> onOkButtonClicked; ^ symbol: class T location: class ShowDialogImplWithListBuilder ``` Because the generated code looks like this: ``` public final class ShowDialogImplWithListBuilder { private Consumer<T> onOkButtonClicked; ``` The ``ShowDialogImplWithListBuilder`` class should take <T> generic argument. Of course, I can get rid of <T> and use the Object type, but then I have to use type casts, it's ugly.. I also have a method with two generic types ``` public <T, Q> void withTreeList(Context context, String title, List<T> values, Function<T, List<Q>> subitemsListGetter, BiConsumer<T, Q> onClick) ``` Example usage (pseudocode): ``` title: "Please choose subcategory" values: categories, subitemsListGetter: category -> category.subcategories, onClick: (category, subcategory) -> {} ``` So the generated class should be ``ShowDialogImplWithTreeList<T, Q>``.

Comments

tomasbjerre

Sounds like a great feature. Pull requests are welcome!

andob

done. I've tested via unit tests. I've added 4 more test cases: ``` public <T> void methodWithGenericType(T argument) {} public <T, Q> void methodWithTwoGenericTypes(T argument1, Q argument2) {} public <T extends Point2D> void methodWithBoundedGenericType(T argument) {} public <T extends Serializable&Comparable> void methodWithDoublyBoundedGenericType(T argument) {} ```

tomasbjerre

Thanks! Looks great. I'm releasing it as 1.4 now.

andob

Cool. Thanks!

andob

Implemented and tested in my app. It works! :)