Search code examples
javaeclipsejava-8functional-interface

Generate Method with correct signature as expected from Functional interface in Eclipse


I am using Java 8 functional interfaces a lot.

In particular, I have some methods that register functions with certain signatures. Its signature is as follows:

<A extends IA, D extends IB, B extends IB> void registerRConv(Class<A> tA, Class<D> tD, Class<B> tB,
        BiFunction<A, D, B> ad2b);

Sidenote: in case you're wondering, its a converter registry that registers, which types are converted to which, and a function which does the actual convertig

I use this method quite often, and everytime i find myself typing

registry.registerRConv(Common.Choice.class, Choice.class, Object.class, Converter::getChoice)

Where the given classes all exist, but the static method - Converter::getChoice in the example - does not yet exist.

Now, if i do this in IntelliJ IDE's, when i press alt+Enter I get an option to automatically create that method in the current class - with the expected signature.

Can I make Eclipse do this? and if so, how?


Solution

  • This is the kind of change that is provided by "quick fixes" invoked by pressing Ctrl+1.

    Unfortunately, when the resolve error is reported against a method reference expression, the existing quick fix is not offered.

    I filed https://bugs.eclipse.org/516504 to address this.

    In the meanwhile, the workaround would be:

    • Change the method reference to a lambda expression
    • Invoke the quick fix to create the missing method
    • Invoke the quick assist "Convert to method reference" (Ctrl+1 also here)