am 03a1c442: Clean up compatible utility classes

* commit '03a1c442aeddbd511be92e11465b6206d2ca976c':
  Clean up compatible utility classes
This commit is contained in:
Tadashi G. Takaoka 2014-08-04 02:04:23 +00:00 committed by Android Git Automerger
commit 9681027aad
8 changed files with 41 additions and 30 deletions

View file

@ -21,6 +21,7 @@ import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public final class CompatUtils {
@ -33,31 +34,31 @@ public final class CompatUtils {
public static Class<?> getClass(final String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
} catch (final ClassNotFoundException e) {
return null;
}
}
public static Method getMethod(final Class<?> targetClass, final String name,
final Class<?>... parameterTypes) {
if (targetClass == null || TextUtils.isEmpty(name)) return null;
if (targetClass == null || TextUtils.isEmpty(name)) {
return null;
}
try {
return targetClass.getMethod(name, parameterTypes);
} catch (SecurityException e) {
// ignore
} catch (NoSuchMethodException e) {
} catch (final SecurityException | NoSuchMethodException e) {
// ignore
}
return null;
}
public static Field getField(final Class<?> targetClass, final String name) {
if (targetClass == null || TextUtils.isEmpty(name)) return null;
if (targetClass == null || TextUtils.isEmpty(name)) {
return null;
}
try {
return targetClass.getField(name);
} catch (SecurityException e) {
// ignore
} catch (NoSuchFieldException e) {
} catch (final SecurityException | NoSuchFieldException e) {
// ignore
}
return null;
@ -65,22 +66,25 @@ public final class CompatUtils {
public static Constructor<?> getConstructor(final Class<?> targetClass,
final Class<?> ... types) {
if (targetClass == null || types == null) return null;
if (targetClass == null || types == null) {
return null;
}
try {
return targetClass.getConstructor(types);
} catch (SecurityException e) {
// ignore
} catch (NoSuchMethodException e) {
} catch (final SecurityException | NoSuchMethodException e) {
// ignore
}
return null;
}
public static Object newInstance(final Constructor<?> constructor, final Object ... args) {
if (constructor == null) return null;
if (constructor == null) {
return null;
}
try {
return constructor.newInstance(args);
} catch (Exception e) {
} catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Log.e(TAG, "Exception in newInstance", e);
}
return null;
@ -88,10 +92,13 @@ public final class CompatUtils {
public static Object invoke(final Object receiver, final Object defaultValue,
final Method method, final Object... args) {
if (method == null) return defaultValue;
if (method == null) {
return defaultValue;
}
try {
return method.invoke(receiver, args);
} catch (Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Log.e(TAG, "Exception in invoke", e);
}
return defaultValue;
@ -99,20 +106,24 @@ public final class CompatUtils {
public static Object getFieldValue(final Object receiver, final Object defaultValue,
final Field field) {
if (field == null) return defaultValue;
if (field == null) {
return defaultValue;
}
try {
return field.get(receiver);
} catch (Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException e) {
Log.e(TAG, "Exception in getFieldValue", e);
}
return defaultValue;
}
public static void setFieldValue(final Object receiver, final Field field, final Object value) {
if (field == null) return;
if (field == null) {
return;
}
try {
field.set(receiver, value);
} catch (Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException e) {
Log.e(TAG, "Exception in setFieldValue", e);
}
}

View file

@ -24,7 +24,7 @@ public final class DownloadManagerCompatUtils {
// DownloadManager.Request#setAllowedOverMetered() has been introduced
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
private static final Method METHOD_setAllowedOverMetered = CompatUtils.getMethod(
DownloadManager.Request.class, "setAllowedOverMetered", Boolean.TYPE);
DownloadManager.Request.class, "setAllowedOverMetered", boolean.class);
public static DownloadManager.Request setAllowedOverMetered(
final DownloadManager.Request request, final boolean allowOverMetered) {

View file

@ -26,7 +26,7 @@ public final class InputMethodManagerCompatWrapper {
// Note that InputMethodManager.switchToNextInputMethod() has been introduced
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod(
InputMethodManager.class, "switchToNextInputMethod", IBinder.class, Boolean.TYPE);
InputMethodManager.class, "switchToNextInputMethod", IBinder.class, boolean.class);
// Note that InputMethodManager.shouldOfferSwitchingToNextInputMethod() has been introduced
// in API level 19 (Build.VERSION_CODES.KITKAT).

View file

@ -32,8 +32,8 @@ public final class InputMethodSubtypeCompatUtils {
// has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
private static final Constructor<?> CONSTRUCTOR_INPUT_METHOD_SUBTYPE =
CompatUtils.getConstructor(InputMethodSubtype.class,
Integer.TYPE, Integer.TYPE, String.class, String.class, String.class,
Boolean.TYPE, Boolean.TYPE, Integer.TYPE);
int.class, int.class, String.class, String.class, String.class, boolean.class,
boolean.class, int.class);
static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null) {

View file

@ -34,9 +34,9 @@ import java.util.ArrayList;
public final class SuggestionSpanUtils {
// Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced
// in API level 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1).
public static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
private static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
private static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
static {

View file

@ -26,7 +26,7 @@ public final class UserDictionaryCompatUtils {
// UserDictionary.Words#addWord(Context, String, int, String, Locale) was introduced
// in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord",
Context.class, String.class, Integer.TYPE, String.class, Locale.class);
Context.class, String.class, int.class, String.class, Locale.class);
@SuppressWarnings("deprecation")
public static void addWord(final Context context, final String word,

View file

@ -30,7 +30,7 @@ public final class ViewCompatUtils {
View.class, "getPaddingEnd");
private static final Method METHOD_setPaddingRelative = CompatUtils.getMethod(
View.class, "setPaddingRelative",
Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE);
int.class, int.class, int.class, int.class);
private ViewCompatUtils() {
// This utility class is not publicly instantiable.

View file

@ -53,7 +53,7 @@ public class MoreKeySpecSplitTests extends InstrumentationTestCase {
private static String[] getAllResourceIdNames(final Class<?> resourceIdClass) {
final ArrayList<String> names = new ArrayList<>();
for (final Field field : resourceIdClass.getFields()) {
if (field.getType() == Integer.TYPE) {
if (field.getType() == int.class) {
names.add(field.getName());
}
}