Example usage for org.apache.commons.lang.reflect FieldUtils writeDeclaredField

List of usage examples for org.apache.commons.lang.reflect FieldUtils writeDeclaredField

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect FieldUtils writeDeclaredField.

Prototype

public static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Write a public field.

Usage

From source file:org.apache.servicecomb.config.archaius.sources.TestConfigCenterConfigurationSource.java

@Test
public void destroy_inited() throws IllegalAccessException {
    AtomicInteger count = new AtomicInteger();
    ConfigCenterClient configCenterClient = new MockUp<ConfigCenterClient>() {
        @Mock//from w  w  w .  j a v  a2 s . com
        void destroy() {
            count.incrementAndGet();
        }
    }.getMockInstance();
    ConfigCenterConfigurationSourceImpl configCenterSource = new ConfigCenterConfigurationSourceImpl();
    FieldUtils.writeDeclaredField(configCenterSource, "configCenterClient", configCenterClient, true);

    configCenterSource.destroy();

    Assert.assertEquals(1, count.get());
}

From source file:org.gradle.util.JavaReflectionUtil.java

public static void setProperty(Object target, String property, Object value) {
    try {/*w  w  w. ja v a2s .  co  m*/
        FieldUtils.writeDeclaredField(target, property, value, true);
    } catch (IllegalAccessException e) {
        throw new UncheckedException(e);
    }
}

From source file:org.openengsb.core.ekb.persistence.persist.edb.TestTransformationEngine.java

@Override
public Object performTransformation(ModelDescription sourceClass, ModelDescription targetClass, Object source,
        Object target) {//from   w w  w .ja va2  s.c om
    TestModelRegistry registry = new TestModelRegistry();
    try {
        Class<?> clazz = registry.loadModel(sourceClass);
        for (Field field : clazz.getDeclaredFields()) {
            String fieldName = field.getName();
            if (fieldName.equals(ModelUtils.MODEL_TAIL_FIELD_NAME) || fieldName.contains("LOGGER")) {
                continue;
            }
            Object value = FieldUtils.readDeclaredField(source, fieldName, true);
            if (value != null) {
                if (FieldUtils.getDeclaredField(target.getClass(), fieldName, true) == null) {
                    continue;
                }
                FieldUtils.writeDeclaredField(target, fieldName, value, true);
            }
        }
        return target;
    } catch (Exception e) {
        throw new EKBException("Unable to perform test transformation", e);
    }
}

From source file:org.pentaho.di.ui.repository.repositoryexplorer.controllers.ConnectionsControllerTest.java

@Before
public void setUp() throws Exception {
    // a tricky initialisation - first inject private fields
    controller = new ConnectionsController();
    connectionsTable = mock(XulTree.class);
    FieldUtils.writeDeclaredField(controller, "connectionsTable", connectionsTable, true);

    // and then spy the controller
    controller = spy(controller);//from w w  w.ja  v  a 2  s  .  c  om

    databaseDialog = mock(DatabaseDialog.class);
    doReturn(databaseDialog).when(controller).getDatabaseDialog();
    doNothing().when(controller).refreshConnectionList();
    doNothing().when(controller).showAlreadyExistsMessage();

    repository = mock(Repository.class);
    controller.init(repository);
}

From source file:pl.bristleback.server.bristle.conf.resolver.message.ObjectSenderInjector.java

private void injectProperty(Object bean, Field field, ConditionObjectSender objectSender) {
    try {/*from  w  w  w. j  a va  2  s  .  c  om*/
        FieldUtils.writeDeclaredField(bean, field.getName(), objectSender, true);
    } catch (IllegalAccessException e) {
        throw new BeanInstantiationException(bean.getClass(), e.getMessage(), e);
    }
}