Example usage for org.springframework.util ReflectionUtils makeAccessible

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

Introduction

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

Prototype

@SuppressWarnings("deprecation") 
public static void makeAccessible(Field field) 

Source Link

Document

Make the given field accessible, explicitly setting it accessible if necessary.

Usage

From source file:nh.examples.springintegration.order.framework.domain.internal.ReflectionHelper.java

public static Method getAccessibleMethod(String name, Class<?> targetClass, Object parameter) {
    checkNotNull(name);/*  w  ww  .java  2  s .  c om*/
    checkNotNull(targetClass);
    checkNotNull(parameter);

    Method executeMethod = ReflectionUtils.findMethod(targetClass, name, parameter.getClass());
    if (executeMethod == null) {
        throw new IllegalStateException(format("No '%s'-method for command type %s found on class %s", name,
                parameter.getClass().getName(), targetClass.getName()));
    }
    ReflectionUtils.makeAccessible(executeMethod);

    return executeMethod;
}

From source file:com.github.javarch.persistence.orm.hibernate.listener.EntityDateRegister.java

public static void setCurrentDateOnFieldWithAnnotation(final Object obj,
        final Class<? extends Annotation> annotationClass) {
    try {/*from w w w  .j  a  v a2  s. co  m*/
        if (LOG.isDebugEnabled()) {
            LOG.debug("PreUpdateEvent - Em classe {}", obj.getClass().getName());
        }
        List<Field> campos = FieldAnnotationUtils.findFieldsWithAnnotation(obj.getClass(), annotationClass);
        for (Field campo : campos) {
            ReflectionUtils.makeAccessible(campo);
            ReflectionUtils.setField(campo, obj, new Date());
        }
    } catch (Exception e) {
        LOG.error("O seguinte erro ocorreu ao tentar setar new Date() em campo anotado com {} da classe {}: {}",
                annotationClass.getName(), obj.getClass().getName(), e.getMessage());
    }

}

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  www . ja v  a2s  .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: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//from  ww  w.  java  2s. 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.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.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:com.erinors.hpb.tests.HpbTestUtils.java

public static void assertEqualsByCloneableFields(Object o1, Object o2) {
    if (o1 == null || o2 == null || o1.getClass() != o2.getClass()) {
        throw new IllegalArgumentException(
                "Non-null objects of same class expected but got: " + o1 + ", " + o2);
    }//  www  .j  a v a  2 s  .  c o  m

    List<Field> fields = new LinkedList<Field>();
    ClassUtils.collectCloneableFields(o1.getClass(), fields);

    for (Field field : fields) {
        try {
            ReflectionUtils.makeAccessible(field);

            Object fieldValue1 = field.get(o1);
            Object fieldValue2 = field.get(o2);

            if (!equal(fieldValue1, fieldValue2)) {
                throw new AssertionError("Field value mismatch: " + field + ", value1=" + fieldValue1
                        + ", value2=" + fieldValue2);
            }
        } catch (Exception e) {
            throw new RuntimeException("Cannot copy field: " + field, e);
        }
    }
}

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.consol.citrus.admin.mock.Mocks.java

/**
 * Inject Spring autowired fields in target instance with mocks.
 * @param target//from  ww w .  ja v a 2 s.c  o m
 */
public static void injectMocks(Object target) {
    ReflectionUtils.doWithFields(target.getClass(),
            field -> ReflectionUtils.setField(field, target, Mockito.mock(field.getType())), field -> {
                if (field.isAnnotationPresent(Autowired.class)) {
                    if (!field.isAccessible()) {
                        ReflectionUtils.makeAccessible(field);
                    }

                    return true;
                }

                return false;
            });
}