mercredi 31 décembre 2014

How to ignore modifiers when invoking methods on run-time - JAVA

I want to look for a specific annotation of every method in a class and if a method with that annotation is found, i would like to invoke it. Also, if it's not found in the current class, a check on all inheriting classes should be made.


My problem is that there might be some methods that are protected, private etc. and I would like to ignore those modifiers and to gain access to all methods (i.e even if it's private etc.)


So this is how I invoke (given is the name of the annotation that I'm looking for:



if (m.isAnnotationPresent(Given.class)) {
m.invoke(instObj, intArgument);
}


(And this is how I check the rest of the class hierarchy - if i made a mistake somewhere, enlighten me please:



Class<?> superC = c.getSuperclass();
while (!(superC.equals(Object.class))) {
handleGiven(instObj, superC, methods, currentSentence,
methodArgument);


when handleGiven is a recursive call.






Aucun commentaire:

Enregistrer un commentaire