jeudi 23 avril 2015

Approach doing the same stuff on different objects

I am currently making a library which is an utility for me to handle something which is not associated with the question (I am implicitly not saying the subject because it is not really important), however it does use reflection.

I am retrieving all declared and inherited methods from a class, which currently works fine and is not the issue. But the thing is, I need to do this as well for sub-classes since those inherit over like methods do (however you cannot override those like methods).

The problem that I am facing that it will use the same algorithm but there will be on difference, instead of calling clazz.getDeclaredMethods() I need to call clazz.getMethods. What is the best way too approach this, and I kind of need to return Class[] and Method[] in the method signature as well.

Normally I would look for a shared superclass, but in this case I prefer to the have Class[] and Method[] accordingly. For starters, I did some research and found some shared superclasses:

  • GenericDeclaration
  • AnnotatedElement

Since I need both Class[] and Method[] arrays I am thinking something like generics, so the method would look like:

public static <T extends GenericDecleration> T[] getT () {

}

But how would I detect whether I need to call getDeclaredMethods or getDeclaredClasses?

What is the best approach on how to do this without duplicating a lot of code? I really tried to explain myself here, but if it is still unclear what I am doing please feel free to ask away!

Thank you very much in advance!





Aucun commentaire:

Enregistrer un commentaire