PageRenderTime 46ms CodeModel.GetById 19ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/utils/src/com/googlecode/eyesfree/utils/compat/CompatUtils.java

http://eyes-free.googlecode.com/
Java | 113 lines | 80 code | 12 blank | 21 comment | 18 complexity | 69324ce939722550f26a88facea739d8 MD5 | raw file
  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
 17package com.googlecode.eyesfree.utils.compat;
 18
 19import android.text.TextUtils;
 20import android.util.Log;
 21
 22import java.lang.reflect.Constructor;
 23import java.lang.reflect.Field;
 24import java.lang.reflect.Method;
 25
 26public class CompatUtils {
 27    private static final String TAG = CompatUtils.class.getSimpleName();
 28
 29    public static Class<?> getClass(String className) {
 30        try {
 31            return Class.forName(className);
 32        } catch (ClassNotFoundException e) {
 33            return null;
 34        }
 35    }
 36
 37    public static Method getMethod(Class<?> targetClass, String name,
 38            Class<?>... parameterTypes) {
 39        if (targetClass == null || TextUtils.isEmpty(name)) return null;
 40        try {
 41            return targetClass.getMethod(name, parameterTypes);
 42        } catch (SecurityException e) {
 43            // ignore
 44        } catch (NoSuchMethodException e) {
 45            // ignore
 46        }
 47        return null;
 48    }
 49
 50    public static Field getField(Class<?> targetClass, String name) {
 51        if (targetClass == null || TextUtils.isEmpty(name)) return null;
 52        try {
 53            return targetClass.getField(name);
 54        } catch (SecurityException e) {
 55            // ignore
 56        } catch (NoSuchFieldException e) {
 57            // ignore
 58        }
 59        return null;
 60    }
 61
 62    public static Constructor<?> getConstructor(Class<?> targetClass, Class<?> ... types) {
 63        if (targetClass == null || types == null) return null;
 64        try {
 65            return targetClass.getConstructor(types);
 66        } catch (SecurityException e) {
 67            // ignore
 68        } catch (NoSuchMethodException e) {
 69            // ignore
 70        }
 71        return null;
 72    }
 73
 74    public static Object newInstance(Constructor<?> constructor, Object ... args) {
 75        if (constructor == null) return null;
 76        try {
 77            return constructor.newInstance(args);
 78        } catch (Exception e) {
 79            Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName());
 80        }
 81        return null;
 82    }
 83
 84    public static Object invoke(
 85            Object receiver, Object defaultValue, Method method, Object... args) {
 86        if (method == null) return defaultValue;
 87        try {
 88            return method.invoke(receiver, args);
 89        } catch (Exception e) {
 90            Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
 91        }
 92        return defaultValue;
 93    }
 94
 95    public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
 96        if (field == null) return defaultValue;
 97        try {
 98            return field.get(receiver);
 99        } catch (Exception e) {
100            Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName());
101        }
102        return defaultValue;
103    }
104
105    public static void setFieldValue(Object receiver, Field field, Object value) {
106        if (field == null) return;
107        try {
108            field.set(receiver, value);
109        } catch (Exception e) {
110            Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
111        }
112    }
113}