Home | History | Annotate | Download | only in compat
      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.Method;
     25 
     26 public final class CompatUtils {
     27     private static final String TAG = CompatUtils.class.getSimpleName();
     28 
     29     private CompatUtils() {
     30         // This utility class is not publicly instantiable.
     31     }
     32 
     33     public static Class<?> getClass(final String className) {
     34         try {
     35             return Class.forName(className);
     36         } catch (ClassNotFoundException e) {
     37             return null;
     38         }
     39     }
     40 
     41     public static Method getMethod(final Class<?> targetClass, final String name,
     42             final Class<?>... parameterTypes) {
     43         if (targetClass == null || TextUtils.isEmpty(name)) return null;
     44         try {
     45             return targetClass.getMethod(name, parameterTypes);
     46         } catch (SecurityException e) {
     47             // ignore
     48         } catch (NoSuchMethodException e) {
     49             // ignore
     50         }
     51         return null;
     52     }
     53 
     54     public static Field getField(final Class<?> targetClass, final String name) {
     55         if (targetClass == null || TextUtils.isEmpty(name)) return null;
     56         try {
     57             return targetClass.getField(name);
     58         } catch (SecurityException e) {
     59             // ignore
     60         } catch (NoSuchFieldException e) {
     61             // ignore
     62         }
     63         return null;
     64     }
     65 
     66     public static Constructor<?> getConstructor(final Class<?> targetClass,
     67             final Class<?> ... types) {
     68         if (targetClass == null || types == null) return null;
     69         try {
     70             return targetClass.getConstructor(types);
     71         } catch (SecurityException e) {
     72             // ignore
     73         } catch (NoSuchMethodException e) {
     74             // ignore
     75         }
     76         return null;
     77     }
     78 
     79     public static Object newInstance(final Constructor<?> constructor, final Object ... args) {
     80         if (constructor == null) return null;
     81         try {
     82             return constructor.newInstance(args);
     83         } catch (Exception e) {
     84             Log.e(TAG, "Exception in newInstance", e);
     85         }
     86         return null;
     87     }
     88 
     89     public static Object invoke(final Object receiver, final Object defaultValue,
     90             final Method method, final Object... args) {
     91         if (method == null) return defaultValue;
     92         try {
     93             return method.invoke(receiver, args);
     94         } catch (Exception e) {
     95             Log.e(TAG, "Exception in invoke", e);
     96         }
     97         return defaultValue;
     98     }
     99 
    100     public static Object getFieldValue(final Object receiver, final Object defaultValue,
    101             final Field field) {
    102         if (field == null) return defaultValue;
    103         try {
    104             return field.get(receiver);
    105         } catch (Exception e) {
    106             Log.e(TAG, "Exception in getFieldValue", e);
    107         }
    108         return defaultValue;
    109     }
    110 
    111     public static void setFieldValue(final Object receiver, final Field field, final Object value) {
    112         if (field == null) return;
    113         try {
    114             field.set(receiver, value);
    115         } catch (Exception e) {
    116             Log.e(TAG, "Exception in setFieldValue", e);
    117         }
    118     }
    119 }
    120