Linearize try-catch blocks

There are many possible method signatures for getPrimaryClip() and
setPrimaryClip().

Avoid the nested try-catch blocks.
This commit is contained in:
Romain Vimont 2024-07-11 12:19:47 +02:00
parent 9989668226
commit fe7494c492

View file

@ -38,39 +38,54 @@ public final class ClipboardManager {
if (getPrimaryClipMethod == null) { if (getPrimaryClipMethod == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class); getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class);
} else { return getPrimaryClipMethod;
}
try { try {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class); getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class);
getMethodVersion = 0; getMethodVersion = 0;
} catch (NoSuchMethodException e1) { return getPrimaryClipMethod;
} catch (NoSuchMethodException e) {
// fall-through
}
try { try {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class); getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class);
getMethodVersion = 1; getMethodVersion = 1;
} catch (NoSuchMethodException e2) { return getPrimaryClipMethod;
} catch (NoSuchMethodException e) {
// fall-through
}
try { try {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class, int.class); getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class, int.class);
getMethodVersion = 2; getMethodVersion = 2;
} catch (NoSuchMethodException e3) { return getPrimaryClipMethod;
} catch (NoSuchMethodException e) {
// fall-through
}
try { try {
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class, String.class); getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class, String.class);
getMethodVersion = 3; getMethodVersion = 3;
} catch (NoSuchMethodException e4) { return getPrimaryClipMethod;
} catch (NoSuchMethodException e) {
// fall-through
}
try { try {
getPrimaryClipMethod = manager.getClass() getPrimaryClipMethod = manager.getClass()
.getMethod("getPrimaryClip", String.class, String.class, int.class, int.class, boolean.class); .getMethod("getPrimaryClip", String.class, String.class, int.class, int.class, boolean.class);
getMethodVersion = 4; getMethodVersion = 4;
} catch (NoSuchMethodException e5) { return getPrimaryClipMethod;
} catch (NoSuchMethodException e) {
// fall-through
}
getPrimaryClipMethod = manager.getClass() getPrimaryClipMethod = manager.getClass()
.getMethod("getPrimaryClip", String.class, String.class, String.class, String.class, int.class, int.class, .getMethod("getPrimaryClip", String.class, String.class, String.class, String.class, int.class, int.class, boolean.class);
boolean.class);
getMethodVersion = 5; getMethodVersion = 5;
} }
}
}
}
}
}
}
return getPrimaryClipMethod; return getPrimaryClipMethod;
} }
@ -78,28 +93,38 @@ public final class ClipboardManager {
if (setPrimaryClipMethod == null) { if (setPrimaryClipMethod == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class); setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class);
} else { return setPrimaryClipMethod;
}
try { try {
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class); setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class);
setMethodVersion = 0; setMethodVersion = 0;
return setPrimaryClipMethod;
} catch (NoSuchMethodException e1) { } catch (NoSuchMethodException e1) {
// fall-through
}
try { try {
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class); setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class);
setMethodVersion = 1; setMethodVersion = 1;
return setPrimaryClipMethod;
} catch (NoSuchMethodException e2) { } catch (NoSuchMethodException e2) {
// fall-through
}
try { try {
setPrimaryClipMethod = manager.getClass() setPrimaryClipMethod = manager.getClass()
.getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class, int.class); .getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class, int.class);
setMethodVersion = 2; setMethodVersion = 2;
return setPrimaryClipMethod;
} catch (NoSuchMethodException e3) { } catch (NoSuchMethodException e3) {
// fall-through
}
setPrimaryClipMethod = manager.getClass() setPrimaryClipMethod = manager.getClass()
.getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class, int.class, boolean.class); .getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class, int.class, boolean.class);
setMethodVersion = 3; setMethodVersion = 3;
} }
}
}
}
}
return setPrimaryClipMethod; return setPrimaryClipMethod;
} }