View Javadoc

1   package com.atlassian.plugins.rest.common.util;
2   
3   import com.google.common.collect.Lists;
4   
5   import java.lang.reflect.Field;
6   import java.util.List;
7   import java.util.Arrays;
8   
9   /**
10   * A class to simplify some reflection calls.
11   */
12  public class ReflectionUtils
13  {
14      /**
15       * Gets the value of the {@link Field field} for the given object. It will change the accessibility of the field if necessary.
16       * Setting it back to its original value at the end of the method call.
17       * @param field the field to read from
18       * @param object the object to read the field from
19       * @return the value of the field.
20       */
21      public static Object getFieldValue(Field field, Object object)
22      {
23          final boolean accessible = field.isAccessible();
24          try
25          {
26              if (!accessible)
27              {
28                  field.setAccessible(true);
29              }
30              return field.get(object);
31          }
32          catch (IllegalAccessException e)
33          {
34              throw new RuntimeException("Could not access '" + field + "' from '" + object + "'", e);
35          }
36          finally
37          {
38              if (!accessible)
39              {
40                  field.setAccessible(false);
41              }
42          }
43      }
44  
45      /**
46       * Sets the value of the {@link Field field} for the given object. It will change the accessibility of the field if necessary.
47       * Setting it back to its original value at the end of the method call.
48       * @param field the field to set the value of
49       * @param object the object to for which to set the field value.
50       * @param value the new value to be set to the field of object.
51       */
52      public static void setFieldValue(Field field, Object object, Object value)
53      {
54          final boolean accessible = field.isAccessible();
55          try
56          {
57              if (!accessible)
58              {
59                  field.setAccessible(true);
60              }
61              field.set(object, value);
62          }
63          catch (IllegalAccessException e)
64          {
65              throw new RuntimeException("Could not access '" + field + "' from '" + object + "'", e);
66          }
67          finally
68          {
69              if (!accessible)
70              {
71                  field.setAccessible(false);
72              }
73          }
74      }
75  
76      /**
77       * Returns the result of running {@link Class#getDeclaredFields()} on the
78       * supplied class, as well as all its super types. Fields are ordered in
79       * ascending hierarchy order (subclasses first).
80       *
81       * @since v1.0.4
82       * @param clazz
83       * @return  all of the class's fields (including inherited fields).
84       */
85      public static List<Field> getDeclaredFields(Class clazz)
86      {
87          if (clazz == null)
88          {
89              return Lists.newArrayList();
90          }
91          else
92          {
93              final List<Field> superFields = getDeclaredFields(clazz.getSuperclass());
94              superFields.addAll(0, Arrays.asList(clazz.getDeclaredFields()));
95              return superFields;
96          }
97      }
98  }