View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package org.org.usurper.utils;
20  
21  import java.lang.reflect.Array;
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.Field;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.lang.reflect.ParameterizedType;
27  import java.lang.reflect.Type;
28  
29  /**
30   * This class is a reflection utility class
31   * 
32   * @author pagregoire
33   */
34  public final class ReflectionUtils {
35      private ReflectionUtils() {
36      }
37      
38      /**
39       * Gets the generic types for a given constructor argument (specified by its ordering number).
40       * 
41       * @param targetConstructor the target constructor
42       * @param constructorArgOrderingNumber the constructor arg ordering number
43       * 
44       * @return the generic types
45       */
46      public static Type[] getGenericTypes(Constructor<?> targetConstructor, Integer constructorArgOrderingNumber) {
47          Type[] genericParameterTypes = targetConstructor.getGenericParameterTypes();
48          ParameterizedType type = (ParameterizedType) genericParameterTypes[constructorArgOrderingNumber - 1];
49          return type.getActualTypeArguments();
50      }
51  
52      /**
53       * Gets the generic types for a given field.
54       * 
55       * @param attribute the attribute
56       * 
57       * @return the generic types
58       */
59      public static Type[] getGenericTypes(Field attribute) {
60          ParameterizedType type = (ParameterizedType) attribute.getGenericType();
61          return type.getActualTypeArguments();
62      }
63  
64      /**
65       * This method gets the Field from an Object and an attribute name.
66       * 
67       * @param object
68       * @param attributeName
69       * @return
70       * @throws SecurityException
71       * @throws NoSuchFieldException
72       */
73      public static Field getField(Object object, String attributeName) throws SecurityException, NoSuchFieldException {
74          return object.getClass().getDeclaredField(attributeName);
75      }
76  
77      /**
78       * This method sets a property on an object
79       * 
80       * @param object
81       * @param propertyName
82       * @param value
83       * @throws SecurityException
84       * @throws NoSuchFieldException
85       * @throws IllegalArgumentException
86       * @throws IllegalAccessException
87       */
88      public static void setProperty(Object object, String propertyName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
89          Field field = getField(object, propertyName);
90          field.setAccessible(true);
91          if (value.getClass().isArray()) {
92              Object[] valueArray = ((Object[]) value);
93              Object array = Array.newInstance(field.getType().getComponentType(), valueArray.length);
94              for (int i = 0; i < valueArray.length; i++) {
95                  Array.set(array, i, ((Object[]) value)[i]);
96              }
97              field.set(object, array);
98          } else {
99              field.set(object, value);
100         }
101     }
102 
103     /**
104      * This method invokes a property's setter on an object
105      * 
106      * @param object
107      * @param propertyDescriptor
108      * @param value
109      * @throws InvocationTargetException
110      * @throws IllegalAccessException
111      * @throws IllegalArgumentException
112      */
113     public static void setProperty(Object object, Method setter, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
114         if (setter != null) {
115             if (value.getClass().isArray()) {
116                 Object[] valueArray = ((Object[]) value);
117                 Class<?> componentType = setter.getParameterTypes()[0].getComponentType();
118                 Object array = Array.newInstance(componentType, valueArray.length);
119                 for (int i = 0; i < valueArray.length; i++) {
120                     Array.set(array, i, ((Object[]) value)[i]);
121                 }
122                 setter.invoke(object, array);
123             } else {
124                 setter.invoke(object, value);
125             }
126         }
127     }
128     /**
129      * Turns a primitive type to its Object counterpart.<br>
130      * Just returns the passed type as-is if it is not primitive. 
131      * @param usurpedClass
132      *            the usurped class
133      * 
134      * @return the not primitive type
135      */
136     public static Class<?> toNotPrimitiveType(Class<?> usurpedClass) {
137         Class<?> result = usurpedClass;
138         if (usurpedClass.isPrimitive()) {
139             if (usurpedClass == int.class) {
140                 result = Integer.class;
141             } else if (usurpedClass == float.class) {
142                 result = Float.class;
143             } else if (usurpedClass == long.class) {
144                 result = Long.class;
145             } else if (usurpedClass == double.class) {
146                 result = Double.class;
147             } else if (usurpedClass == short.class) {
148                 result = Short.class;
149             } else if (usurpedClass == boolean.class) {
150                 result = Boolean.class;
151             } else if (usurpedClass == byte.class) {
152                 result = Byte.class;
153             } else if (usurpedClass == char.class) {
154                 result = Character.class;
155             }
156         }
157         return result;
158     }
159 }