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:com.ciphertool.genetics.algorithms.crossover.LiberalUnevaluatedCrossoverAlgorithmTest.java

@Test
public void testSetGeneDao() {
    GeneDao geneDaoToSet = mock(GeneDao.class);
    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setGeneDao(geneDaoToSet);

    Field geneDaoField = ReflectionUtils.findField(LiberalUnevaluatedCrossoverAlgorithm.class, "geneDao");
    ReflectionUtils.makeAccessible(geneDaoField);
    GeneDao geneDaoFromObject = (GeneDao) ReflectionUtils.getField(geneDaoField,
            liberalUnevaluatedCrossoverAlgorithm);

    assertSame(geneDaoToSet, geneDaoFromObject);
}

From source file:com.ebiz.modules.persistence.repository.support.MyRepositoryImpl.java

@Override
@Transactional//from w  ww.j  a va2 s . c o  m
public void delete(T entity) {
    logger.trace("----->MyRepositoryImpl.delete(T entity)");
    Assert.notNull(entity, "The entity must not be null!");
    Class<?> clazz = entity.getClass();
    if (clazz.isAnnotationPresent(LogicallyDelete.class)) {
        LogicallyDelete logicallyDelete = clazz.getAnnotation(LogicallyDelete.class);
        Object value = ConvertUtils.convert(logicallyDelete.value(), logicallyDelete.type().getClazz());
        Field field = ReflectionUtils.findField(entity.getClass(), logicallyDelete.name());
        ReflectionUtils.makeAccessible(field);
        ReflectionUtils.setField(field, entity, value);
        save(entity);
    } else {
        super.delete(entity);
    }
}

From source file:nl.surfnet.coin.teams.control.AbstractControllerTest.java

private void doAutoWireRemainingResources(Object target, Field[] fields) throws IllegalAccessException {
    for (Field field : fields) {
        ReflectionUtils.makeAccessible(field);
        if (field.getAnnotation(Autowired.class) != null && field.get(target) == null) {
            field.set(target, mock(field.getType(), new DoesNothing()));
        }/*w w  w  . j a  v a 2s  . c o m*/
    }
}

From source file:com.ciphertool.genetics.algorithms.mutation.LiberalMutationAlgorithmTest.java

@BeforeClass
public static void setUp() {
    liberalMutationAlgorithm = new LiberalMutationAlgorithm();

    geneDaoMock = mock(VariableLengthGeneDao.class);
    liberalMutationAlgorithm.setGeneDao(geneDaoMock);
    chromosomeHelperSpy = spy(new KeylessChromosomeHelper());
    geneDaoMockForChromosomeHelper = mock(VariableLengthGeneDao.class);
    liberalMutationAlgorithm.setChromosomeHelper(chromosomeHelperSpy);

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

From source file:com.ciphertool.genetics.algorithms.crossover.LiberalCrossoverAlgorithmTest.java

@Test
public void testSetGeneDao() {
    GeneDao geneDaoToSet = mock(GeneDao.class);
    LiberalCrossoverAlgorithm liberalCrossoverAlgorithm = new LiberalCrossoverAlgorithm();
    liberalCrossoverAlgorithm.setGeneDao(geneDaoToSet);

    Field geneDaoField = ReflectionUtils.findField(LiberalCrossoverAlgorithm.class, "geneDao");
    ReflectionUtils.makeAccessible(geneDaoField);
    GeneDao geneDaoFromObject = (GeneDao) ReflectionUtils.getField(geneDaoField, liberalCrossoverAlgorithm);

    assertSame(geneDaoToSet, geneDaoFromObject);
}

From source file:pl.maciejwalkowiak.plist.FieldSerializer.java

private String serializeField(Object o, Field field) {
    String result = "";

    int modifiers = field.getModifiers();

    if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
        ReflectionUtils.makeAccessible(field);

        if (!field.isAnnotationPresent(PlistIgnore.class)) {
            result = processField(field, o).toString();
        } else {/*from   w  w  w.  ja  va2 s.com*/
            logger.debug("field {} is ignored", field.getName());
        }
    }

    return result;
}

