Example usage for org.springframework.util ReflectionUtils findField

List of usage examples for org.springframework.util ReflectionUtils findField

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils findField.

Prototype

@Nullable
public static Field findField(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Field field on the supplied Class with the supplied name .

Usage

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
@Ignore// ww w .  j a v  a2s.co m
public void performanceTest() {
    int times = 1000000;
    StopWatch watch = new StopWatch();
    watch.start();
    for (int i = 0; i < times; i++) {
        new GenericMessage<Integer>(0);
    }
    watch.stop();
    double defaultGeneratorElapsedTime = watch.getTotalTimeSeconds();

    Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
    ReflectionUtils.makeAccessible(idGeneratorField);
    ReflectionUtils.setField(idGeneratorField, null, (IdGenerator) () -> TimeBasedUUIDGenerator.generateId());
    watch = new StopWatch();
    watch.start();
    for (int i = 0; i < times; i++) {
        new GenericMessage<Integer>(0);
    }
    watch.stop();
    double timebasedGeneratorElapsedTime = watch.getTotalTimeSeconds();

    logger.info("Generated " + times + " messages using default UUID generator " + "in "
            + defaultGeneratorElapsedTime + " seconds");
    logger.info("Generated " + times + " messages using Timebased UUID generator " + "in "
            + timebasedGeneratorElapsedTime + " seconds");

    logger.info("Time-based ID generator is " + defaultGeneratorElapsedTime / timebasedGeneratorElapsedTime
            + " times faster");
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

private void assertDestroy() throws Exception {
    Field idGenField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
    ReflectionUtils.makeAccessible(idGenField);
    assertNull("the idGenerator field has not been properly reset to null", idGenField.get(null));
}

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/* ww  w . ja  v a  2  s  .c om*/
 * @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);
}

From source file:org.squashtest.tm.web.internal.controller.audittrail.RequirementAuditEventTableModelBuilder.java

private boolean propertyIsEnumeratedAndInternationalizable(RequirementPropertyChange event) {
    Field field = ReflectionUtils.findField(RequirementVersion.class, event.getPropertyName());
    Class<?> fieldType = field.getType();

    return Enum.class.isAssignableFrom(fieldType) && Internationalizable.class.isAssignableFrom(fieldType);
}

From source file:org.squashtest.tm.web.internal.controller.audittrail.RequirementAuditEventTableModelBuilder.java

private boolean propertyIsInfolist(RequirementPropertyChange event) {
    Field field = ReflectionUtils.findField(RequirementVersion.class, event.getPropertyName());
    Class<?> fieldType = field.getType();

    return InfoListItem.class.isAssignableFrom(fieldType);

}

From source file:org.squashtest.tm.web.internal.controller.audittrail.RequirementAuditEventTableModelBuilder.java

private Object[] buildMessageArgsForI18nableEnumProperty(RequirementPropertyChange event) {
    Field enumField = ReflectionUtils.findField(RequirementVersion.class, event.getPropertyName());
    Class<?> enumType = enumField.getType();

    String oldValueLabel = retrieveEnumI18ndLabel(enumType, event.getOldValue());
    String newValueLabel = retrieveEnumI18ndLabel(enumType, event.getNewValue());

    return new Object[] { oldValueLabel, newValueLabel };
}

From source file:org.tdar.core.service.ReflectionService.java

public Field getFieldForGetterOrSetter(Method method) {
    String name = cleanupMethodName(method);
    Field field = ReflectionUtils.findField(method.getDeclaringClass(), name);
    return field;
}