Catch generic ReflectiveOperationException

This exception is a super-type of:
 - ClassNotFoundException
 - IllegalAccessException
 - InstantiationException
 - InvocationTargetException
 - NoSuchFieldException
 - NoSuchMethodException

Use it to simplify.
This commit is contained in:
Romain Vimont 2024-02-09 18:39:57 +01:00
parent 05b5deacad
commit f7b4a18b43
10 changed files with 33 additions and 46 deletions

View file

@ -13,7 +13,6 @@ import android.os.IBinder;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@SuppressLint("PrivateApi,DiscouragedPrivateApi") @SuppressLint("PrivateApi,DiscouragedPrivateApi")
@ -34,7 +33,7 @@ public final class ActivityManager {
Method getDefaultMethod = cls.getDeclaredMethod("getDefault"); Method getDefaultMethod = cls.getDeclaredMethod("getDefault");
IInterface am = (IInterface) getDefaultMethod.invoke(null); IInterface am = (IInterface) getDefaultMethod.invoke(null);
return new ActivityManager(am); return new ActivityManager(am);
} catch (Exception e) { } catch (ReflectiveOperationException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }
@ -89,7 +88,7 @@ public final class ActivityManager {
return null; return null;
} }
return new ContentProvider(this, provider, name, token); return new ContentProvider(this, provider, name, token);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | NoSuchFieldException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -99,7 +98,7 @@ public final class ActivityManager {
try { try {
Method method = getRemoveContentProviderExternalMethod(); Method method = getRemoveContentProviderExternalMethod();
method.invoke(manager, name, token); method.invoke(manager, name, token);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }

View file

@ -8,7 +8,6 @@ import android.content.IOnPrimaryClipChangedListener;
import android.os.Build; import android.os.Build;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public final class ClipboardManager { public final class ClipboardManager {
@ -98,8 +97,7 @@ public final class ClipboardManager {
return setPrimaryClipMethod; return setPrimaryClipMethod;
} }
private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager) private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager) throws ReflectiveOperationException {
throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME); return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME);
} }
@ -121,8 +119,7 @@ public final class ClipboardManager {
} }
} }
private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData) private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData) throws ReflectiveOperationException {
throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME); method.invoke(manager, clipData, FakeContext.PACKAGE_NAME);
return; return;
@ -149,7 +146,7 @@ public final class ClipboardManager {
return null; return null;
} }
return clipData.getItemAt(0).getText(); return clipData.getItemAt(0).getText();
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -161,14 +158,14 @@ public final class ClipboardManager {
ClipData clipData = ClipData.newPlainText(null, text); ClipData clipData = ClipData.newPlainText(null, text);
setPrimaryClip(method, setMethodVersion, manager, clipData); setPrimaryClip(method, setMethodVersion, manager, clipData);
return true; return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }
} }
private static void addPrimaryClipChangedListener(Method method, int methodVersion, IInterface manager, IOnPrimaryClipChangedListener listener) 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) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, listener, FakeContext.PACKAGE_NAME); method.invoke(manager, listener, FakeContext.PACKAGE_NAME);
return; return;
@ -220,7 +217,7 @@ public final class ClipboardManager {
Method method = getAddPrimaryClipChangedListener(); Method method = getAddPrimaryClipChangedListener();
addPrimaryClipChangedListener(method, addListenerMethodVersion, manager, listener); addPrimaryClipChangedListener(method, addListenerMethodVersion, manager, listener);
return true; return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }

View file

@ -11,7 +11,6 @@ import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import java.io.Closeable; import java.io.Closeable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public final class ContentProvider implements Closeable { public final class ContentProvider implements Closeable {
@ -75,8 +74,7 @@ public final class ContentProvider implements Closeable {
return callMethod; return callMethod;
} }
private Bundle call(String callMethod, String arg, Bundle extras) private Bundle call(String callMethod, String arg, Bundle extras) throws ReflectiveOperationException {
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
try { try {
Method method = getCallMethod(); Method method = getCallMethod();
Object[] args; Object[] args;
@ -97,7 +95,7 @@ public final class ContentProvider implements Closeable {
} }
} }
return (Bundle) method.invoke(provider, args); return (Bundle) method.invoke(provider, args);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
throw e; throw e;
} }

