/** * create a new instance by the given constructor. */ public static <T> T newInstance(Constructor<T> constructor,Object... args){ try { return constructor.newInstance(args); } catch (Exception e) { handleException(e); return null; } }
protected static Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException { try { return method.invoke(target, args); } catch (InvocationTargetException ex) { Throwable targetEx = ex.getTargetException(); if (targetEx instanceof IOException) { throw (IOException) targetEx; } Reflection.handleException(ex); } catch (Exception ex) { Reflection.handleException(ex); } throw new IllegalStateException("Invalid code path reached"); }
/** * Get the field represented by the supplied {@link Field field object} on the * specified {@link Object target object}. In accordance with {@link Field#get(Object)} * semantics, the returned value is automatically wrapped if the underlying field * has a primitive type. * * @param field the field to get * @param target the target object from which to get the field * @return the field's current value */ public static Object getFieldValue(Object target,Field field) { try { return field.get(target); } catch (IllegalAccessException ex) { handleException(ex); throw new ReflectException("Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
/** * Invoke the specified {@link Method} against the supplied target object with the supplied Args. The target * object can be <code>null</code> when invoking a static {@link Method}. * * @param method the method to invoke * @param target the target object to invoke the method on * @param args the invocation Arguments (may be <code>null</code>) * @return the invocation result, if any */ public static Object invokeMethod(Method method, Object target, Object... args) { try { if(!method.isAccessible()){ method.setAccessible(true); } return method.invoke(target, args); } catch (Exception ex) { handleException(ex); throw new ReflectException(ex.getMessage(), ex); } }