0

Is it possible to get an instance of java.lang.reflect.Method by using the new method reference feature of Java 8?

That way I would have a compile time check and refactoring would be also easier. Also, I wouldn't need to catch the exceptions (which shouldn't been thrown after all).

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Jimmy T.
  • 4,033
  • 2
  • 22
  • 38

1 Answers1

1

Short answer: No.

You will get a lambda of that method, not a java.lang.reflect.Method. You do not know the name of the method. Just as you can not have a reference to a "property" of a java bean. You can have a reference to the getter or setter but that is also a lambda and you do not know the actual name. In any case you'd have to provide the name as a String and that can't be checked by the compiler. I also tried this but failed. It simply can't be done unless you write something that checks the javacode/bytecode. But there are tools that do that. Maybe the Criteria API could be used for that, but it depends on the requirements. http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html There you'd have a SingularAttribute or similar field on a "metamodel" and then the regular java compiler can check the (generic) type of it.

Claude Martin
  • 745
  • 6
  • 21