| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public final class ReflectionUtils { |
| 35 | 0 | private ReflectionUtils() { |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 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 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public static Type[] getGenericTypes(Field attribute) { |
| 60 | 11 | ParameterizedType type = (ParameterizedType) attribute.getGenericType(); |
| 61 | 11 | return type.getActualTypeArguments(); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public static Field getField(Object object, String attributeName) throws SecurityException, NoSuchFieldException { |
| 74 | 24 | return object.getClass().getDeclaredField(attributeName); |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 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 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 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 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 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 | |
} |