7

I'm trying to use method references to capture method invocations and am hitting some limitations. This works fine:

<T> void capture(Function<T, ?> in) {
}

private interface Foo {
  String getBar();
} 

capture(Foo::getBar);

But if I change the signature of Foo.setBar to something like this:

private interface Foo {
  void setBar(String bar);
}

capture(Foo::setBar);

I get an error:

Cannot make a static reference to the non-static method setBar(String) from the type MyTest.Foo

It's not clear to me what the restriction is. Ideally I'd like to use method references to capture invocations on standard setter. Is there any way to do this?

7
  • Is this even Java? Or is that syntax new in Java 8? May 21, 2014 at 21:33
  • Could you please post an error? May 21, 2014 at 21:33
  • @AnubianNoob This is indeed Java8s method references etc... May 21, 2014 at 21:33
  • 1
    Erm, what is "static" about that? I don't see a static modifier?
    – meriton
    May 21, 2014 at 21:36
  • 3
    @meriton: The method reference is effectively static, because it's like a lambda expression which takes a Foo and calls a method on that reference.
    – Jon Skeet
    May 21, 2014 at 21:37

3 Answers 3

13

There are two problems here:

  • You're using Function, which has to return something. setBar doesn't return anything.
  • Function only takes a single input, but you've got two inputs: the Foo you'd call setBar on, and the String argument you'd pass into setBar.

If you change to use BiConsumer instead (which has a void return type and two inputs) it works fine:

static <T, U> void capture(BiConsumer<T, U> in) {
}

You can overload your capture method to have both signatures:

static <T, U> void capture(BiConsumer<T, U> in) { }
static <T> void capture(Function<T, ?> in) { }

and then use both method references:

capture(Foo::setBar);
capture(Foo::getBar);
3
  • Amazingly quick response. Thanks! Once SO allows me I'll accept your answer.
    – Josh Stone
    May 21, 2014 at 21:40
  • 1
    @JoshStone: Glad it makes sense. I haven't done much with the Java 8 stuff or written about it, so it'll take a while to hone how to explain things. Let me know if there's anything you'd like me to expand on.
    – Jon Skeet
    May 21, 2014 at 21:41
  • 1
    @JoshStone You'll want to keep in mind though that offering all possible overloads (especially once you hit similar arity) will get you into trouble due to erased generics on runtime. Currently I believe there are even some issues with similar arity when void is involved, I think a fix for that particular issue is in the works though.
    – skiwi
    May 22, 2014 at 8:30
8

Foo::getBar corresponds to a function that takes a Foo (the target object) and returns a String. The interface Function<Foo, String> can be used to represent such a function.

On the other hand, Foo::setBar corresponds to a function that takes two arguments, a Foo (the target object) and a String (the first parameter). The matching interface is BiConsumer<Foo, String>. That means you need an overload for BiConsumer:

<T, U> void capture(BiConsumer<T, U> setter) {
    // ...
}
3

Expose syntax sugar method reference, you should see, that

Foo::getBar

is equal to

(Foo)foo -> foo.getBar()

which is Function <Foo, String>

But

Foo::setBar

is in this context the function of two variables (foo and some String str), so it is not the function of one variable (Function)

For the more convenient answer you should see, where the method references are allowed:

  1. Reference to a static method (not this case at all)
  2. Reference to an instance method of a particular object (not this case at all)
  3. Reference to an instance method of an arbitrary object of a particular type (this case)

    In the instruction above there is an example, which is almost the same as in your case. And it is said, that the equivalent lambda expression would be taking two parameters (in this case Foo and String), which is not Function

  4. Reference to a constructor (not this case at all)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.