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