java - XPosed: Hooking a function overridden in an enum -
say have enum class defined (adapted java documentation)
package com.example.planetexample; public enum planet { mercury (3.303e+23, 2.4397e6){ public double surfacegravity() { return 42; } }, venus (4.869e+24, 6.0518e6); private final double mass; // in kilograms private final double radius; // in meters planet(double mass, double radius) { this.mass = mass; this.radius = radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double g = 6.67300e-11; double surfacegravity() { return g * mass / (radius * radius); } }
now, want use xposed hook surfacegravity
function has been overriden mercury (and not general 1 defined below). how can access function?
i tried findandhookmethod("com.example.planetexample.planet", lpparam.classloader, "surfacegravity", [etc])
, 1 hooks general surfacegravity defined planet class, , not 1 defined mercury. if try com.example.planetexample.planet$mercury
or com.example.planetexample.planet.mercury
, i'm getting errors xposed function surfacegravity
not found.
is there way hook function using xposed?
mercury field of planet enum. since has own implementation, class generated @ compile time it, unfortunately name won't match field's name (e.g., in case com.test.planet$1).
consider following example:
public static void main(string[] args) { system.out.println("mercury radius: " + planet.mercury.surfacegravity()); // 42 system.out.println("planet class: " + planet.class.getname()); //prints "com.test.planet" try { class<?> planet_cls = class.forname("com.test.planet"); system.out.println(planet.class); // com.test.planet system.out.println(planet.mercury.getclass()); // com.test.planet$1 system.out.println(planet.venus.getclass()); // com.test.planet for(class c: planet.class.getdeclaredclasses()) system.out.println("name:" + c.getname()); // wont print for(field c: planet.class.getdeclaredfields()) system.out.println("field name:" + c.getname()); // mercury & venus :) try { field mercury_field = planet_cls.getdeclaredfield("mercury"); object o = mercury_field.get(null); system.out.println("field class name: " + o.getclass()); // com.test.planet$1 try { method surfacegravity = o.getclass().getdeclaredmethod("surfacegravity"); system.out.println("confirm result: " + surfacegravity.invoke(o)); // 42! } catch (nosuchmethodexception e) { e.printstacktrace(); } catch (invocationtargetexception e) { e.printstacktrace(); } } catch (nosuchfieldexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } } catch (classnotfoundexception e) { e.printstacktrace(); } }
so retrieving field name, can class , methods. note in xposed have hooking api receives method instead of looking name, pass method in example.
the output of code save time:
mercury radius: 42.0 planet class: com.test.planet class com.test.planet class com.test.planet$1 class com.test.planet field name:mercury field name:venus field name:mass field name:radius field name:g field name:$values field class name: class com.test.planet$1 confirm result: 42.0
Comments
Post a Comment