aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/compat/CompatUtils.java
diff options
context:
space:
mode:
authorYohei Yukawa <yukawa@google.com>2014-08-20 08:48:42 +0900
committerYohei Yukawa <yukawa@google.com>2014-08-20 09:27:22 +0900
commitfa39ffcf8461ad6e53a86d8957ab892a3e367d16 (patch)
tree34d9914c15b038411fbbdbd759777316cf9d34e1 /java/src/com/android/inputmethod/compat/CompatUtils.java
parent23f41049d69b17402d91506ef1283d7eb48ba430 (diff)
downloadlatinime-fa39ffcf8461ad6e53a86d8957ab892a3e367d16.tar.gz
latinime-fa39ffcf8461ad6e53a86d8957ab892a3e367d16.tar.xz
latinime-fa39ffcf8461ad6e53a86d8957ab892a3e367d16.zip
Support more methods in the wrapper of CursorAnchorInfo
This CL add more compatibility wrapper methods for CursorAnchorInfo. This CL also adds more utility functions and types into CompatUtils to reduce explict cast operations. Change-Id: Id50165b552bbf28b832a6da13bf06eedcd2a190e
Diffstat (limited to '')
-rw-r--r--java/src/com/android/inputmethod/compat/CompatUtils.java88
1 files changed, 88 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java
index 4fd2a6936..6aa2736c1 100644
--- a/java/src/com/android/inputmethod/compat/CompatUtils.java
+++ b/java/src/com/android/inputmethod/compat/CompatUtils.java
@@ -127,4 +127,92 @@ public final class CompatUtils {
Log.e(TAG, "Exception in setFieldValue", e);
}
}
+
+ public static ClassWrapper getClassWrapper(final String className) {
+ return new ClassWrapper(getClass(className));
+ }
+
+ public static final class ClassWrapper {
+ private final Class<?> mClass;
+ public ClassWrapper(final Class<?> targetClass) {
+ mClass = targetClass;
+ }
+
+ public boolean exists() {
+ return mClass != null;
+ }
+
+ public <T> ToObjectMethodWrapper<T> getMethod(final String name,
+ final T defaultValue, final Class<?>... parameterTypes) {
+ return new ToObjectMethodWrapper<T>(CompatUtils.getMethod(mClass, name, parameterTypes),
+ defaultValue);
+ }
+
+ public ToIntMethodWrapper getPrimitiveMethod(final String name, final int defaultValue,
+ final Class<?>... parameterTypes) {
+ return new ToIntMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
+ defaultValue);
+ }
+
+ public ToFloatMethodWrapper getPrimitiveMethod(final String name, final float defaultValue,
+ final Class<?>... parameterTypes) {
+ return new ToFloatMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
+ defaultValue);
+ }
+
+ public ToBooleanMethodWrapper getPrimitiveMethod(final String name,
+ final boolean defaultValue, final Class<?>... parameterTypes) {
+ return new ToBooleanMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
+ defaultValue);
+ }
+ }
+
+ public static final class ToObjectMethodWrapper<T> {
+ private final Method mMethod;
+ private final T mDefaultValue;
+ public ToObjectMethodWrapper(final Method method, final T defaultValue) {
+ mMethod = method;
+ mDefaultValue = defaultValue;
+ }
+ @SuppressWarnings("unchecked")
+ public T invoke(final Object receiver, final Object... args) {
+ return (T) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
+ }
+ }
+
+ public static final class ToIntMethodWrapper {
+ private final Method mMethod;
+ private final int mDefaultValue;
+ public ToIntMethodWrapper(final Method method, final int defaultValue) {
+ mMethod = method;
+ mDefaultValue = defaultValue;
+ }
+ public int invoke(final Object receiver, final Object... args) {
+ return (int) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
+ }
+ }
+
+ public static final class ToFloatMethodWrapper {
+ private final Method mMethod;
+ private final float mDefaultValue;
+ public ToFloatMethodWrapper(final Method method, final float defaultValue) {
+ mMethod = method;
+ mDefaultValue = defaultValue;
+ }
+ public float invoke(final Object receiver, final Object... args) {
+ return (float) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
+ }
+ }
+
+ public static final class ToBooleanMethodWrapper {
+ private final Method mMethod;
+ private final boolean mDefaultValue;
+ public ToBooleanMethodWrapper(final Method method, final boolean defaultValue) {
+ mMethod = method;
+ mDefaultValue = defaultValue;
+ }
+ public boolean invoke(final Object receiver, final Object... args) {
+ return (boolean) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
+ }
+ }
}