Coverage Report - org.org.usurper.utils.ReflectionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionUtils
96 %
47/49
93 %
26/28
0
 
 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  0
     private ReflectionUtils() {
 36  0
     }
 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  5
         Type[] genericParameterTypes = targetConstructor.getGenericParameterTypes();
 48  5
         ParameterizedType type = (ParameterizedType) genericParameterTypes[constructorArgOrderingNumber - 1];
 49  5
         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  11
         ParameterizedType type = (ParameterizedType) attribute.getGenericType();
 61  11
         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  24
         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  2
         Field field = getField(object, propertyName);
 90  2
         field.setAccessible(true);
 91  2
         if (value.getClass().isArray()) {
 92  1
             Object[] valueArray = ((Object[]) value);
 93  1
             Object array = Array.newInstance(field.getType().getComponentType(), valueArray.length);
 94  3
             for (int i = 0; i < valueArray.length; i++) {
 95  2
                 Array.set(array, i, ((Object[]) value)[i]);
 96  
             }
 97  1
             field.set(object, array);
 98  1
         } else {
 99  1
             field.set(object, value);
 100  
         }
 101  2
     }
 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  6975
         if (setter != null) {
 115  6975
             if (value.getClass().isArray()) {
 116  12
                 Object[] valueArray = ((Object[]) value);
 117  12
                 Class<?> componentType = setter.getParameterTypes()[0].getComponentType();
 118  12
                 Object array = Array.newInstance(componentType, valueArray.length);
 119  124
                 for (int i = 0; i < valueArray.length; i++) {
 120  112
                     Array.set(array, i, ((Object[]) value)[i]);
 121  
                 }
 122  12
                 setter.invoke(object, array);
 123  12
             } else {
 124  6963
                 setter.invoke(object, value);
 125  
             }
 126  
         }
 127  6975
     }
 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  11
         Class<?> result = usurpedClass;
 138  11
         if (usurpedClass.isPrimitive()) {
 139  8
             if (usurpedClass == int.class) {
 140  1
                 result = Integer.class;
 141  7
             } else if (usurpedClass == float.class) {
 142  1
                 result = Float.class;
 143  6
             } else if (usurpedClass == long.class) {
 144  1
                 result = Long.class;
 145  5
             } else if (usurpedClass == double.class) {
 146  1
                 result = Double.class;
 147  4
             } else if (usurpedClass == short.class) {
 148  1
                 result = Short.class;
 149  3
             } else if (usurpedClass == boolean.class) {
 150  1
                 result = Boolean.class;
 151  2
             } else if (usurpedClass == byte.class) {
 152  1
                 result = Byte.class;
 153  1
             } else if (usurpedClass == char.class) {
 154  1
                 result = Character.class;
 155  
             }
 156  
         }
 157  11
         return result;
 158  
     }
 159  
 }