Is it possible to do method swizzling in android using java? i would like to intercept a system method and log its parameters then process it normally
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think that technique could not be used using Java in any environment.
Maybe you could achieve a similar result using AOP.
But what you can do with that looks limited on Android. See Aspect-oriented programming in android. In fact, since you won’t be compiling the target code (system method), compile-time weaving (which appears to be all you could use on Android) will be useless for this case. As is this answer I suppose.
One other thought… I guess you want to do this logging consistently. But if you needed this to debug a problem, you could do it using a conditional breakpoint in Eclipse.
I don’t know specifically whether this works with methods in the Android SDK. But it does work with methods in the Java SDK. For example, here is simple code:
I made a conditional breakpoint in PrintStream.print, like this:
And the console output when debugging in the program is this:
Note that since the JDK is not compiled with debug symbols, I can’t refer to method parameters by name, but using the
arg0..argn.