I am looking for a convenient workaround to get a Method object from a method. Idea:
Method fooMethod = getMethod( new MyObject().foo() ) // returns method "foo" in MyObject
The obvious way is to use the method name as a string:
Method fooMethod = MyObject.class.getMethod("foo")
but I want to avoid this, because if I rename foo (), this code will stop working or I will rename the string in all places where it is used.
A use case is that I want to use something similar to ProperyChangeListeners , however they rely on the method name as a string. I would like to use the actual method (safely) and not rely on strings.
What can I use to get the method in a safe way of renaming?
UPDATE : I would like to find a pure Java solution that does not rely on IDE functions.
java reflection
Andrejs Mar 25 2018-12-12T00: 00Z
source share