Skip to content

Commit

Permalink
Catch generic ReflectiveOperationException
Browse files Browse the repository at this point in the history
This exception is a super-type of:
 - ClassNotFoundException
 - IllegalAccessException
 - InstantiationException
 - InvocationTargetException
 - NoSuchFieldException
 - NoSuchMethodException

Use it to simplify.
  • Loading branch information
rom1v committed Feb 10, 2024
1 parent 05b5dea commit f7b4a18
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.os.IInterface;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressLint("PrivateApi,DiscouragedPrivateApi")
Expand All @@ -34,7 +33,7 @@ static ActivityManager create() {
Method getDefaultMethod = cls.getDeclaredMethod("getDefault");
IInterface am = (IInterface) getDefaultMethod.invoke(null);
return new ActivityManager(am);
} catch (Exception e) {
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
Expand Down Expand Up @@ -89,7 +88,7 @@ private ContentProvider getContentProviderExternal(String name, IBinder token) {
return null;
}
return new ContentProvider(this, provider, name, token);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | NoSuchFieldException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -99,7 +98,7 @@ void removeContentProviderExternal(String name, IBinder token) {
try {
Method method = getRemoveContentProviderExternalMethod();
method.invoke(manager, name, token);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.Build;
import android.os.IInterface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ClipboardManager {
Expand Down Expand Up @@ -98,8 +97,7 @@ private Method getSetPrimaryClipMethod() throws NoSuchMethodException {
return setPrimaryClipMethod;
}

private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager)
throws InvocationTargetException, IllegalAccessException {
private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager) throws ReflectiveOperationException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME);
}
Expand All @@ -121,8 +119,7 @@ private static ClipData getPrimaryClip(Method method, int methodVersion, IInterf
}
}

private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData)
throws InvocationTargetException, IllegalAccessException {
private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData) throws ReflectiveOperationException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME);
return;
Expand All @@ -149,7 +146,7 @@ public CharSequence getText() {
return null;
}
return clipData.getItemAt(0).getText();
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -161,14 +158,14 @@ public boolean setText(CharSequence text) {
ClipData clipData = ClipData.newPlainText(null, text);
setPrimaryClip(method, setMethodVersion, manager, clipData);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
}

private static void addPrimaryClipChangedListener(Method method, int methodVersion, IInterface manager, IOnPrimaryClipChangedListener listener)
throws InvocationTargetException, IllegalAccessException {
throws ReflectiveOperationException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, listener, FakeContext.PACKAGE_NAME);
return;
Expand Down Expand Up @@ -220,7 +217,7 @@ public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener liste
Method method = getAddPrimaryClipChangedListener();
addPrimaryClipChangedListener(method, addListenerMethodVersion, manager, listener);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.os.IBinder;

