I'm hooking into a method via the Xposed framework for Android. This effectively allows me to inject my own code into another Android app at runtime and have it run just as if the target app was built with it. Fun stuff. While this is how I'm injecting the code, my question should be independent of Xposed as it's using an extension of the standard Java reflection utilities to retrieve the class references I need.
Class targetClass = XposedHelpers.findClass("fullClassName", targetAppClassLoader);
Object timerField = XposedHelpers.getStaticObjectField(targetClass, "targetFieldName");
// This /should/ synchronize with the actual static field not our local representation of it
synchronized (timerField) {
// Do stuff
}
In the above snippet I'm retrieving a static object field in a class via reflection. Internally, the target app uses synchronized
blocks on this field. I need to operate in a similar synchronous manner so I do the same in my own code (link to full code).
Since Java objects are sorta passed by reference I should be retrieving a local reference to the static object in the target app. But do Java synchronized
blocks work on reflected objects? And do they work together with non-reflected pre-existing synchronized
blocks?
For reference, here is the code for findClass
and getStaticObjectField
. The former essentially returns a returns a Class<?>
from org.external.org.apache.commons.lang3.ClassUtils
and the latter (essentially) calls getDeclaredField
on the Class<?>
parameter, sets the Field
to accessible, and returns the result of get(null)
on the Field
.
Aucun commentaire:
Enregistrer un commentaire