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.LowestCommonGroupCrossoverAlgorithmTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*from w  w  w . j  a va2  s  .  co  m*/
public void testSetMutationAlgorithm() {
    MutationAlgorithm mutationAlgorithmToSet = mock(MutationAlgorithm.class);

    LowestCommonGroupCrossoverAlgorithm lowestCommonGroupCrossoverAlgorithm = new LowestCommonGroupCrossoverAlgorithm();
    lowestCommonGroupCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

    Field mutationAlgorithmField = ReflectionUtils.findField(LowestCommonGroupCrossoverAlgorithm.class,
            "mutationAlgorithm");
    ReflectionUtils.makeAccessible(mutationAlgorithmField);
    MutationAlgorithm mutationAlgorithmFromObject = (MutationAlgorithm) ReflectionUtils
            .getField(mutationAlgorithmField, lowestCommonGroupCrossoverAlgorithm);

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

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

@Test
public void testSetPersistenceBatchSize() {
    int persistenceBatchSizeToSet = 99;
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet);

    Field persistenceBatchSizeField = ReflectionUtils.findField(FrequencyListImporterImpl.class,
            "persistenceBatchSize");
    ReflectionUtils.makeAccessible(persistenceBatchSizeField);
    int persistenceBatchSizeFromObject = (int) ReflectionUtils.getField(persistenceBatchSizeField,
            frequencyListImporterImpl);//from  w  w  w. ja  v  a2 s .  co m

    assertEquals(persistenceBatchSizeToSet, persistenceBatchSizeFromObject);
}

From source file:com.erinors.hpb.server.handler.JavaBeanHandler.java

private Object copyBean(Object object, ObjectCopier objectCopier, Context context) {
    Class<?> clazz = object.getClass();

    Object result;//from  w  w w  .  j  a  v  a  2  s. c o m
    try {
        Constructor<?> constructor = ClassUtils.getAccessibleInstanceConstructor(clazz);
        result = constructor.newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Cannot instantiate: " + object.getClass(), e);
    }

    context.addProcessedObject(object, result);

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

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

            Object fieldValue = field.get(object);
            Object processedFieldValue = objectCopier.processObject(fieldValue);
            field.set(result, processedFieldValue);
        } catch (Exception e) {
            throw new RuntimeException("Cannot copy field: " + field, e);
        }
    }

    return result;
}

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

@Test
public void testSetCommandsBefore() {
    String[] commandsBeforeToSet = new String[1];
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();
    geneticCipherSolutionService.setCommandsBefore(commandsBeforeToSet);

    Field commandsBeforeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "commandsBefore");
    ReflectionUtils.makeAccessible(commandsBeforeField);
    String[] commandsBeforeFromObject = (String[]) ReflectionUtils.getField(commandsBeforeField,
            geneticCipherSolutionService);

    assertSame(commandsBeforeToSet, commandsBeforeFromObject);
}

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

private static ProxyFactory findCglibProxyFactory(final Object proxy) {
    Field field = ReflectionUtils.findField(proxy.getClass(), "CGLIB$CALLBACK_0");
    ReflectionUtils.makeAccessible(field);
    Object CGLIB$CALLBACK_0 = ReflectionUtils.getField(field, proxy);
    return (ProxyFactory) ReflectionUtils.getField(CglibAopProxy$DynamicAdvisedInterceptor_advised_FIELD,
            CGLIB$CALLBACK_0);/*from  ww  w .j a v  a  2s. c  o m*/

}

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

@Test
public void testSetGeneDao() {
    VariableLengthGeneDao geneDaoToSet = mock(VariableLengthGeneDao.class);

    ConservativeMutationAlgorithm conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();
    conservativeMutationAlgorithm.setGeneDao(geneDaoToSet);

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

    assertSame(geneDaoToSet, geneDaoFromObject);
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetGeneticStructure() {
    Population population = new Population();

    MockBreeder mockBreeder = new MockBreeder();
    population.setBreeder(mockBreeder);/*from   ww w.  j  a v  a 2  s. c  o m*/

    Object geneticStructure = new Object();
    population.setGeneticStructure(geneticStructure);

    Field breederField = ReflectionUtils.findField(Population.class, "breeder");
    ReflectionUtils.makeAccessible(breederField);
    MockBreeder breederFromObject = (MockBreeder) ReflectionUtils.getField(breederField, population);

    Field geneticStructureField = ReflectionUtils.findField(MockBreeder.class, "geneticStructure");
    ReflectionUtils.makeAccessible(geneticStructureField);
    Object geneticStructureFromObject = ReflectionUtils.getField(geneticStructureField, breederFromObject);

    assertSame(geneticStructure, geneticStructureFromObject);
}

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

@Test
public void testSetSequenceDao() {
    SequenceDao sequenceDaoToSet = mock(SequenceDao.class);

    SingleSequenceMutationAlgorithm singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();
    singleSequenceMutationAlgorithm.setSequenceDao(sequenceDaoToSet);

    Field sequenceDaoField = ReflectionUtils.findField(SingleSequenceMutationAlgorithm.class, "sequenceDao");
    ReflectionUtils.makeAccessible(sequenceDaoField);
    SequenceDao sequenceDaoFromObject = (SequenceDao) ReflectionUtils.getField(sequenceDaoField,
            singleSequenceMutationAlgorithm);

    assertSame(sequenceDaoToSet, sequenceDaoFromObject);
}

From source file:com.anteam.demo.codec.common.CoderUtil.java

public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws DecoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }/*from w  ww  .ja  v a2 s .  co m*/
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.decode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.decode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.decode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

From source file:com.ciphertool.zodiacengine.fitness.AbstractSolutionEvaluatorBaseTest.java

@Test
public void testCalculateAdjacentMatches() {
    ConcreteSolutionEvaluatorBase abstractSolutionEvaluatorBase = new ConcreteSolutionEvaluatorBase();

    Field logField = ReflectionUtils.findField(ConcreteSolutionEvaluatorBase.class, "log");
    Logger mockLogger = mock(Logger.class);
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, abstractSolutionEvaluatorBase, mockLogger);

    simpleSolution.getPlaintextCharacters().get(0).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(1).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(4).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(5).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(6).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(8).setHasMatch(true);
    simpleSolution.getPlaintextCharacters().get(9).setHasMatch(true);

    assertEquals(0, simpleSolution.getAdjacentMatches());

    abstractSolutionEvaluatorBase.setGeneticStructure(simpleCipher);
    int adjacentMatches = abstractSolutionEvaluatorBase
            .calculateAdjacentMatches(simpleSolution.getPlaintextCharacters());

    /*//from www.  java2  s  . c o  m
     * The adjacent match count should not be updated on the solution. It
     * should only be returned by the method.
     */
    assertEquals(0, simpleSolution.getAdjacentMatches());
    assertEquals(4, adjacentMatches);

    verifyZeroInteractions(mockLogger);
}