import java.io.Closeable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ContentProvider implements Closeable {
Expand Down Expand Up @@ -75,8 +74,7 @@ private Method getCallMethod() throws NoSuchMethodException {
return callMethod;
}

private Bundle call(String callMethod, String arg, Bundle extras)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
private Bundle call(String callMethod, String arg, Bundle extras) throws ReflectiveOperationException {
try {
Method method = getCallMethod();
Object[] args;
Expand All @@ -97,7 +95,7 @@ private Bundle call(String callMethod, String arg, Bundle extras)
}
}
return (Bundle) method.invoke(provider, args);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.os.Build;
import android.os.IBinder;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressLint({"PrivateApi", "SoonBlockedPrivateApi", "BlockedPrivateApi"})
Expand Down Expand Up @@ -55,7 +54,7 @@ public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
try {
Method method = getGetPhysicalDisplayTokenMethod();
return (IBinder) method.invoke(null, physicalDisplayId);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -72,7 +71,7 @@ public static long[] getPhysicalDisplayIds() {
try {
Method method = getGetPhysicalDisplayIdsMethod();
return (long[]) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.Display;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -24,7 +23,7 @@ static DisplayManager create() {
Method getInstanceMethod = clazz.getDeclaredMethod("getInstance");
Object dmg = getInstanceMethod.invoke(null);
return new DisplayManager(dmg);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
Expand Down Expand Up @@ -75,7 +74,7 @@ private static int parseDisplayFlags(String text) {
try {
Field filed = Display.class.getDeclaredField(flagString);
flags |= filed.getInt(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
} catch (ReflectiveOperationException e) {
// Silently ignore, some flags reported by "dumpsys display" are @TestApi
}
}
Expand All @@ -97,15 +96,15 @@ public DisplayInfo getDisplayInfo(int displayId) {
int layerStack = cls.getDeclaredField("layerStack").getInt(displayInfo);
int flags = cls.getDeclaredField("flags").getInt(displayInfo);
return new DisplayInfo(displayId, new Size(width, height), rotation, layerStack, flags);
} catch (Exception e) {
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}

public int[] getDisplayIds() {
try {
return (int[]) manager.getClass().getMethod("getDisplayIds").invoke(manager);
} catch (Exception e) {
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.view.InputEvent;
import android.view.MotionEvent;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressLint("PrivateApi,DiscouragedPrivateApi")
Expand All @@ -28,7 +27,7 @@ static InputManager create() {
Method getInstanceMethod = inputManagerClass.getDeclaredMethod("getInstance");
Object im = getInstanceMethod.invoke(null);
return new InputManager(im);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
Expand Down Expand Up @@ -57,7 +56,7 @@ public boolean injectInputEvent(InputEvent inputEvent, int mode) {
try {
Method method = getInjectInputEventMethod();
return (boolean) method.invoke(manager, inputEvent, mode);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
Expand All @@ -75,7 +74,7 @@ public static boolean setDisplayId(InputEvent inputEvent, int displayId) {
Method method = getSetDisplayIdMethod();
method.invoke(inputEvent, displayId);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Cannot associate a display id to the input event", e);
return false;
}
Expand All @@ -93,7 +92,7 @@ public static boolean setActionButton(MotionEvent motionEvent, int actionButton)
Method method = getSetActionButtonMethod();
method.invoke(motionEvent, actionButton);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Cannot set action button on MotionEvent", e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Build;
import android.os.IInterface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class PowerManager {
Expand Down Expand Up @@ -35,7 +34,7 @@ public boolean isScreenOn() {
try {
Method method = getIsScreenOnMethod();
return (boolean) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.os.IInterface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class StatusBarManager {
Expand Down Expand Up @@ -67,7 +66,7 @@ public void expandNotificationsPanel() {
} else {
method.invoke(manager);
}
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand All @@ -82,7 +81,7 @@ public void expandSettingsPanel() {
// old version
method.invoke(manager);
}
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand All @@ -91,7 +90,7 @@ public void collapsePanels() {
try {
Method method = getCollapsePanelsMethod();
method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.IBinder;
import android.view.Surface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressLint("PrivateApi")
Expand Down Expand Up @@ -109,7 +108,7 @@ public static IBinder getBuiltInDisplay() {

// call getInternalDisplayToken()
return (IBinder) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -126,7 +125,7 @@ public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
try {
Method method = getGetPhysicalDisplayTokenMethod();
return (IBinder) method.invoke(null, physicalDisplayId);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -152,7 +151,7 @@ public static long[] getPhysicalDisplayIds() {
try {
Method method = getGetPhysicalDisplayIdsMethod();
return (long[]) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return null;
}
Expand All @@ -170,7 +169,7 @@ public static boolean setDisplayPowerMode(IBinder displayToken, int mode) {
Method method = getSetDisplayPowerModeMethod();
method.invoke(null, displayToken, mode);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.view.IDisplayFoldListener;
import android.view.IRotationWatcher;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class WindowManager {
Expand Down Expand Up @@ -66,7 +65,7 @@ public int getRotation() {
try {
Method method = getGetRotationMethod();
return (int) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return 0;
}
Expand All @@ -76,7 +75,7 @@ public void freezeRotation(int rotation) {
try {
Method method = getFreezeRotationMethod();
method.invoke(manager, rotation);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand All @@ -85,7 +84,7 @@ public boolean isRotationFrozen() {
try {
Method method = getIsRotationFrozenMethod();
return (boolean) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
return false;
}
Expand All @@ -95,7 +94,7 @@ public void thawRotation() {
try {
Method method = getThawRotationMethod();
method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e);
}
}
Expand Down

0 comments on commit f7b4a18

Please sign in to comment.