| 1 |
/* |
| 2 |
* @(#)ReflectionUtils.java 3/23/2011 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2011 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.utils; |
| 8 |
|
| 9 |
import java.lang.reflect.Constructor; |
| 10 |
import java.lang.reflect.Method; |
| 11 |
|
| 12 |
/** |
| 13 |
* Utility class to use reflection to call methods. |
| 14 |
*/ |
| 15 |
public class ReflectionUtils { |
| 16 |
/** |
| 17 |
* Helper method to call a set boolean method. |
| 18 |
* |
| 19 |
* @param thisObject the instance |
| 20 |
* @param methodName the method name |
| 21 |
* @param value true or false |
| 22 |
* |
| 23 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 24 |
*/ |
| 25 |
public static void callSetBoolean(Object thisObject, String methodName, boolean value) throws Exception { |
| 26 |
Class<?>[] argTypes = new Class<?>[]{boolean.class}; |
| 27 |
Object[] args = new Object[]{value ? Boolean.TRUE : Boolean.FALSE}; |
| 28 |
Method method = thisObject.getClass().getMethod(methodName, argTypes); |
| 29 |
method.invoke(thisObject, args); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Helper method to call a set boolean method. |
| 34 |
* |
| 35 |
* @param thisObject the instance |
| 36 |
* @param methodName the method name |
| 37 |
* @param value the value |
| 38 |
* |
| 39 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 40 |
*/ |
| 41 |
public static void callSetInt(Object thisObject, String methodName, int value) throws Exception { |
| 42 |
Class<?>[] argTypes = new Class<?>[]{int.class}; |
| 43 |
Object[] args = new Object[]{value}; |
| 44 |
Method method = thisObject.getClass().getMethod(methodName, argTypes); |
| 45 |
method.invoke(thisObject, args); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Helper method to call a set boolean method. |
| 50 |
* |
| 51 |
* @param thisObject the instance |
| 52 |
* @param methodName the method name |
| 53 |
* @param value the value |
| 54 |
* |
| 55 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 56 |
*/ |
| 57 |
public static void callSet(Object thisObject, String methodName, Object value) |
| 58 |
throws Exception { |
| 59 |
Class<?>[] argTypes = new Class<?>[]{value.getClass()}; |
| 60 |
Object[] args = new Object[]{value}; |
| 61 |
Method method = thisObject.getClass().getMethod(methodName, argTypes); |
| 62 |
method.invoke(thisObject, args); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Helper method to call a get method with no argument. |
| 67 |
* |
| 68 |
* @param thisObject the instance |
| 69 |
* @param methodName the method name |
| 70 |
* |
| 71 |
* @return the value the method returns. |
| 72 |
* |
| 73 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 74 |
*/ |
| 75 |
public static Object callGet(Object thisObject, String methodName) throws Exception { |
| 76 |
Method method = thisObject.getClass().getMethod(methodName, (Class[]) null); |
| 77 |
return method.invoke(thisObject, (Object[]) null); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Helper method to call a no argument, no return method. |
| 82 |
* |
| 83 |
* @param thisObject the instance |
| 84 |
* @param methodName the method name |
| 85 |
* |
| 86 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 87 |
*/ |
| 88 |
public static void call(Object thisObject, String methodName) throws Exception { |
| 89 |
Method method = thisObject.getClass().getMethod(methodName, (Class[]) null); |
| 90 |
method.invoke(thisObject, (Object[]) null); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Helper method to call a constructor. |
| 95 |
* |
| 96 |
* @param clazz the class |
| 97 |
* @param argTypes argument Classes for constructor lookup. Must not be null. |
| 98 |
* @param args the argument array |
| 99 |
* |
| 100 |
* @return the value the method returns. |
| 101 |
* |
| 102 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 103 |
*/ |
| 104 |
public static Object callConstructor(Class<?> clazz, Class<?>[] argTypes, Object[] args) throws Exception { |
| 105 |
Constructor constructor = clazz.getConstructor(argTypes); |
| 106 |
return constructor.newInstance(args); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Helper method to call a multi-argument method having a return. The class types will be derived from the input |
| 111 |
* values. This call is usually successful with primitive types or Strings as arguments, but care should be used |
| 112 |
* with other kinds of values. The constructor lookup is not polymorphic. |
| 113 |
* <p/> |
| 114 |
* Calls <code>callAny(Object, methodName, argTypes, args)</code>. |
| 115 |
* |
| 116 |
* @param thisObject the instance |
| 117 |
* @param methodName the method name |
| 118 |
* @param args the argument array, must not contain null. |
| 119 |
* |
| 120 |
* @return the value the method returns. |
| 121 |
* |
| 122 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 123 |
*/ |
| 124 |
public static Object callAny(Object thisObject, String methodName, Object[] args) throws Exception { |
| 125 |
Class<?>[] argTypes = null; |
| 126 |
if (args != null) { |
| 127 |
argTypes = new Class[args.length]; |
| 128 |
for (int i = 0; i < args.length; i++) { |
| 129 |
argTypes[i] = args[i].getClass(); |
| 130 |
} |
| 131 |
} |
| 132 |
return callAny(thisObject, methodName, argTypes, args); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Helper method to call a multi-argument method having a return. |
| 137 |
* |
| 138 |
* @param thisObject the instance |
| 139 |
* @param methodName the method name |
| 140 |
* @param argTypes argument Classes for constructor lookup. Must not be null. |
| 141 |
* @param args the argument array |
| 142 |
* |
| 143 |
* @return the value the method returns. |
| 144 |
* |
| 145 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 146 |
*/ |
| 147 |
public static Object callAny(Object thisObject, String methodName, Class<?>[] argTypes, Object[] args) throws Exception { |
| 148 |
Method method = thisObject.getClass().getMethod(methodName, argTypes); |
| 149 |
return method.invoke(thisObject, args); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Helper method to call a multi-argument method having a return without throwing an exception. |
| 154 |
* |
| 155 |
* @param thisObject the instance |
| 156 |
* @param objectClass the class which could find the method name |
| 157 |
* @param methodName the method name |
| 158 |
* @param argTypes argument Classes for constructor lookup. Must not be null. |
| 159 |
* @param args the argument array |
| 160 |
* |
| 161 |
* @return the value the method returns. |
| 162 |
*/ |
| 163 |
public static Object callAnyWithoutException(Object thisObject, Class<?> objectClass, String methodName, Class<?>[] argTypes, Object[] args) { |
| 164 |
Method m; |
| 165 |
try { |
| 166 |
m = objectClass.getDeclaredMethod(methodName, argTypes); |
| 167 |
} |
| 168 |
catch (Exception e) { |
| 169 |
m = null; |
| 170 |
} |
| 171 |
if (m != null) { |
| 172 |
try { |
| 173 |
m.setAccessible(true); |
| 174 |
return m.invoke(thisObject, args); |
| 175 |
} |
| 176 |
catch (Exception ignored) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
finally { |
| 180 |
m.setAccessible(false); |
| 181 |
} |
| 182 |
} |
| 183 |
return null; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Helper method to call a multi-argument static class method having a return. |
| 188 |
* |
| 189 |
* @param thisClass the class |
| 190 |
* @param methodName the method name |
| 191 |
* @param argTypes argument Classes for constructor lookup. Must not be null. |
| 192 |
* @param args the argument array |
| 193 |
* |
| 194 |
* @return the value the method returns. |
| 195 |
* |
| 196 |
* @throws Exception if the method is not found or invocation to the method fails. |
| 197 |
*/ |
| 198 |
public static Object callStatic(Class<?> thisClass, String methodName, Class<?>[] argTypes, Object[] args) throws Exception { |
| 199 |
Method method = thisClass.getMethod(methodName, argTypes); |
| 200 |
Object result = null; |
| 201 |
if (method != null) { |
| 202 |
try { |
| 203 |
method.setAccessible(true); |
| 204 |
result = method.invoke(null, args); |
| 205 |
} |
| 206 |
finally { |
| 207 |
method.setAccessible(false); |
| 208 |
} |
| 209 |
} |
| 210 |
return result; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Instantiate an object based on the class name. |
| 215 |
* |
| 216 |
* @param className the class name |
| 217 |
* @param types the class types for the constructor |
| 218 |
* @param args the constructor values |
| 219 |
* |
| 220 |
* @return the object instance. null if any exception occurs. |
| 221 |
*/ |
| 222 |
public static Object createInstance(String className, Class<?>[] types, Object[] args) { |
| 223 |
Object instantiation = null; |
| 224 |
// try default class |
| 225 |
try { |
| 226 |
Class<?> cls = Class.forName(className); |
| 227 |
if (types != null && types.length != 0) { |
| 228 |
Constructor<?> constructor = cls.getConstructor(types); |
| 229 |
instantiation = constructor.newInstance(args); |
| 230 |
} |
| 231 |
else { |
| 232 |
instantiation = cls.newInstance(); |
| 233 |
} |
| 234 |
} |
| 235 |
catch (Exception e) { |
| 236 |
// null |
| 237 |
} |
| 238 |
|
| 239 |
return instantiation; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Checks if the instance o is an instance of a subclass of the designated class name. |
| 244 |
* |
| 245 |
* @param o the instance to check |
| 246 |
* @param className the class name to check |
| 247 |
* |
| 248 |
* @return true if the instance o is an instance of a subclass of the class name. Otherwise false. |
| 249 |
* |
| 250 |
* @since 3.4.9 |
| 251 |
*/ |
| 252 |
public static boolean isSubClassOf(Object o, String className) { |
| 253 |
if (o == null) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
Class<?> aClass = o.getClass(); |
| 257 |
while (aClass != null) { |
| 258 |
if (aClass.getName().contains(className)) { |
| 259 |
return true; |
| 260 |
} |
| 261 |
aClass = aClass.getSuperclass(); |
| 262 |
} |
| 263 |
return false; |
| 264 |
} |
| 265 |
} |