From source file:com.alibaba.otter.shared.communication.core.impl.AbstractCommunicationEndpoint.java

/**
 * ?/*  w ww  . j ava  2  s .c  o m*/
 */
public Object acceptEvent(Event event) {
    if (event instanceof HeartEvent) {
        return event; // ??
    }

    try {
        Object action = CommunicationRegistry.getAction(event.getType());
        if (action != null) {

            // ???
            String methodName = "on" + StringUtils.capitalize(event.getType().toString());
            Method method = ReflectionUtils.findMethod(action.getClass(), methodName,
                    new Class[] { event.getClass() });
            if (method == null) {
                methodName = DEFAULT_METHOD; // ?
                method = ReflectionUtils.findMethod(action.getClass(), methodName,
                        new Class[] { event.getClass() });

                if (method == null) { // ??Event?
                    method = ReflectionUtils.findMethod(action.getClass(), methodName,
                            new Class[] { Event.class });
                }
            }
            // ?,????
            if (method != null) {
                try {
                    ReflectionUtils.makeAccessible(method);
                    return method.invoke(action, new Object[] { event });
                } catch (Throwable e) {
                    throw new CommunicationException("method_invoke_error:" + methodName, e);
                }
            } else {
                throw new CommunicationException(
                        "no_method_error for[" + StringUtils.capitalize(event.getType().toString())
                                + "] in Class[" + action.getClass().getName() + "]");
            }

        }

        throw new CommunicationException("eventType_no_action", event.getType().name());
    } catch (RuntimeException e) {
        logger.error("endpoint_error", e);
        throw e;
    } catch (Exception e) {
        logger.error("endpoint_error", e);
        throw new CommunicationException(e);
    }
}

From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java

@Test
public void testSetGeneticAlgorithm() {
    GeneticAlgorithm geneticAlgorithmToSet = mock(GeneticAlgorithm.class);
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();
    geneticCipherSolutionService.setGeneticAlgorithm(geneticAlgorithmToSet);

    Field geneticAlgorithmField = ReflectionUtils.findField(GeneticCipherSolutionService.class,
            "geneticAlgorithm");
    ReflectionUtils.makeAccessible(geneticAlgorithmField);
    GeneticAlgorithm geneticAlgorithmFromObject = (GeneticAlgorithm) ReflectionUtils
            .getField(geneticAlgorithmField, geneticCipherSolutionService);

    assertSame(geneticAlgorithmToSet, geneticAlgorithmFromObject);
}

From source file:com.excilys.ebi.utils.spring.log.slf4j.InjectLoggerAnnotationBeanPostProcessor.java

/**
 * Processes a bean's fields for injection if it has a {@link InjectLogger}
 * annotation./*from w ww . j av  a  2s.c o m*/
 */
protected void processLogger(final Object bean) {
    final Class<?> clazz = bean.getClass();

    ReflectionUtils.doWithFields(clazz, new FieldCallback() {
        public void doWith(Field field) {
            Annotation annotation = field.getAnnotation(InjectLogger.class);

            if (annotation != null) {
                int modifiers = field.getModifiers();
                Assert.isTrue(!Modifier.isStatic(modifiers),
                        "InjectLogger annotation is not supported on static fields");
                Assert.isTrue(!Modifier.isFinal(modifiers),
                        "InjectLogger annotation is not supported on final fields");

                ReflectionUtils.makeAccessible(field);

                Logger logger = LoggerFactory.getLogger(clazz);

                ReflectionUtils.setField(field, bean, logger);
            }
        }
    });
}

From source file:com.ciphertool.sentencebuilder.etl.importers.FrequencyListImporterImplTest.java

@Test
public void testSetWordDao() {
    WordDao wordDaoToSet = mock(WordDao.class);
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setWordDao(wordDaoToSet);

    Field wordDaoField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "wordDao");
    ReflectionUtils.makeAccessible(wordDaoField);
    WordDao wordDaoFromObject = (WordDao) ReflectionUtils.getField(wordDaoField, frequencyListImporterImpl);

    assertSame(wordDaoToSet, wordDaoFromObject);
}