vendredi 2 décembre 2016

How to store methods in order to call it arbitrarily with AspectJ?

I am a newbie of AspectJ and Java Reflection and I am bit lost. My objective is to extract and store all the method with a certain annotation during their execution and invoke them myself arbitrarily later.

Let's make an example

public class example {

public void method1(String str){

}

@tagged
public void method2(){

}
@tagged
public void method3(String str, int i){

}
@tagged
public void method4(Double d){

}

}

and the AspectJ code should do something like this

@Aspect
public class Generic {
List<Class> classes = new LinkedList<>;
List<String> methods= new LinkedList<>;
List<Object[]> arguments_list= new LinkedList<>;  

@before("execution(@Tagged * *(..))")
public void storeMethods(JoinPoint jp){


        classes.add(jp.getThis().getClass());
        methods.add(jp.getSignature().getName();)
        arguments_list.add(jp.getArgs());
        }

}

and after I stored all the methods that I need, I would like to do something like

for(int i = 0; i<classes.size, i++){
//some elaboration of the arguments in arguments_list[i]

//invoke methods[i] with arguments_list[i]
}

Therefore, my questions are:

  • Do you think that is possible to invoke these methods separately?
  • Is there a way to obtain the types of the arguments of each method? With jp.getArgs() I receive a list of object but I am not aware of their type

Thank you in advance





Aucun commentaire:

Enregistrer un commentaire