/src/main/java/com/google/ie/common/util/ClassUtility.java

http://thoughtsite.googlecode.com/ · Java · 111 lines · 44 code · 15 blank · 52 comment · 10 complexity · 82710dd1e20c06365a4364dd39156ecd MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.common.util;
  16. import java.lang.reflect.InvocationTargetException;
  17. import java.lang.reflect.Method;
  18. /**
  19. * Utility class for accessing value for object by using reflection.
  20. *
  21. * @author gmaurya
  22. *
  23. */
  24. public class ClassUtility {
  25. /**
  26. * Get the getter method names for attributes.
  27. *
  28. * @param fieldName the name of the field
  29. * @param fieldType the type of the field
  30. * @return String the name of the getter method for the field
  31. */
  32. private static <T> String getGetterMethodName(String fieldName, java.lang.Class<T> fieldType) {
  33. String firstStringChar = fieldName.substring(0, 1).toUpperCase();
  34. String remaingString = fieldName.substring(1);
  35. String methodName = null;
  36. /* In case the the field is a boolean */
  37. if (fieldType != null && fieldType == boolean.class
  38. || fieldType == Boolean.class) {
  39. methodName = "is" + firstStringChar + remaingString;
  40. } else {
  41. methodName = "get" + firstStringChar + remaingString;
  42. }
  43. return methodName;
  44. }
  45. /**
  46. * This method invoke the method and return the return value.
  47. *
  48. * @param object the object
  49. * @param fileName String
  50. * @return the result of dispatching the method represented by this object
  51. * on object
  52. *
  53. * @throws SecurityException
  54. * @throws NoSuchMethodException
  55. * @throws IllegalArgumentException
  56. * @throws IllegalAccessException
  57. * @throws InvocationTargetException
  58. */
  59. @SuppressWarnings("unchecked")
  60. public static Object getObject(Object object, String fileName)
  61. throws SecurityException, NoSuchMethodException,
  62. IllegalArgumentException, IllegalAccessException,
  63. InvocationTargetException {
  64. String methodName = getGetterMethodName(fileName, null);
  65. Method method = null;
  66. Class inputClass = object.getClass();
  67. method = getMethod(inputClass, methodName, null);
  68. return method.invoke(object, (Object[]) null);
  69. }
  70. /**
  71. * This method return the list of method of any class.
  72. *
  73. * @param clazz Class
  74. * @param methodName String
  75. * @param parameterTypes Class[]
  76. * @return Method
  77. * @throws SecurityException
  78. * @throws NoSuchMethodException
  79. */
  80. private static <T, Y> Method getMethod(Class<T> clazz, String methodName,
  81. Class<Y>[] parameterTypes) throws SecurityException,
  82. NoSuchMethodException {
  83. Method method = null;
  84. try {
  85. method = clazz.getMethod(methodName, parameterTypes);
  86. } catch (NoSuchMethodException e) {
  87. if (methodName.startsWith("get")) {
  88. methodName = methodName.replaceFirst("get", "is");
  89. } else if (methodName.startsWith("is")) {
  90. methodName = methodName.replaceFirst("get", "is");
  91. }
  92. method = clazz.getMethod(methodName, parameterTypes);
  93. }
  94. return method;
  95. }
  96. }