1

In some expermients I'd like to write something like this:

query(Person.class, eq(Person::lastName, "Hawking"));

where eq method creates database query part lastName = 'Hawking' and query as a result return String "SELECT * FROM person WHERE lastName = 'Hawking'". This way I'd be able to write type safe queries with short form and without code generation.

Problem is that eq as first parameter takes Supplier<E>.

Is it possible to find out via reflection API method name from passed method reference?

Even hackish way allowed! :)

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
tunguski
  • 763
  • 7
  • 12
  • 3
    A method reference is just compile time syntax. At runtime, it's exactly the same as passing a `Supplier` argument. – Sotirios Delimanolis Feb 15 '15 at 16:06
  • 1
    If you [call for a hackish way…](http://stackoverflow.com/a/21879031/2711488)   but do me your task sounds like something that could end up in production code some day so I don’t recommend it. And [beware…](http://stackoverflow.com/a/26656912/2711488) – Holger Feb 16 '15 at 09:05
  • Ok, the answer is **no**. But in this special case I've figured out how to resolve exact method in **very hackish** way. I invoke passed method reference on mock (Mockito) and intercept invocation. Now I'm able to resolve exact name of passed method. [github.com/tunguski/matsuo-core](https://github.com/tunguski/matsuo-core/blob/73b08bd1b8f2f05fbe52ed4eed199c58b89ca919/matsuo-core/src/main/java/pl/matsuo/core/service/login/LoginService.java#L96). – tunguski Feb 16 '15 at 21:09

0 Answers0