Skip to content

Generate list-of-interface getters on Java interfaces - #953

Open
ryanflegel wants to merge 1 commit into
Netflix:masterfrom
ryanflegel:interface-list-getters
Open

ryanflegel wants to merge 1 commit into
Netflix:masterfrom
ryanflegel:interface-list-getters

Conversation

@ryanflegel

@ryanflegel ryanflegel commented Aug 27, 2026

Copy link
Copy Markdown

Generate Java getter methods by default for interface fields that are list of interfaces. This is an enhancement to #882, which added getters by default but excluded lists.

I believe lists were probably left out of #882 due to lack of return type compatibility without using wildcard types. i.e., List<ConcreteType> does not override List<Interface>, but it does override List<? extends Interface>.

This PR basically implements the latter and re-uses the same wildcard logic that was created for the generateInterfaces flag. It also introduces wildcards for interface List setters on interfaces, which is consistent with how generateInterfaces works (and because it shares the logic).

if (useWildcardType) {
if (typeName is ClassName) {
if (document.definitions
if (isFieldTypeAnInterface(node.type)) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use wildcards for List of interface.

) {
canUseWildcardType = true
}
} else if (typeName is ParameterizedTypeName && typeName.rawType().canonicalName() == "java.util.List") {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also use wildcard for List of List (of List...).

This ensure List<List<Interface>> still works and becomes List<? extends List<? extends Interface>>.

It will also unnecessarily do it for concrete types, which should be harmless. e.g., List<List<ConcreteType>> becomes List<? extends List<ConcreteType>>.

| List<? extends Pet> getFriends();
|
| void setFriends(List<Pet> friends);
| void setFriends(List<? extends Pet> friends);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This demonstrates how interface-setters also have wildcards. This is more flexible for the Java compiler, but may theoretically lead to runtime errors. e.g., if a concrete type only supported a specific subtype of Pet.

Note that this is consistent with the existing generateInterfaces behaviour, but I wanted to point it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant