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.sentencebuilder.etl.importers.WordListImporterImplTest.java

@Test
public void testImportNullWord() {
    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();

    Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount");
    ReflectionUtils.makeAccessible(rowCountField);
    AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField,
            wordListImporterImpl);/*  ww w  .  j a  v a  2s .  co  m*/

    assertEquals(0, rowCountFromObject.intValue());

    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true);

    wordListImporterImpl.setWordDao(wordDaoMock);
    wordListImporterImpl.setPersistenceBatchSize(3);

    List<Word> wordBatch = new ArrayList<Word>();
    wordListImporterImpl.importWord(null, wordBatch);

    rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl);

    assertEquals(0, rowCountFromObject.intValue());
    verifyZeroInteractions(wordDaoMock);
    assertTrue(wordBatch.isEmpty());
}

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

@Test
public void testEndImmediately_DebugMode() throws IOException {
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();
    geneticCipherSolutionService.toggleRunning();

    GeneticAlgorithm geneticAlgorithm = mock(GeneticAlgorithm.class);
    Population population = new Population();

    SolutionChromosome solutionChromosome = new SolutionChromosome();
    solutionChromosome.setEvaluationNeeded(false);
    population.addIndividual(solutionChromosome);
    when(geneticAlgorithm.getPopulation()).thenReturn(population);
    geneticCipherSolutionService.setGeneticAlgorithm(geneticAlgorithm);

    Runtime runtimeMock = mock(Runtime.class);

    Field runtimeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "runtime");
    ReflectionUtils.makeAccessible(runtimeField);
    ReflectionUtils.setField(runtimeField, geneticCipherSolutionService, runtimeMock);

    String[] commandsAfter = { "command1", "command2" };
    geneticCipherSolutionService.setCommandsAfter(commandsAfter);

    SolutionDao solutionDaoMock = mock(SolutionDao.class);
    geneticCipherSolutionService.setSolutionDao(solutionDaoMock);

    assertTrue(geneticCipherSolutionService.isRunning());
    geneticCipherSolutionService.endImmediately(true);
    assertFalse(geneticCipherSolutionService.isRunning());

    verify(solutionDaoMock, times(1)).insert(same(solutionChromosome));

    verify(runtimeMock, times(1)).exec(eq("command1"));
    verify(runtimeMock, times(1)).exec(eq("command2"));
    verifyNoMoreInteractions(runtimeMock);
}

From source file:org.terasoluna.gfw.web.token.transaction.TransactionTokenContextImplTest.java

@Test
public void TestCancelReservation04() throws IllegalArgumentException, IllegalAccessException {

    // setup parameters
    TransactionTokenInfo inTransactionToken = new TransactionTokenInfo("testTokenAttribute2",
            TransactionTokenType.IN);//from   w w  w  .j  a  v  a  2  s .c o  m
    TransactionToken receivedToken = new TransactionToken("bbb", "key", "value");

    // setup up expected result
    ReserveCommand expectedCommand = ReserveCommand.UPDATE_TOKEN;

    // run
    TransactionTokenContextImpl contextImpl = new TransactionTokenContextImpl(inTransactionToken,
            receivedToken);
    contextImpl.cancelReservation();

    // test
    Field field = ReflectionUtils.findField(TransactionTokenContextImpl.class, "defaultCommand");
    ReflectionUtils.makeAccessible(field);
    ReserveCommand resultCommand = (ReserveCommand) field.get(contextImpl);
    assertThat(resultCommand, is(expectedCommand));

}

From source file:es.fcs.batch.integration.chunk.MyChunkMessageChannelItemWriter.java

private static Object getField(Object target, String name) {
    Assert.notNull(target, "Target object must not be null");
    Field field = ReflectionUtils.findField(target.getClass(), name);
    if (field == null) {
        logger.debug("Could not find field [" + name + "] on target [" + target + "]");
        return null;
    }/*from   w w w  .  j a v  a2  s.  c o  m*/

    if (logger.isDebugEnabled()) {
        logger.debug("Getting field [" + name + "] from target [" + target + "]");
    }
    ReflectionUtils.makeAccessible(field);
    return ReflectionUtils.getField(field, target);
}

From source file:at.ac.tuwien.infosys.jcloudscale.server.JCloudScaleServer.java

