dimanche 5 février 2017

Calling a method using reflection causes exception

I am trying to Invoke a method on a private field that gets its instance from the system service. The problem is I am not able to invoke the method on the private field and I keep getting IllegalArgumentExceptions.

Interface MyRequiredService {
   public void setValues(int a, int b, String c);
}

public Class ARandomClass {
   private MyRequiredService mService;

   private synchronized MyRequiredService getServiceInstance() {
        mService = getInternalSystemService();
        return mService;
   }
}

My Code:

 private void testingIstatusBar() {
    Object object = getSystemService("internalSystemService");

    Class<?> randomClass;
    try {
        randomClass = Class.forName("com.system.ARandomClass ");
        Field mService = randomClass.getDeclaredField("mService");
        mService.setAccessible(true);

        Class<?> myRequiredService = mService.getType();

        Method getService = myRequiredService.getMethod("setValues",
                new Class[] { int.class, int.class, String.class });

        getService.setAccessible(true);
        getService.invoke((Object)mService.getType(), 0, 1,
                "OnSystemUiVisibilityChangeListener");

    } catch (Exception e) {
        Log.e(TAG, " Exception"+ e.getLocalizedMessage());
    }
}

Exception:

java.lang.IllegalArgumentException: Expected receiver of type com.system.internal.MyRequiredService, but got java.lang.Class<com.system.internal.MyRequiredService>

How do I call the setValue() method? What am I doing wrong?





Aucun commentaire:

Enregistrer un commentaire