View file

@ -7,7 +7,6 @@ import android.annotation.TargetApi;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@SuppressLint({"PrivateApi", "SoonBlockedPrivateApi", "BlockedPrivateApi"}) @SuppressLint({"PrivateApi", "SoonBlockedPrivateApi", "BlockedPrivateApi"})
@ -55,7 +54,7 @@ public final class DisplayControl {
try { try {
Method method = getGetPhysicalDisplayTokenMethod(); Method method = getGetPhysicalDisplayTokenMethod();
return (IBinder) method.invoke(null, physicalDisplayId); return (IBinder) method.invoke(null, physicalDisplayId);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -72,7 +71,7 @@ public final class DisplayControl {
try { try {
Method method = getGetPhysicalDisplayIdsMethod(); Method method = getGetPhysicalDisplayIdsMethod();
return (long[]) method.invoke(null); return (long[]) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }

View file

@ -9,7 +9,6 @@ import android.annotation.SuppressLint;
import android.view.Display; import android.view.Display;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -24,7 +23,7 @@ public final class DisplayManager {
Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); Method getInstanceMethod = clazz.getDeclaredMethod("getInstance");
Object dmg = getInstanceMethod.invoke(null); Object dmg = getInstanceMethod.invoke(null);
return new DisplayManager(dmg); return new DisplayManager(dmg);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { } catch (ReflectiveOperationException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }
@ -75,7 +74,7 @@ public final class DisplayManager {
try { try {
Field filed = Display.class.getDeclaredField(flagString); Field filed = Display.class.getDeclaredField(flagString);
flags |= filed.getInt(null); flags |= filed.getInt(null);
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (ReflectiveOperationException e) {
// Silently ignore, some flags reported by "dumpsys display" are @TestApi // Silently ignore, some flags reported by "dumpsys display" are @TestApi
} }
} }
@ -97,7 +96,7 @@ public final class DisplayManager {
int layerStack = cls.getDeclaredField("layerStack").getInt(displayInfo); int layerStack = cls.getDeclaredField("layerStack").getInt(displayInfo);
int flags = cls.getDeclaredField("flags").getInt(displayInfo); int flags = cls.getDeclaredField("flags").getInt(displayInfo);
return new DisplayInfo(displayId, new Size(width, height), rotation, layerStack, flags); return new DisplayInfo(displayId, new Size(width, height), rotation, layerStack, flags);
} catch (Exception e) { } catch (ReflectiveOperationException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }
@ -105,7 +104,7 @@ public final class DisplayManager {
public int[] getDisplayIds() { public int[] getDisplayIds() {
try { try {
return (int[]) manager.getClass().getMethod("getDisplayIds").invoke(manager); return (int[]) manager.getClass().getMethod("getDisplayIds").invoke(manager);
} catch (Exception e) { } catch (ReflectiveOperationException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }

View file

@ -6,7 +6,6 @@ import android.annotation.SuppressLint;
import android.view.InputEvent; import android.view.InputEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@SuppressLint("PrivateApi,DiscouragedPrivateApi") @SuppressLint("PrivateApi,DiscouragedPrivateApi")
@ -28,7 +27,7 @@ public final class InputManager {
Method getInstanceMethod = inputManagerClass.getDeclaredMethod("getInstance"); Method getInstanceMethod = inputManagerClass.getDeclaredMethod("getInstance");
Object im = getInstanceMethod.invoke(null); Object im = getInstanceMethod.invoke(null);
return new InputManager(im); return new InputManager(im);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { } catch (ReflectiveOperationException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }
@ -57,7 +56,7 @@ public final class InputManager {
try { try {
Method method = getInjectInputEventMethod(); Method method = getInjectInputEventMethod();
return (boolean) method.invoke(manager, inputEvent, mode); return (boolean) method.invoke(manager, inputEvent, mode);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }
@ -75,7 +74,7 @@ public final class InputManager {
Method method = getSetDisplayIdMethod(); Method method = getSetDisplayIdMethod();
method.invoke(inputEvent, displayId); method.invoke(inputEvent, displayId);
return true; return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Cannot associate a display id to the input event", e); Ln.e("Cannot associate a display id to the input event", e);
return false; return false;
} }
@ -93,7 +92,7 @@ public final class InputManager {
Method method = getSetActionButtonMethod(); Method method = getSetActionButtonMethod();
method.invoke(motionEvent, actionButton); method.invoke(motionEvent, actionButton);
return true; return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Cannot set action button on MotionEvent", e); Ln.e("Cannot set action button on MotionEvent", e);
return false; return false;
} }

View file

@ -6,7 +6,6 @@ import android.annotation.SuppressLint;
import android.os.Build; import android.os.Build;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public final class PowerManager { public final class PowerManager {
@ -35,7 +34,7 @@ public final class PowerManager {
try { try {
Method method = getIsScreenOnMethod(); Method method = getIsScreenOnMethod();
return (boolean) method.invoke(manager); return (boolean) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }

View file

@ -4,7 +4,6 @@ import com.genymobile.scrcpy.Ln;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public final class StatusBarManager { public final class StatusBarManager {
@ -67,7 +66,7 @@ public final class StatusBarManager {
} else { } else {
method.invoke(manager); method.invoke(manager);
} }
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }
@ -82,7 +81,7 @@ public final class StatusBarManager {
// old version // old version
method.invoke(manager); method.invoke(manager);
} }
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }
@ -91,7 +90,7 @@ public final class StatusBarManager {
try { try {
Method method = getCollapsePanelsMethod(); Method method = getCollapsePanelsMethod();
method.invoke(manager); method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }

View file

@ -8,7 +8,6 @@ import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.view.Surface; import android.view.Surface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@SuppressLint("PrivateApi") @SuppressLint("PrivateApi")
@ -109,7 +108,7 @@ public final class SurfaceControl {
// call getInternalDisplayToken() // call getInternalDisplayToken()
return (IBinder) method.invoke(null); return (IBinder) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -126,7 +125,7 @@ public final class SurfaceControl {
try { try {
Method method = getGetPhysicalDisplayTokenMethod(); Method method = getGetPhysicalDisplayTokenMethod();
return (IBinder) method.invoke(null, physicalDisplayId); return (IBinder) method.invoke(null, physicalDisplayId);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -152,7 +151,7 @@ public final class SurfaceControl {
try { try {
Method method = getGetPhysicalDisplayIdsMethod(); Method method = getGetPhysicalDisplayIdsMethod();
return (long[]) method.invoke(null); return (long[]) method.invoke(null);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return null; return null;
} }
@ -170,7 +169,7 @@ public final class SurfaceControl {
Method method = getSetDisplayPowerModeMethod(); Method method = getSetDisplayPowerModeMethod();
method.invoke(null, displayToken, mode); method.invoke(null, displayToken, mode);
return true; return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }

View file

@ -7,7 +7,6 @@ import android.os.IInterface;
import android.view.IDisplayFoldListener; import android.view.IDisplayFoldListener;
import android.view.IRotationWatcher; import android.view.IRotationWatcher;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public final class WindowManager { public final class WindowManager {
@ -66,7 +65,7 @@ public final class WindowManager {
try { try {
Method method = getGetRotationMethod(); Method method = getGetRotationMethod();
return (int) method.invoke(manager); return (int) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return 0; return 0;
} }
@ -76,7 +75,7 @@ public final class WindowManager {
try { try {
Method method = getFreezeRotationMethod(); Method method = getFreezeRotationMethod();
method.invoke(manager, rotation); method.invoke(manager, rotation);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }
@ -85,7 +84,7 @@ public final class WindowManager {
try { try {
Method method = getIsRotationFrozenMethod(); Method method = getIsRotationFrozenMethod();
return (boolean) method.invoke(manager); return (boolean) method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false; return false;
} }
@ -95,7 +94,7 @@ public final class WindowManager {
try { try {
Method method = getThawRotationMethod(); Method method = getThawRotationMethod();
method.invoke(manager); method.invoke(manager);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
} }
} }