mardi 21 septembre 2021

How to programmatically get the name of a static method in Java?

I am developing a Java program that persists a Java class and static method name to JSON. I want to write this code to be as safe as possible, so that if the class/method changes (e.g., is moved or renamed), then the code will fail to compile. Likewise, if I use refactoring tools to change the class or method name, I would like these tools to be able to automatically update the name of the referenced class/method in the serializer. In order to achieve this, I have been using reflection to get the fully-qualified class name, as follows:

MyClass.class.getName()

But I have not been able to find a similar approach to get the referenced static method. The following code seems to almost work, in that my IDE suggested it, but then throws a syntax error:

MyClass::myStaticMethod.getStaticMethodName()

Is there a way to programmatically get the name of a referenced static method as a string in Java?

Thank you.





Aucun commentaire:

Enregistrer un commentaire