/** * Invoke the specified {@link Method} against the supplied target object with the * supplied arguments. The target object can be {@code null} when invoking a * static {@link Method}. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException}. * @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}) * @return the invocation result, if any */ @Nullable public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) { try { return method.invoke(target, args); } catch (Exception ex) { handleReflectionException(ex); } throw new IllegalStateException("Should never get here"); }
/** * Set the field represented by the supplied {@link Field field object} on the * specified {@link Object target object} to the specified {@code value}. * In accordance with {@link Field#set(Object, Object)} semantics, the new value * is automatically unwrapped if the underlying field has a primitive type. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}. * @param field the field to set * @param target the target object on which to set the field * @param value the value to set (may be {@code null}) */ public static void setField(Field field, @Nullable Object target, @Nullable Object value) { try { field.set(target, value); } catch (IllegalAccessException ex) { handleReflectionException(ex); throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
/** * 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. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}. * @param field the field to get * @param target the target object from which to get the field * @return the field's current value */ @Nullable public static Object getField(Field field, @Nullable Object target) { try { return field.get(target); } catch (IllegalAccessException ex) { handleReflectionException(ex); throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
protected static Object invokeVfsMethod(Method method, @Nullable 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; } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } throw new IllegalStateException("Invalid code path reached"); }
@Nullable private static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args) throws SQLException { try { return method.invoke(target, args); } catch (IllegalAccessException ex) { ReflectionUtils.handleReflectionException(ex); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof SQLException) { throw (SQLException) ex.getTargetException(); } ReflectionUtils.handleInvocationTargetException(ex); } throw new IllegalStateException("Should never get here"); }
public static Object getProtectedFieldValue(String protectedField, Object object) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); return field.get(object); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); return null; // unreachable - previous line throws exception } }
public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); field.set(object, newValue); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } }
@SuppressWarnings("unchecked") private T getBean() { if (name != null) { return (T) beanFactory.getBean(name); } try { return type.newInstance(); } catch (InstantiationException | IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } // should not happen throw new IllegalStateException("Internal error: could not create bean instance for mapping."); }
/** * Invoke the specified {@link Method} against the supplied target object with the * supplied arguments. The target object can be {@code null} when invoking a * static {@link Method}. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException}. * @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}) * @return the invocation result, if any */ @Nullable public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) { try { return method.invoke(target, args); } catch (Exception ex) { handleReflectionException(ex); } throw new IllegalStateException("Should never get here"); }
/** * Set the field represented by the supplied {@link Field field object} on the * specified {@link Object target object} to the specified {@code value}. * In accordance with {@link Field#set(Object, Object)} semantics, the new value * is automatically unwrapped if the underlying field has a primitive type. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}. * @param field the field to set * @param target the target object on which to set the field * @param value the value to set (may be {@code null}) */ public static void setField(Field field, @Nullable Object target, @Nullable Object value) { try { field.set(target, value); } catch (IllegalAccessException ex) { handleReflectionException(ex); throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
/** * 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. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}. * @param field the field to get * @param target the target object from which to get the field * @return the field's current value */ @Nullable public static Object getField(Field field, @Nullable Object target) { try { return field.get(target); } catch (IllegalAccessException ex) { handleReflectionException(ex); throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
protected static Object invokeVfsMethod(Method method, @Nullable 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; } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } throw new IllegalStateException("Invalid code path reached"); }
public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); field.set(object, newValue); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } }
public static Object getProtectedFieldValue(String protectedField, Object object) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); return field.get(object); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); return null; // unreachable - previous line throws exception } }
/** * Invoke the specified JDBC API {@link Method} against the supplied target * object with the supplied arguments. * @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}) * @return the invocation result, if any * @throws SQLException the JDBC API SQLException to rethrow (if any) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) * @deprecated as of 5.0.11, in favor of custom SQLException handling */ @Deprecated @Nullable public static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args) throws SQLException { try { return method.invoke(target, args); } catch (IllegalAccessException ex) { handleReflectionException(ex); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof SQLException) { throw (SQLException) ex.getTargetException(); } handleInvocationTargetException(ex); } throw new IllegalStateException("Should never get here"); }
private static void initialize() { if (!StringUtils.hasText(strategyName)) { // Set default strategyName = MODE_THREADLOCAL; } if (strategyName.equals(MODE_THREADLOCAL)) { strategy = new ThreadLocalSecurityContextHolderStrategy(); } else if (strategyName.equals(MODE_INHERITABLETHREADLOCAL)) { strategy = new InheritableThreadLocalSecurityContextHolderStrategy(); } else if (strategyName.equals(MODE_GLOBAL)) { strategy = new GlobalSecurityContextHolderStrategy(); } else { // Try to load a custom strategy try { Class<?> clazz = Class.forName(strategyName); Constructor<?> customStrategy = clazz.getConstructor(); strategy = (SecurityContextHolderStrategy) customStrategy.newInstance(); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } initializeCount++; }
ReflectionUtils.handleReflectionException(ex); throw new IllegalStateException("Should never get here");
private static void initialize() { if (!StringUtils.hasText(strategyName)) { // Set default strategyName = MODE_THREADLOCAL; } if (strategyName.equals(MODE_THREADLOCAL)) { strategy = new ThreadLocalSecurityContextHolderStrategy(); } else if (strategyName.equals(MODE_INHERITABLETHREADLOCAL)) { strategy = new InheritableThreadLocalSecurityContextHolderStrategy(); } else if (strategyName.equals(MODE_GLOBAL)) { strategy = new GlobalSecurityContextHolderStrategy(); } else { // Try to load a custom strategy try { Class<?> clazz = Class.forName(strategyName); Constructor<?> customStrategy = clazz.getConstructor(); strategy = (SecurityContextHolderStrategy) customStrategy.newInstance(); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } initializeCount++; }
private Object getPropertyValue(Object bean, String nestedName) { BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); wrapper.setAutoGrowNestedPaths(true); Object nestedValue = wrapper.getPropertyValue(nestedName); if (nestedValue == null) { try { nestedValue = wrapper.getPropertyType(nestedName).newInstance(); wrapper.setPropertyValue(nestedName, nestedValue); } catch (InstantiationException | IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } } return nestedValue; }
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; } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } throw new IllegalStateException("Invalid code path reached"); }