Example usage for org.springframework.util ReflectionUtils getField

List of usage examples for org.springframework.util ReflectionUtils getField

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils getField.

Prototype

@Nullable
public static Object getField(Field field, @Nullable Object target) 

Source Link

Document

Get the field represented by the supplied Field field object on the specified Object target object .

Usage

From source file:py.una.pol.karaku.replication.client.ReplicationResponseHandler.java

/**
 * @param response//from   www  . j av a2 s . co  m
 * @return
 */
private String getLastId(Object response) {

    notNull(response, "Cant get id from null response");
    Class<?> clazz = notNull(response.getClass());

    Field f = KarakuReflectionUtils.findField(clazz, ID_FIELDS);

    notNull(f,
            "Cant get the id field, please use the @ReplicationId "
                    + "annotation, or create a field with name %s, see %s",
            Arrays.toString(ID_FIELDS), response.getClass());

    f.setAccessible(true);

    Object id = ReflectionUtils.getField(f, response);
    notNull(id, "Id null in response is not allowed");

    return id.toString();
}

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   ww w.  j ava 2 s  . c om

    assertEquals(persistenceBatchSizeToSet, persistenceBatchSizeFromObject);
}

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);/* w w w  . j  a v a 2s .co m*/

}

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:com.agapple.asyncload.impl.pool.AsyncLoadThreadPool.java

@Override
public void execute(Runnable command) {
    if (command instanceof AsyncLoadFuture) {
        AsyncLoadFuture afuture = (AsyncLoadFuture) command;
        boolean flag = afuture.getConfig().getNeedThreadLocalSupport();
        if (flag) {
            Thread thread = Thread.currentThread();
            if (ReflectionUtils.getField(threadLocalField, thread) == null) {
                // ThreadLocal,
                new ThreadLocal<Boolean>(); // runnerThreadLocalMap
            }//from   w  w w.ja v a 2s. co  m
            if (ReflectionUtils.getField(inheritableThreadLocalField, thread) == null) {
                // ThreadLocal,
                new InheritableThreadLocal<Boolean>(); // ?ThreadLocal
            }
        }
    }

    super.execute(command);// ??
}

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   www  .  ja  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:io.pivotal.poc.gateway.filters.pre.FileClaimCheckFilter.java

@Override
public void filter(RequestContext ctx) {
    StandardMultipartHttpServletRequest multipartRequest = this.extractMultipartRequest(ctx.getRequest());
    Map<String, String> uploadedFileMap = new HashMap<>();
    for (Map.Entry<String, MultipartFile> entry : multipartRequest.getFileMap().entrySet()) {
        String fileKey = entry.getKey();
        String claimCheck = store.save(new MultipartFileResource(entry.getValue()));
        uploadedFileMap.put(fileKey, claimCheck);
    }/*from  w ww  .  j  av a  2  s . co  m*/
    try {
        String json = this.mapper.writeValueAsString(uploadedFileMap);
        FileClaimCheckRequestWrapper wrapper = null;
        HttpServletRequest request = ctx.getRequest();
        if (request instanceof HttpServletRequestWrapper) {
            HttpServletRequest wrapped = (HttpServletRequest) ReflectionUtils.getField(this.requestField,
                    request);
            if (wrapped instanceof HttpServletRequestWrapper) {
                wrapped = ((HttpServletRequestWrapper) wrapped).getRequest();
            }
            wrapper = new FileClaimCheckRequestWrapper(json, wrapped);
            ReflectionUtils.setField(this.requestField, request, wrapper);
            ReflectionUtils.setField(this.contentDataField, request, json.getBytes());
            if (request instanceof ServletRequestWrapper) {
                ReflectionUtils.setField(this.servletRequestField, request, wrapper);
            }
        } else {
            wrapper = new FileClaimCheckRequestWrapper(json, request);
            ctx.setRequest(wrapper);
        }
        if (wrapper != null) {
            ctx.getZuulRequestHeaders().put("content-type", wrapper.getContentType());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    log.info(String.format("%s request to %s", multipartRequest.getMethod(),
            multipartRequest.getRequestURL().toString()));
}

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

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

    Field fileParserField = ReflectionUtils.findField(WordListImporterImpl.class, "partOfSpeechFileParser");
    ReflectionUtils.makeAccessible(fileParserField);
    FileParser<Word> fileParserFromObject = (FileParser<Word>) ReflectionUtils.getField(fileParserField,
            wordListImporterImpl);//from  w  ww  .j a  v  a 2s . c om

    assertSame(fileParserToSet, fileParserFromObject);
}