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:net.prasenjit.auth.validation.FieldMatchValidator.java

/** {@inheritDoc} */
@Override/*from w ww .  j  a v a  2 s .co m*/
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    try {
        Field field = ReflectionUtils.findField(value.getClass(), firstFieldName);
        ReflectionUtils.makeAccessible(field);
        final Object firstObj = ReflectionUtils.getField(field, value);
        field = ReflectionUtils.findField(value.getClass(), secondFieldName);
        ReflectionUtils.makeAccessible(field);
        final Object secondObj = ReflectionUtils.getField(field, value);

        return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
    } catch (final Exception ignore) {
        // ignore
    }
    return true;
}

From source file:almira.sample.util.LogPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) {
    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
        @Override/*w  ww .  j  a va 2s .  c o m*/
        public void doWith(Field field) throws IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            if (field.getAnnotation(Log.class) != null) {
                Logger logger = LoggerFactory.getLogger(bean.getClass().getName());
                field.set(bean, logger);
            }
        }
    });
    return bean;
}

From source file:au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtilsUnitTest.java

@Test
public void testConstructor() throws Exception {
    Constructor<DateTimeUtils> constructor = DateTimeUtils.class.getDeclaredConstructor();
    assertFalse(constructor.isAccessible());
    ReflectionUtils.makeAccessible(constructor);
    assertNotNull(constructor.newInstance());
}

From source file:grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.java

@Override
protected Object retrieveAction(GroovyObject controller, String actionName, HttpServletResponse response) {

    Method method = ReflectionUtils.findMethod(AopProxyUtils.ultimateTargetClass(controller), actionName,
            MethodGrailsControllerHelper.NOARGS);

    if (method != null) {
        ReflectionUtils.makeAccessible(method);
        if (method.getAnnotation(Action.class) != null) {
            return method;
        }//from  w ww. j a v a 2s. com
    }

    return super.retrieveAction(controller, actionName, response);
}

From source file:com.ciphertool.sentencebuilder.dao.WordDaoTest.java

@BeforeClass
public static void setUp() {
    queryMock = mock(Query.class);
    sessionMock = mock(Session.class);
    logMock = mock(Logger.class);
    sessionFactoryMock = mock(SessionFactory.class);

    wordDao = new WordDao();
    wordDao.setSessionFactory(sessionFactoryMock);

    Field logField = ReflectionUtils.findField(WordDao.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, wordDao, logMock);
}

From source file:org.grails.datastore.gorm.support.BeforeValidateHelper.java

public void invokeBeforeValidate(final Object target, final List<?> validatedFieldsList) {
    Class<?> domainClass = target.getClass();
    Method method = null;/*from  w  w w. ja v a2  s.c  o m*/
    if (validatedFieldsList == null) {
        // prefer the no-arg version of beforeValidate() if validatedFieldsList
        // is null...
        method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE);
        if (method == null) {
            method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE, List.class);
        }
    } else {
        // prefer the list-arg version of beforeValidate() if
        // validatedFieldsList is not null...
        method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE, List.class);
        if (method == null) {
            method = ReflectionUtils.findMethod(domainClass, BEFORE_VALIDATE);
        }
    }
    if (method != null) {
        ReflectionUtils.makeAccessible(method);
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
            ReflectionUtils.invokeMethod(method, target, validatedFieldsList);
        } else {
            ReflectionUtils.invokeMethod(method, target);
        }
    }
}

From source file:com.ciphertool.genetics.algorithms.mutation.cipherkey.RandomValueMutationAlgorithmTest.java

@BeforeClass
public static void setUp() {
    randomValueMutationAlgorithm = new RandomValueMutationAlgorithm();

    geneDaoMock = mock(GeneDao.class);
    randomValueMutationAlgorithm.setGeneDao(geneDaoMock);

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

From source file:com.blstream.hooks.springframework.mongodb.event.CollectionCascadingMongoEventListener.java

public void onBeforeConvert(final Object source) {
    ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {

        @Override/*from   w w w .  ja  v a  2  s .  c o  m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(DBRef.class)
                    && field.isAnnotationPresent(DBRefCollectionCascade.class)) {
                final Object fieldValue = field.get(source);

                if (fieldValue instanceof Collection<?>) {
                    Collection<?> collection = (Collection<?>) fieldValue;

                    for (Object collectionElement : collection) {
                        DbRefFieldCallback callback = new DbRefFieldCallback();

                        ReflectionUtils.doWithFields(collectionElement.getClass(), callback);

                        if (!callback.isIdFound()) {
                            throw new MappingException(
                                    "Cannot perform cascade save on child object without id set");
                        }

                        mongoOperations.save(collectionElement);
                    }
                }
            }
        }
    });
}

From source file:com.blstream.hooks.springframework.mongodb.event.CascadingMongoEventListener.java

public void onBeforeConvert(final Object source) {
    ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {

        @Override//from w  w  w  .  ja v a 2 s .c  o m
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(DBRefCascade.class)) {
                final Object fieldValue = field.get(source);

                DbRefFieldCallback callback = new DbRefFieldCallback();

                ReflectionUtils.doWithFields(fieldValue.getClass(), callback);

                if (!callback.isIdFound()) {
                    throw new MappingException("Cannot perform cascade save on child object without id set");
                }

                mongoOperations.save(fieldValue);
            }
        }
    });
}

From source file:org.grails.datastore.mapping.keyvalue.mapping.config.AnnotationKeyValueMappingFactory.java

@Override
public KeyValue createMappedForm(PersistentProperty mpp) {
    final Class javaClass = mpp.getOwner().getJavaClass();
    final ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(javaClass);

    final PropertyDescriptor pd = cpf.getPropertyDescriptor(mpp.getName());
    final KeyValue kv = super.createMappedForm(mpp);
    Index index = AnnotationUtils.getAnnotation(pd.getReadMethod(), Index.class);

    if (index == null) {
        final Field field = ReflectionUtils.findField(javaClass, mpp.getName());
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            index = field.getAnnotation(Index.class);
        }//from www  .j a  v a 2 s  .  c om
    }
    if (index != null) {
        kv.setIndex(true);
    }

    return kv;
}