Example usage for org.springframework.util ReflectionUtils setField

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

Introduction

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

Prototype

public static void setField(Field field, @Nullable Object target, @Nullable Object value) 

Source Link

Document

Set the field represented by the supplied Field field object on the specified Object target object to the specified value .

Usage

From source file:com.github.javarch.support.log.LoggingAnnotationBeanPostProcessor.java

private void injectLogger(Object bean, Field field) {
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, bean, LoggerFactory.getLogger(field.getDeclaringClass()));
}

From source file:com.orange.clara.cloud.boot.ssl.SslDefaultTrustStoreAppenderListenerTest.java

@Before
public void setup() throws IllegalArgumentException, IllegalAccessException {
    sslTrustStoreGeneratorListener = new SslTrustStoreGeneratorListener();
    Field envField = ReflectionUtils.findField(SslTrustStoreGeneratorListener.class, "propertyResolver");
    ReflectionUtils.makeAccessible(envField);
    ReflectionUtils.setField(envField, sslTrustStoreGeneratorListener, propertyResolver);
    Field sslCertTrusterField = ReflectionUtils.findField(SslTrustStoreGeneratorListener.class,
            "trustStoreAppender");
    ReflectionUtils.makeAccessible(sslCertTrusterField);
    ReflectionUtils.setField(sslCertTrusterField, sslTrustStoreGeneratorListener, trustStoreAppender);
}

From source file:org.eclipse.gemini.blueprint.test.ConfigurableBundleCreatorTestsTest.java

public void testDefaultJarSettings() throws Exception {

    Properties defaultSettings = bundleCreator.getSettings();
    Field field = ReflectionUtils.findField(AbstractConfigurableBundleCreatorTests.class, "jarSettings",
            Properties.class);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, null, defaultSettings);
    assertNotNull(defaultSettings);/*from w  ww  .j  a  v  a  2  s  .  co m*/
    assertNotNull(bundleCreator.getRootPath());
    assertNotNull(bundleCreator.getBundleContentPattern());
    assertNotNull(bundleCreator.getManifestLocation());
}

From source file:org.jdal.annotation.AnnotatedElementAccessor.java

/**
 * Try to set a value on AnnotatedElement.
 * @param element the annotated element.
 * @param value value to set.//w  ww  .j ava2 s  .c  o m
 */
public static void setValue(AnnotatedElement element, Object target, Object value) {
    if (element instanceof Field) {
        Field field = (Field) element;
        ReflectionUtils.makeAccessible(field);
        ReflectionUtils.setField(field, target, value);
    } else if (element instanceof Method) {
        Method method = (Method) element;
        try {
            method.invoke(target, new Object[] { value });
        } catch (Exception e) {
            log.error("Cannot set value on method [" + method.toString() + "]");
        }
    }
}

From source file:com.art4ul.jcoon.bean.RestClientAnnotationBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    Field[] fields = bean.getClass().getDeclaredFields();
    for (Field field : fields) {
        RestClient restClient = field.getAnnotation(RestClient.class);
        if (restClient != null) {
            field.setAccessible(true);//from  w  w  w  .  j  ava 2  s  .  c om
            Object proxyInstance = createProxyInstance(bean.getClass().getClassLoader(), restClient.value(),
                    field.getType()); //  TODO: Optimization , do not create new instance for same classes
            ReflectionUtils.setField(field, bean, proxyInstance);
        }
    }
    return bean;
}

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

@BeforeClass
public static void setUp() {
    conservativeMutationAlgorithm = new ConservativeMutationAlgorithm();

    geneDaoMock = mock(VariableLengthGeneDao.class);
    conservativeMutationAlgorithm.setGeneDao(geneDaoMock);

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

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

@BeforeClass
public static void setUp() {
    singleSequenceMutationAlgorithm = new SingleSequenceMutationAlgorithm();

    sequenceDaoMock = mock(SequenceDao.class);
    singleSequenceMutationAlgorithm.setSequenceDao(sequenceDaoMock);

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

From source file:org.web4thejob.orm.serial.MyXStreamMarshaller.java

private void customizeMappers(XStream xstream) {
    // huge HACK since there seems to be no direct way of adding
    // HibernateMapper
    final Converter reflectionConverter = xstream.getConverterLookup().lookupConverterForType(Entity.class);

    if (!ReflectionConverter.class.isInstance(reflectionConverter))
        throw new IllegalStateException("expected " + ReflectionConverter.class.getName() + " but got "
                + reflectionConverter.getClass().getName());

    final Field field = ReflectionUtils.findField(ReflectionConverter.class, "mapper");
    ReflectionUtils.makeAccessible(field);
    CachingMapper mapper = (CachingMapper) ReflectionUtils.getField(field, reflectionConverter);
    mapper = new CachingMapper(new MyHibernateMapper(mapper));
    ReflectionUtils.setField(field, reflectionConverter, mapper);
}

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

/**
 * //w w  w  .j a v  a 2 s.co  m
 * @param ri
 * @return
 */
private Object getRequest(Class<?> clazz, String id) {

    try {
        Object o = clazz.newInstance();
        Field idF = KarakuReflectionUtils.findField(clazz, FIELDS);
        notNull(idF, "Cant get the Sync ID in the request, " + "please create a field with name id or use"
                + "@ReplicationId, see '%s'", clazz);
        idF.setAccessible(true);
        ReflectionUtils.setField(idF, o, id);
        return o;
    } catch (Exception e) {
        throw new KarakuRuntimeException("Cant create request for ReplicateionInfo with id: " + id, e);
    }

}

From source file:com.ciphertool.zodiacengine.entities.SolutionChromosomeTest.java

@BeforeClass
public static void setUp() {
    BigInteger cipherId = new BigInteger("12345");
    cipher.setId(cipherId);/*  ww  w  .j  a  v a  2  s . c om*/

    logMock = mock(Logger.class);

    Field logField = ReflectionUtils.findField(SolutionChromosome.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, null, logMock);
}