mercredi 2 décembre 2015

Object on which a method was called - Java reflection

I am very new to Java Reflection APIs. I am trying to understand the same. In my pursuit, I came across a situation, where in I would like to know/get a reference to the object on which a certain method was called.

Example - from the Android world :

URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

Now I want to know if there is a way of knowing that 'url' was the object on which the openConnection() was called. The end goal is that I want to be able to get the string 'myurl' that was passed to URL while creating it's object.

Is there a way I can do this ?

I have already gone through this : How do I find the caller of a method using stacktrace or reflection?

But, the above hasn't proved very helpful in my case. Moreover, in the above there are comments that mention that even if I used StackTraceElement, I would not get the name/reference of the object. I would just get the type of the caller instead. And with the called alone, I don't think I would be able to achieve my end goal of finding the 'url' string. Will I ?





Aucun commentaire:

Enregistrer un commentaire