mardi 7 avril 2015

How to access method properties at compile-time?

I would like to access the usual reflection properties of a specific method without relying on Strings.


Is there any way to get from the method-Identifier to its reflection properties, like parameter types?



void run() {
java.lang.reflect.Method m = myMethod.getMethod(); // how?
Class<?>[] argClasses = m.getParameterTypes();
}

int myMethod(String arg) {
// ...
}


The piece myMethod.getMethod() is not valid. It is my way of saying "get from the method-identifier to the java.lang.reflect.Method object".


The string-way I do not want to use would be similar to m = getClass().getDeclaredMethod("myMethod", ...)


I use myMethod like a regular identifier and not as a String, i.e. "myMethod". I think this would be beneficial for refactoring, where someone renames myMethod to hisMethod -- the relation via the String-name would break, where the relation via the identifier would be refactored as well.






Aucun commentaire:

Enregistrer un commentaire