I have this question on my mind and I severely need to find an answer for it.
My problem: How to get a reference of the object (something like memory address) then get the object using the reference (note: I know where the object located (in an array)). I hear there are ways to do what I want to do using the Unsafe class, or even JNA (I lack knowledge of both of them, but happy to learn!)
What I'm doing:
I have made an application (app1) which is inserted into another application**(app2)** through Instrumentation and this application (app1) sends data fetched from (app2) through the Reflection API to another application of mine (app3) through RMI. The objects in the array change constantly in terms of values, so what I'm doing is fetching the objects and reading their data but my problem comes when I try to get updated data using the previously obtained object which is normally done by getDeclaredField(fieldName).get(PreviouslyObtainedObject);
but I can't keep the previously obtained object on the current application (app1) due to it being Unserializable so I need to get a reference of this object so I can get it later on.
What I've tried:
I've tried doing it using HashCode but HashCode just keeps changing/dissppears whenever the object's values change and the hashCode might be the same with other objects so this creates another problem.
I've tried doing it using XStream,GSON,etc.. but using the xml/json it creates a new object and doesn't relate to the previously obtained object. so when I try to get updated info about the object using the JSON/XML created object it returns the old info as it does not equal the previously obtained object. So like getDeclaredField(fieldName).get(PreviouslyObtainedObject)
returns updated information about the object but getDeclaredField(fieldName).get(JSON/XMLVersionOfPreviouslyObtainedObject)
does not return new data.
I can't do Object -> byte Array due to the objects being unserializable.
There's no code in the question because I felt that the code won't serve a purpose to the question. to get more information about what I'm doing you could view a previous question of mine: Getting an Object by reference in Java Note: there's a difference between this question and my previous one as the previous one discussed ways of wrapping or turning the objects into JSON or XML format and this one is just for getting a reference.
Summary of what I'm trying to do (for more clarity):
I have 2 applications: first app (app1): Application which is instrumented into another application (to gather data and send)
second app (app2): Application which receives the data from the instrumented data and does logic..
app1 gets the data from the application its instrumented into via the Reflection API so like (getDeclaredField(fieldName).get(object/null);) app1 send the Object reference (memory address/hashCode/this is what I want) and the current values of the object to app2. app2 sends back the reference whenever it wants and gets the object updated data.
so like in code terms:
The first time I want to get an object, I access an object array using reflection then loop through it to find my perfect object like this:
Object[] obArray = (Object[]) getClassLoader().loadClass("className").getDeclaredField("fieldName").get(null);
for(Object ob : obArray) {
if(ob is what I want) return ob;
}
then If I wanted to get updated information of ob (object that I fetched above). I do getClassLoader().loadClass(className).getDeclaredField(fieldName).get(ob);
Now due to the amount of objects I process I can't store the objects I fetched on the same application so I must get a reference to them (something like a memory address or hashCode) to send to app2 so that app2 can send it back to app1 at a later date to get the object back and get updated information about it.
Aucun commentaire:
Enregistrer un commentaire