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:net.projectmonkey.spring.acl.util.reflect.FieldUtil.java

public static Object getFieldValue(final Object domainObject, final String fieldName) {
    Field field = ReflectionUtils.findField(domainObject.getClass(), fieldName);
    field.setAccessible(true);/*from   w w  w  .  java 2 s.  co  m*/
    try {
        return field.get(domainObject);
    } catch (Exception e) {
        throw new AuthenticationServiceException("Exception retrieving field " + fieldName, e);
    }
}

From source file:br.com.modoagil.util.ReflectionUtil.java

/**
 * Seta em um atributo de uma classe o atributo com nome '{@code name}' o valor '{@code value}' no objeto desta instncia
 *
 * @param target//from   w ww  . j  a v  a  2s.  co m
 *            objeto a ter o campo e valores setados
 * @param name
 *            nome do atributo a ser setado o contedo
 * @param value
 *            contedo a ser setado
 * @see ReflectionUtils#findField(Class, String)
 * @see ReflectionUtils#makeAccessible(Field)
 * @see ReflectionUtils#setField(Field, Object, Object)
 */
public static void setField(final Object target, final String name, final Object value) {
    final Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, target, value);
}

From source file:org.grails.orm.hibernate.cfg.GrailsIdentifierGeneratorFactory.java

public static void applyNewInstance(Configuration cfg) throws IllegalArgumentException, IllegalAccessException {
    Field field = ReflectionUtils.findField(Configuration.class, "identifierGeneratorFactory");
    field.setAccessible(true);/*from  ww  w  .j ava2  s .  co m*/
    field.set(cfg, new GrailsIdentifierGeneratorFactory());
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.AlphaSelectorTest.java

@BeforeClass
public static void setUp() {
    alphaSelector = new AlphaSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(AlphaSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, alphaSelector, logMock);
}

From source file:com.oembedler.moon.graphql.engine.dfs.SchemaHelper.java

public static void replaceTypeReferencesForUnionType(final GraphQLSchema schema,
        final Set<GraphQLUnionType> graphQLUnionTypeMap) {
    Field graphQLTypesField = ReflectionUtils.findField(GraphQLUnionType.class, "types");
    graphQLTypesField.setAccessible(true);
    for (GraphQLUnionType graphQLUnionType : graphQLUnionTypeMap) {
        List<GraphQLType> graphQLTypes = new ArrayList<>();
        for (GraphQLType graphQLType : graphQLUnionType.getTypes()) {
            if (graphQLType instanceof GraphQLTypeReference) {
                graphQLTypes.add(schema.getType(graphQLType.getName()));
            } else {
                graphQLTypes.add(graphQLType);
            }//  ww  w  . j a v a2 s  .  co  m
        }
        ReflectionUtils.setField(graphQLTypesField, graphQLUnionType, graphQLTypes);
    }
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.RandomSelectorTest.java

@BeforeClass
public static void setUp() {
    randomSelector = new RandomSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(RandomSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, randomSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.RouletteSelectorTest.java

@BeforeClass
public static void setUp() {
    rouletteSelector = new RouletteSelector();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(RouletteSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, rouletteSelector, logMock);
}

From source file:grails.plugin.cache.util.ClassUtils.java

/**
 * This method will try to retrieve the value of the named property from the
 * object using a corresponding getter method.  If no getter method is found
 * then this method will look for the corresponding field and return its value.
 *
 * @param object object to inspect// w w  w .  j  av  a2  s.  c om
 * @param propertyOrFieldName the name of the field or property to retrieve
 * @return the value of the field or property, null if neither is found
 */
public static Object getPropertyOrFieldValue(Object object, String propertyOrFieldName) {
    final String getterName = GrailsNameUtils.getGetterName(propertyOrFieldName);
    final Class<? extends Object> objectClass = object.getClass();
    try {
        final Method method = objectClass.getMethod(getterName, new Class[0]);
        if (method != null) {
            ReflectionUtils.makeAccessible(method);
            return method.invoke(object, new Object[0]);
        }
    } catch (Exception e) {
    }
    try {
        final Field field = ReflectionUtils.findField(objectClass, propertyOrFieldName);
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            return field.get(object);
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.ciphertool.genetics.algorithms.selection.modes.TournamentSelectorTest.java

@BeforeClass
public static void setUp() {
    tournamentSelector = new TournamentSelector();
    tournamentSelector.setSelectionAccuracy(0.9);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(TournamentSelector.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, tournamentSelector, logMock);
}

From source file:com.ciphertool.genetics.algorithms.selection.ProbabilisticSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    probabilisticSelectionAlgorithm = new ProbabilisticSelectionAlgorithm();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(ProbabilisticSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, probabilisticSelectionAlgorithm, logMock);
}