mercredi 8 mars 2017

invoke() method intermediate steps in Java reflection

Is there any possibility to get control of the internal processing steps of invoke() method in java reflection ? Here, I am creating an "Encryption" object whose "send()" method will be run when the invoke() method of reflection is called.

The problem is, my send() method is calling another method "calculate()" which might take long time to execute. Hence, I want to keep track the period of this calculation so that I can do some other work and get notified once this calculation is finished. Another problem is, it is not fixed that I will always use calculate() method. I might invoke some other object (say Compressed) that use different methods inside its send() method. Hence it is a little bit necessary for me to understand intermediate results of the invoke() method. Is that possible in Java ? Here is my code snippet. Any help is much appreciated.

Class Encryption{

// my class whose send() method will be called by invoke
String data;

public Encryption(String data)
{
    this.data = data;
}
public void send()
{
    String encryptedResult = calculate(data);
    System.out.println("Here is the encrypted result: " + encryptedResult);
}
private String calculate(String data)
{
    //calculation for encryption that might take long time
}

}

Class encryptionMain{

public static void main(String args[])
{

    Encryption enc = new Encryption("Hello World");
    Class cls = enc.getClass();
    Object obj = cls.newInstance();
    Method method;
    types = null;
    params = null;
    methodName = "send";
        method = obj.getClass().getMethod(methodName, types);
        Object objRet = method.invoke(obj, params);
    ......
    ......
}

}





Aucun commentaire:

Enregistrer un commentaire