List of usage examples for org.springframework.util ReflectionUtils getField
@Nullable public static Object getField(Field field, @Nullable Object target)
From source file:org.springframework.data.solr.server.support.SolrServerUtils.java
/** * Solr property names do not match the getters/setters used for them. Check on any write method, try to find the * according property and set the value for it. Will ignore all other, and nested properties * //from ww w. j a va 2 s . com * @param source * @param target */ private static void copyProperties(SolrServer source, SolrServer target) { BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source); for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) { Method writer = pd.getWriteMethod(); if (writer != null) { try { Field property = ReflectionUtils.findField(source.getClass(), pd.getName()); if (property != null) { ReflectionUtils.makeAccessible(property); Object o = ReflectionUtils.getField(property, source); if (o != null) { writer.invoke(target, o); } } } catch (Exception e) { logger.warn("Could not copy property value for: " + pd.getName(), e); } } } }
From source file:org.springframework.integration.config.IdGeneratorConfigurer.java
private boolean setIdGenerator(ApplicationContext context) { try {//w w w. j av a2 s. com IdGenerator idGeneratorBean = context.getBean(IdGenerator.class); if (logger.isDebugEnabled()) { logger.debug("using custom MessageHeaders.IdGenerator [" + idGeneratorBean.getClass() + "]"); } Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); IdGenerator currentIdGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null); if (currentIdGenerator != null) { if (currentIdGenerator.equals(idGeneratorBean)) { // same instance is already set, nothing needs to be done return false; } else { if (IdGeneratorConfigurer.theIdGenerator.getClass() == idGeneratorBean.getClass()) { if (logger.isWarnEnabled()) { logger.warn("Another instance of " + idGeneratorBean.getClass() + " has already been established; ignoring"); } return true; } else { // different instance has been set, not legal throw new BeanDefinitionStoreException( "'MessageHeaders.idGenerator' has already been set and can not be set again"); } } } if (logger.isInfoEnabled()) { logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass() + "]"); } ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean); IdGeneratorConfigurer.theIdGenerator = idGeneratorBean; } catch (NoSuchBeanDefinitionException e) { // No custom IdGenerator. We will use the default. int idBeans = context.getBeansOfType(IdGenerator.class).size(); if (idBeans > 1 && logger.isWarnEnabled()) { logger.warn("Found too many 'IdGenerator' beans (" + idBeans + ") " + "Will use the existing UUID strategy."); } else if (logger.isDebugEnabled()) { logger.debug("Unable to locate MessageHeaders.IdGenerator. Will use the existing UUID strategy."); } return false; } catch (IllegalStateException e) { // thrown from ReflectionUtils if (logger.isWarnEnabled()) { logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders." + " Will use the existing UUID strategy.", e); } return false; } return true; }
From source file:org.springframework.test.context.junit4.rules.SpringMethodRule.java
/** * Throw an {@link IllegalStateException} if the supplied {@code testClass} * does not declare a {@code public static final SpringClassRule} field * that is annotated with {@code @ClassRule}. *//*from www. j ava2 s. com*/ private static SpringClassRule validateSpringClassRuleConfiguration(Class<?> testClass) { Field ruleField = findSpringClassRuleField(testClass) .orElseThrow( () -> new IllegalStateException(String.format( "Failed to find 'public static final SpringClassRule' field in test class [%s]. " + "Consult the javadoc for SpringClassRule for details.", testClass.getName()))); Assert.state(ruleField.isAnnotationPresent(ClassRule.class), () -> String .format("SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. " + "Consult the javadoc for SpringClassRule for details.", ruleField)); Object result = ReflectionUtils.getField(ruleField, null); Assert.state(result instanceof SpringClassRule, "SpringClassRule field mismatch"); return (SpringClassRule) result; }
From source file:org.springframework.test.util.ReflectionTestUtils.java
/** * Get the value of the {@linkplain Field field} with the given {@code name} * from the provided {@code targetObject}/{@code targetClass}. * <p>If the supplied {@code targetObject} is a <em>proxy</em>, it will * be {@linkplain AopTestUtils#getUltimateTargetObject unwrapped} allowing * the field to be retrieved from the ultimate target of the proxy. * <p>This method traverses the class hierarchy in search of the desired * field. In addition, an attempt will be made to make non-{@code public} * fields <em>accessible</em>, thus allowing one to get {@code protected}, * {@code private}, and <em>package-private</em> fields. * @param targetObject the target object from which to get the field; may be * {@code null} if the field is static/*from w w w. j a v a 2 s . co m*/ * @param targetClass the target class from which to get the field; may * be {@code null} if the field is an instance field * @param name the name of the field to get; never {@code null} * @return the field's current value * @since 4.2 * @see #getField(Object, String) * @see #getField(Class, String) * @see ReflectionUtils#findField(Class, String, Class) * @see ReflectionUtils#makeAccessible(Field) * @see ReflectionUtils#getField(Field, Object) * @see AopTestUtils#getUltimateTargetObject(Object) */ @Nullable public static Object getField(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name) { Assert.isTrue(targetObject != null || targetClass != null, "Either targetObject or targetClass for the field must be specified"); if (targetObject != null && springAopPresent) { targetObject = AopTestUtils.getUltimateTargetObject(targetObject); } if (targetClass == null) { targetClass = targetObject.getClass(); } Field field = ReflectionUtils.findField(targetClass, name); if (field == null) { throw new IllegalArgumentException(String.format("Could not find field '%s' on %s or target class [%s]", name, safeToString(targetObject), targetClass)); } if (logger.isDebugEnabled()) { logger.debug(String.format("Getting field '%s' from %s or target class [%s]", name, safeToString(targetObject), targetClass)); } ReflectionUtils.makeAccessible(field); return ReflectionUtils.getField(field, targetObject); }