publicclassSpringReflectionUtils{privatestaticfinalLogger logger =LoggerFactory.getLogger(SpringReflectionUtils.class);/**
* Set the field represented by the supplied {@linkplain Field field object} on
* the specified {@linkplain Object target object} to the specified {@code value}.
* <p>In accordance with {@link Field#set(Object, Object)} semantics, the new value
* is automatically unwrapped if the underlying field has a primitive type.
* <p>This method does not support setting {@code static final} fields.
* <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}.
*
* @param fieldName the field to set
* @param target the target object on which to set the field
* (or {@code null} for a static field)
* @param value the value to set (may be {@code null})
*/publicstaticvoidsetField(String fieldName,Object target,Object value){Field field =ReflectionUtils.findField(target.getClass(), fieldName);Assert.notNull(target,"对象不能为空");if(field ==null){
logger.warn("未找到类[{}]的[{}]字段", target.getClass(), fieldName);return;}ReflectionUtils.makeAccessible(field);ReflectionUtils.setField(field, target, value);}/**
* 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
* (or {@code null} for a static field)
* @return the field's current value
*/@NullablepublicstaticObjectgetField(String fieldName,Object target){Field field =ReflectionUtils.findField(target.getClass(), fieldName);if(field ==null){returnnull;}ReflectionUtils.makeAccessible(field);returnReflectionUtils.getField(field, target);}}