Flapi icon indicating copy to clipboard operation
Flapi copied to clipboard

Support Bounded Generics in Methods

Open spraguep opened this issue 12 years ago • 3 comments

Trying to add a method that uses generics:

addMethod("withApplicationSettings(Class<? extends com.a.b.c.ApplicationSettings> settingsClass)")

Causes this exception:

Caused by: unquietcode.tools.flapi.MethodParser$ParseException: Expected to find character in {)} but was '?' (method signature is [ 'withApplicationSettings(Class<? extends com.paypal.creditpd.rappstack.server.flapi.application.RSApplicationSettings> settings)' ]).
    at unquietcode.tools.flapi.MethodParser.throwUnexpectedCharException(MethodParser.java:273)
    at unquietcode.tools.flapi.MethodParser.match(MethodParser.java:214)
    at unquietcode.tools.flapi.MethodParser.<init>(MethodParser.java:120)
    at unquietcode.tools.flapi.MethodParser.<init>(MethodParser.java:42)
    at unquietcode.tools.flapi.DescriptorPreValidator._checkForInvalidMethodSignatures(DescriptorPreValidator.java:119)

spraguep avatar Jul 29 '13 16:07 spraguep

Version 0.4 added support for generic types, but not wildcards. Adding the wildcard type '?' is likely straightforward, but supporting the full bounds of 'x super y' or 'x extends y' is more difficult. These require changes to JCodeModel, because wildcard support there is a bit spotty. This is definitely worth looking into for the next release though.

Here's a little gem from the CodeModel code: :-)

/**
 * Represents a wildcard type like "? extends Foo".
 *
 * Our modeling of types are starting to look really ugly.
 * ideally it should have been done somewhat like APT,
 * but it's too late now.
 *
 * @author Kohsuke Kawaguchi
 */
final class JTypeWildcard extends JClass {
   ...
}

UnquietCode avatar Jul 30 '13 01:07 UnquietCode

Related to the required changes in JCodeModel: https://github.com/UnquietCode/JCodeModel/issues/4

UnquietCode avatar Aug 18 '13 04:08 UnquietCode

#214 will add support for wildcards, and this issue can remain open for a future enhancement to implement bounded generics, which is much more difficult.

UnquietCode avatar Jan 26 '15 21:01 UnquietCode