1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.compat;
18 
19 import android.text.TextUtils;
20 import android.util.Log;
21 
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 
27 public final class CompatUtils {
28     private static final String TAG = CompatUtils.class.getSimpleName();
29 
CompatUtils()30     private CompatUtils() {
31         // This utility class is not publicly instantiable.
32     }
33 
getClass(final String className)34     public static Class<?> getClass(final String className) {
35         try {
36             return Class.forName(className);
37         } catch (final ClassNotFoundException e) {
38             return null;
39         }
40     }
41 
getMethod(final Class<?> targetClass, final String name, final Class<?>... parameterTypes)42     public static Method getMethod(final Class<?> targetClass, final String name,
43             final Class<?>... parameterTypes) {
44         if (targetClass == null || TextUtils.isEmpty(name)) {
45             return null;
46         }
47         try {
48             return targetClass.getMethod(name, parameterTypes);
49         } catch (final SecurityException | NoSuchMethodException e) {
50             // ignore
51         }
52         return null;
53     }
54 
getField(final Class<?> targetClass, final String name)55     public static Field getField(final Class<?> targetClass, final String name) {
56         if (targetClass == null || TextUtils.isEmpty(name)) {
57             return null;
58         }
59         try {
60             return targetClass.getField(name);
61         } catch (final SecurityException | NoSuchFieldException e) {
62             // ignore
63         }
64         return null;
65     }
66 
getConstructor(final Class<?> targetClass, final Class<?> ... types)67     public static Constructor<?> getConstructor(final Class<?> targetClass,
68             final Class<?> ... types) {
69         if (targetClass == null || types == null) {
70             return null;
71         }
72         try {
73             return targetClass.getConstructor(types);
74         } catch (final SecurityException | NoSuchMethodException e) {
75             // ignore
76         }
77         return null;
78     }
79 
newInstance(final Constructor<?> constructor, final Object ... args)80     public static Object newInstance(final Constructor<?> constructor, final Object ... args) {
81         if (constructor == null) {
82             return null;
83         }
84         try {
85             return constructor.newInstance(args);
86         } catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
87                 | InvocationTargetException e) {
88             Log.e(TAG, "Exception in newInstance", e);
89         }
90         return null;
91     }
92 
invoke(final Object receiver, final Object defaultValue, final Method method, final Object... args)93     public static Object invoke(final Object receiver, final Object defaultValue,
94             final Method method, final Object... args) {
95         if (method == null) {
96             return defaultValue;
97         }
98         try {
99             return method.invoke(receiver, args);
100         } catch (final IllegalAccessException | IllegalArgumentException
101                 | InvocationTargetException e) {
102             Log.e(TAG, "Exception in invoke", e);
103         }
104         return defaultValue;
105     }
106 
getFieldValue(final Object receiver, final Object defaultValue, final Field field)107     public static Object getFieldValue(final Object receiver, final Object defaultValue,
108             final Field field) {
109         if (field == null) {
110             return defaultValue;
111         }
112         try {
113             return field.get(receiver);
114         } catch (final IllegalAccessException | IllegalArgumentException e) {
115             Log.e(TAG, "Exception in getFieldValue", e);
116         }
117         return defaultValue;
118     }
119 
setFieldValue(final Object receiver, final Field field, final Object value)120     public static void setFieldValue(final Object receiver, final Field field, final Object value) {
121         if (field == null) {
122             return;
123         }
124         try {
125             field.set(receiver, value);
126         } catch (final IllegalAccessException | IllegalArgumentException e) {
127             Log.e(TAG, "Exception in setFieldValue", e);
128         }
129     }
130 
getClassWrapper(final String className)131     public static ClassWrapper getClassWrapper(final String className) {
132         return new ClassWrapper(getClass(className));
133     }
134 
135     public static final class ClassWrapper {
136         private final Class<?> mClass;
ClassWrapper(final Class<?> targetClass)137         public ClassWrapper(final Class<?> targetClass) {
138             mClass = targetClass;
139         }
140 
exists()141         public boolean exists() {
142             return mClass != null;
143         }
144 
getMethod(final String name, final T defaultValue, final Class<?>... parameterTypes)145         public <T> ToObjectMethodWrapper<T> getMethod(final String name,
146                 final T defaultValue, final Class<?>... parameterTypes) {
147             return new ToObjectMethodWrapper<>(CompatUtils.getMethod(mClass, name, parameterTypes),
148                     defaultValue);
149         }
150 
getPrimitiveMethod(final String name, final int defaultValue, final Class<?>... parameterTypes)151         public ToIntMethodWrapper getPrimitiveMethod(final String name, final int defaultValue,
152                 final Class<?>... parameterTypes) {
153             return new ToIntMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
154                     defaultValue);
155         }
156 
getPrimitiveMethod(final String name, final float defaultValue, final Class<?>... parameterTypes)157         public ToFloatMethodWrapper getPrimitiveMethod(final String name, final float defaultValue,
158                 final Class<?>... parameterTypes) {
159             return new ToFloatMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
160                     defaultValue);
161         }
162 
getPrimitiveMethod(final String name, final boolean defaultValue, final Class<?>... parameterTypes)163         public ToBooleanMethodWrapper getPrimitiveMethod(final String name,
164                 final boolean defaultValue, final Class<?>... parameterTypes) {
165             return new ToBooleanMethodWrapper(CompatUtils.getMethod(mClass, name, parameterTypes),
166                     defaultValue);
167         }
168     }
169 
170     public static final class ToObjectMethodWrapper<T> {
171         private final Method mMethod;
172         private final T mDefaultValue;
ToObjectMethodWrapper(final Method method, final T defaultValue)173         public ToObjectMethodWrapper(final Method method, final T defaultValue) {
174             mMethod = method;
175             mDefaultValue = defaultValue;
176         }
177         @SuppressWarnings("unchecked")
invoke(final Object receiver, final Object... args)178         public T invoke(final Object receiver, final Object... args) {
179             return (T) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
180         }
181     }
182 
183     public static final class ToIntMethodWrapper {
184         private final Method mMethod;
185         private final int mDefaultValue;
ToIntMethodWrapper(final Method method, final int defaultValue)186         public ToIntMethodWrapper(final Method method, final int defaultValue) {
187             mMethod = method;
188             mDefaultValue = defaultValue;
189         }
invoke(final Object receiver, final Object... args)190         public int invoke(final Object receiver, final Object... args) {
191             return (int) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
192         }
193     }
194 
195     public static final class ToFloatMethodWrapper {
196         private final Method mMethod;
197         private final float mDefaultValue;
ToFloatMethodWrapper(final Method method, final float defaultValue)198         public ToFloatMethodWrapper(final Method method, final float defaultValue) {
199             mMethod = method;
200             mDefaultValue = defaultValue;
201         }
invoke(final Object receiver, final Object... args)202         public float invoke(final Object receiver, final Object... args) {
203             return (float) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
204         }
205     }
206 
207     public static final class ToBooleanMethodWrapper {
208         private final Method mMethod;
209         private final boolean mDefaultValue;
ToBooleanMethodWrapper(final Method method, final boolean defaultValue)210         public ToBooleanMethodWrapper(final Method method, final boolean defaultValue) {
211             mMethod = method;
212             mDefaultValue = defaultValue;
213         }
invoke(final Object receiver, final Object... args)214         public boolean invoke(final Object receiver, final Object... args) {
215             return (boolean) CompatUtils.invoke(receiver, mDefaultValue, mMethod, args);
216         }
217     }
218 }
219