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:org.socialsignin.spring.data.dynamodb.repository.support.FieldAndGetterReflectionEntityInformation.java

/**
 * Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the
 * given domain class for a getter carrying the given annotation.
 * //w  w w.java 2s .c  om
 * @param domainClass
 *            must not be {@literal null}.
 * @param annotation
 *            must not be {@literal null}.
 */
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass,
        final Class<? extends Annotation> annotation) {

    super(domainClass);
    Assert.notNull(annotation);

    ReflectionUtils.doWithMethods(domainClass, new MethodCallback() {
        public void doWith(Method method) {
            if (method.getAnnotation(annotation) != null) {
                FieldAndGetterReflectionEntityInformation.this.method = method;
                return;
            }
        }
    });

    if (method == null) {
        ReflectionUtils.doWithFields(domainClass, new FieldCallback() {
            public void doWith(Field field) {
                if (field.getAnnotation(annotation) != null) {
                    FieldAndGetterReflectionEntityInformation.this.field = field;
                    return;
                }
            }
        });
    }

    Assert.isTrue(this.method != null || this.field != null,
            String.format("No field or method annotated with %s found!", annotation.toString()));
    Assert.isTrue(this.method == null || this.field == null,
            String.format("Both field and method annotated with %s found!", annotation.toString()));

    if (method != null) {
        ReflectionUtils.makeAccessible(method);
    }
}

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

@Test
public void testSetFitnessEvaluator() {
    FitnessEvaluator fitnessEvaluatorToSet = mock(FitnessEvaluator.class);
    LiberalCrossoverAlgorithm liberalCrossoverAlgorithm = new LiberalCrossoverAlgorithm();
    liberalCrossoverAlgorithm.setFitnessEvaluator(fitnessEvaluatorToSet);

    Field fitnessEvaluatorField = ReflectionUtils.findField(LiberalCrossoverAlgorithm.class,
            "fitnessEvaluator");
    ReflectionUtils.makeAccessible(fitnessEvaluatorField);
    FitnessEvaluator fitnessEvaluatorFromObject = (FitnessEvaluator) ReflectionUtils
            .getField(fitnessEvaluatorField, liberalCrossoverAlgorithm);

    assertSame(fitnessEvaluatorToSet, fitnessEvaluatorFromObject);
}

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

@Test
public void testSetCommandsAfter() {
    String[] commandsAfterToSet = new String[1];
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();
    geneticCipherSolutionService.setCommandsAfter(commandsAfterToSet);

    Field commandsAfterField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "commandsAfter");
    ReflectionUtils.makeAccessible(commandsAfterField);
    String[] commandsAfterFromObject = (String[]) ReflectionUtils.getField(commandsAfterField,
            geneticCipherSolutionService);

    assertSame(commandsAfterToSet, commandsAfterFromObject);
}

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

@Test
@SuppressWarnings("unchecked")
public void testSetFileParser() {
    FileParser<Word> fileParserToSet = mock(FileParser.class);
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setFileParser(fileParserToSet);

    Field fileParserField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "frequencyFileParser");
    ReflectionUtils.makeAccessible(fileParserField);
    FileParser<Word> fileParserFromObject = (FileParser<Word>) ReflectionUtils.getField(fileParserField,
            frequencyListImporterImpl);/*w  w  w . ja  v a 2  s. c  om*/

    assertSame(fileParserToSet, fileParserFromObject);
}

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

@Test
public void testSetMutateDuringCrossover() {
    boolean mutateDuringCrossoverToSet = true;

    LowestCommonGroupCrossoverAlgorithm lowestCommonGroupCrossoverAlgorithm = new LowestCommonGroupCrossoverAlgorithm();
    lowestCommonGroupCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

    Field mutateDuringCrossoverField = ReflectionUtils.findField(LowestCommonGroupCrossoverAlgorithm.class,
            "mutateDuringCrossover");
    ReflectionUtils.makeAccessible(mutateDuringCrossoverField);
    boolean mutateDuringCrossoverFromObject = (boolean) ReflectionUtils.getField(mutateDuringCrossoverField,
            lowestCommonGroupCrossoverAlgorithm);

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

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

@Test
public void testSetRandomListElementSelector() {
    RandomListElementSelector randomListElementSelectorToSet = mock(RandomListElementSelector.class);
    ConservativeSinglePointCrossoverAlgorithm conservativeSinglePointCrossoverAlgorithm = new ConservativeSinglePointCrossoverAlgorithm();
    conservativeSinglePointCrossoverAlgorithm.setRandomListElementSelector(randomListElementSelectorToSet);

    Field randomListElementSelectorField = ReflectionUtils
            .findField(ConservativeSinglePointCrossoverAlgorithm.class, "randomListElementSelector");
    ReflectionUtils.makeAccessible(randomListElementSelectorField);
    RandomListElementSelector randomListElementSelectorFromObject = (RandomListElementSelector) ReflectionUtils
            .getField(randomListElementSelectorField, conservativeSinglePointCrossoverAlgorithm);

    assertSame(randomListElementSelectorToSet, randomListElementSelectorFromObject);
}

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

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

    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

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

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

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

@Test
public void testSetMaxMutationsPerChromosome() {
    Integer maxMutationsPerChromosomeToSet = 3;

    ConservativeMutationAlgorithm conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();
    conservativeMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

    Field maxMutationsPerChromosomeField = ReflectionUtils.findField(ConservativeMutationAlgorithm.class,
            "maxMutationsPerChromosome");
    ReflectionUtils.makeAccessible(maxMutationsPerChromosomeField);
    Integer maxMutationsPerChromosomeFromObject = (Integer) ReflectionUtils
            .getField(maxMutationsPerChromosomeField, conservativeMutationAlgorithm);

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

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

@Test
public void testSetMaxMutationsPerChromosome() {
    Integer maxMutationsPerChromosomeToSet = 3;

    SingleSequenceMutationAlgorithm singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();
    singleSequenceMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

    Field maxMutationsPerChromosomeField = ReflectionUtils.findField(SingleSequenceMutationAlgorithm.class,
            "maxMutationsPerChromosome");
    ReflectionUtils.makeAccessible(maxMutationsPerChromosomeField);
    Integer maxMutationsPerChromosomeFromObject = (Integer) ReflectionUtils
            .getField(maxMutationsPerChromosomeField, singleSequenceMutationAlgorithm);

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

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

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

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

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

    assertSame(mockBreeder, breederFromObject);
}