@Override
public void setCloudObjectField(String objectId, String field, byte[] value) {

    UUID theId = UUID.fromString(objectId);
    ServerCloudObject sco = this.cloudObjects.get(theId);
    if (sco == null)
        throw new JCloudScaleException("Cloud object does not exist in server cache: " + objectId);
    touchServerCloudObject(sco);//from   w  ww. j a va2s  .c om
    Object theObject = sco.getObject();

    try {
        Field theField = ReflectionUtil.findField(theObject.getClass(), field);
        ReflectionUtils.makeAccessible(theField);
        Object valueToSet = SerializationUtil.getObjectFromBytes(value,
                this.cloudObjectToClassloaderMap.get(theId));
        valueToSet = CgLibUtil.replaceRefWithProxy(valueToSet, this.cloudObjectToClassloaderMap.get(theId));
        theField.set(theObject, valueToSet);
    } catch (JCloudScaleException e) {
        // this should happen if the by-ref type did not have a default constructor
        e.printStackTrace();
        throw e;
    } catch (Throwable e) {
        logException(e);
        throw new JCloudScaleException(e, "Unable to set field value to cloud object");
    }

}

From source file:grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition.java

protected Closure<?> newInstance(final Class<?> closureClass) {
    try {/* w ww  .ja  va 2s .  co  m*/
        Constructor<?> constructor = closureClass.getConstructor(Object.class, Object.class);
        ReflectionUtils.makeAccessible(constructor);
        return (Closure<?>) constructor.newInstance(this, this);
    } catch (NoSuchMethodException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (InstantiationException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (IllegalAccessException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (InvocationTargetException e) {
        ReflectionUtils.handleInvocationTargetException(e);
    }
    return null;
}

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

@Test
public void testImportWord_BatchSizeNotReached() {
    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();

    Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount");
    ReflectionUtils.makeAccessible(rowCountField);
    AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField,
            wordListImporterImpl);/*  w ww .ja v a2 s . c  o m*/

    assertEquals(0, rowCountFromObject.intValue());

    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true);

    wordListImporterImpl.setWordDao(wordDaoMock);
    wordListImporterImpl.setPersistenceBatchSize(4);

    List<Word> wordBatch = new ArrayList<Word>();

    Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word1, wordBatch);

    verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class));
    assertEquals(1, wordBatch.size());

    Word word2 = new Word(new WordId("elmer", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word2, wordBatch);

    verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class));
    assertEquals(2, wordBatch.size());

    Word word3 = new Word(new WordId("belden", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word3, wordBatch);

    rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl);

    assertEquals(0, rowCountFromObject.intValue());
    verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class));
    assertEquals(3, wordBatch.size());
    assertSame(word1, wordBatch.get(0));
    assertSame(word2, wordBatch.get(1));
    assertSame(word3, wordBatch.get(2));
}

From source file:org.terasoluna.gfw.web.token.transaction.TransactionTokenContextImplTest.java

@Test
public void TestCancelReservation05() throws IllegalArgumentException, IllegalAccessException {

    // setup parameters
    TransactionTokenInfo endTransactionToken = new TransactionTokenInfo("testTokenAttribute3",
            TransactionTokenType.END);/*ww w  . j av a 2  s  .c o m*/
    TransactionToken receivedToken = new TransactionToken("ccc");

    // setup up expected result
    ReserveCommand expectedCommand = ReserveCommand.NONE;

    // run
    TransactionTokenContextImpl contextImpl = new TransactionTokenContextImpl(endTransactionToken,
            receivedToken);

    contextImpl.cancelReservation();

    // test
    Field field = ReflectionUtils.findField(TransactionTokenContextImpl.class, "defaultCommand");
    ReflectionUtils.makeAccessible(field);
    ReserveCommand resultCommand = (ReserveCommand) field.get(contextImpl);
    assertThat(resultCommand, is(expectedCommand));
}

From source file:org.terasoluna.gfw.web.token.transaction.TransactionTokenContextImplTest.java

@Test
public void TestCancelReservation06() throws IllegalArgumentException, IllegalAccessException {

    // setup parameters
    TransactionTokenInfo endTransactionToken = new TransactionTokenInfo("testTokenAttribute3",
            TransactionTokenType.END);/*  w w  w . ja v  a  2 s . c  o m*/
    TransactionToken receivedToken = new TransactionToken("ccc", "key", "value");

    // setup up expected result
    ReserveCommand expectedCommand = ReserveCommand.REMOVE_TOKEN;

    // run
    TransactionTokenContextImpl contextImpl = new TransactionTokenContextImpl(endTransactionToken,
            receivedToken);

    contextImpl.cancelReservation();

    // test
    Field field = ReflectionUtils.findField(TransactionTokenContextImpl.class, "defaultCommand");
    ReflectionUtils.makeAccessible(field);
    ReserveCommand resultCommand = (ReserveCommand) field.get(contextImpl);
    assertThat(resultCommand, is(expectedCommand));
}