Example usage for org.apache.commons.lang3.reflect FieldUtils writeField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils writeField

Introduction

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

Prototype

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

Source Link

Document

Writes a Field .

Usage

From source file:org.edgexfoundry.engine.RuleCreatorTest.java

@Test(expected = Exception.class)
public void testCreateDroolRuleException() throws IllegalAccessException, TemplateException, IOException {
    FieldUtils.writeField(creator, "cfg", null, true);
    creator.createDroolRule(rule);/*from ww  w . j  a  v a  2  s . c  om*/
}

From source file:org.edgexfoundry.engine.RunEngineTest.java

@Before
public void setup() throws IllegalAccessException {
    MockitoAnnotations.initMocks(this);
    FieldUtils.writeField(engine, "resourceFilePath", TEST_RESOURCE_FILE_PATH, true);
    FieldUtils.writeField(engine, "packageName", TEST_PACKAGE_NAME, true);
    FieldUtils.writeField(engine, "ruleFileExtension", TEST_RULE_FILE_EXT, true);
}

From source file:org.edgexfoundry.integration.EdgeXConfigSeedTest.java

@Before
public void setup() throws IllegalAccessException {
    MockitoAnnotations.initMocks(this);
    List<String> yamlExtensions = new ArrayList<>();
    yamlExtensions.add(".yaml");
    yamlExtensions.add(".yml");
    FieldUtils.writeField(seed, "yamlExtensions", yamlExtensions, true);
    FieldUtils.writeField(seed, "protocol", CONSUL_PROTOCOL, true);
    FieldUtils.writeField(seed, "host", CONSUL_HOST, true);
    FieldUtils.writeField(seed, "port", CONSUL_PORT, true);
    FieldUtils.writeField(seed, "globalPrefix", CONSUL_GLOBAL_PREFIX, true);
    FieldUtils.writeField(seed, "failLimit", FAIL_LIMIT, true);
    FieldUtils.writeField(seed, "waitTimeBetweenFails", WAIT_TIME, true);
    FieldUtils.writeField(seed, "configPath", CONFIG_PATH, true);
    List<String> acceptableFileExtenstions = new ArrayList<>();
    acceptableFileExtenstions.addAll(yamlExtensions);
    acceptableFileExtenstions.add(".properties");
    FieldUtils.writeField(seed, "acceptablePropertyExtensions", acceptableFileExtenstions, true);
    consul = seed.getConsul();/*w  ww  .j a  va2s. c  om*/
    assertNotNull("Unable to get Consul instance for testing (check that Consul is running).", consul);
    kvClient = consul.keyValueClient();
    assertNotNull("Key Value Client is null.  Can't run tests", kvClient);
}

From source file:org.edgexfoundry.integration.EdgeXConfigSeedTest.java

@Test
public void testConsulConnectWithRetryFailLimitExceeded() throws InterruptedException, IllegalAccessException {
    FieldUtils.writeField(seed, "failLimit", -1, true);
    assertNull("Consul obtained when fail limit exceeded", seed.consulWithRetry());
}

From source file:org.edgexfoundry.integration.EdgeXConfigSeedTest.java

@Test
public void testConsulConnectWithRetryExhaustedTries() throws InterruptedException, IllegalAccessException {
    FieldUtils.writeField(seed, "host", "1.1.1.1", true);// set bad host
    assertNull("Consul obtained when fail limit exceeded", seed.consulWithRetry());
}

From source file:org.edgexfoundry.integration.EdgeXConfigSeedTest.java

@Test(expected = IOException.class)
public void testLoadConfigWithBadPath() throws IllegalAccessException, IOException {
    FieldUtils.writeField(seed, "configPath", "foobar", true);
    seed.loadConfigFromPath(kvClient);//ww  w  .  j  a  v  a2 s . co  m
}

From source file:org.force66.beantester.tests.ValuePropertyTest.java

@Override
public boolean testProperty(Object bean, PropertyDescriptor descriptor, Object value) {
    Validate.notNull(bean, "Null bean not allowed");
    Validate.notNull(descriptor, "Null PropertyDescriptor not allowed");

    boolean answer = true;
    if (descriptor.getPropertyType().isPrimitive() && value == null) {
        return answer; // Null test doesn't apply
    }/*  w  w  w  .ja v  a2  s  . c  om*/

    boolean fieldExists = FieldUtils.getField(bean.getClass(), descriptor.getName(), true) != null;

    try {
        if (descriptor.getWriteMethod() != null) {
            descriptor.getWriteMethod().invoke(bean, new Object[] { value });
            answer = testReadValue(bean, descriptor, value);
        } else if (fieldExists) {
            /*
             * Accessor exists, but no mutator.  Test the accessor by forcing the test value into the field
             * backing that accessor.
             */
            FieldUtils.writeField(bean, descriptor.getName(), value, true);
            answer = testReadValue(bean, descriptor, value);
        }
        if (descriptor.getReadMethod() != null) {
            /*
             * If an accessor exists, but has no corresponding mutator or field, all we can do
             * is execute it to make sure it doesn't except.
             */
            descriptor.getReadMethod().invoke(bean);
        }

    } catch (Exception e) {
        throw new BeanTesterException("Failed executing assignment test for accessor/mutator", e)
                .addContextValue("property", descriptor)
                .addContextValue("value class", value == null ? null : value.getClass().getName())
                .addContextValue("value", value);
    }
    return answer;
}

From source file:org.force66.vobase.ValueObjectBase.java

private void assignValue(ValueObjectBase obj, Arg arg) {
    try {/*from w w  w.  j  a  v a  2  s.  co  m*/
        FieldUtils.writeField(obj, arg.getFieldName(), arg.getValue(), true);
    } catch (Exception e) {
        throw new ValueObjectException("Problem assigning value object field value", e)
                .addContextValue("named field argument", arg);
    }
}

From source file:org.force66.vobase.ValueObjectBaseTest.java

@Before
public void setUp() throws Exception {
    ValueGeneratorFactory valueFactory = new ValueGeneratorFactory();
    populatedVO = new TestVO();

    for (Field field : TestVO.class.getDeclaredFields()) {
        if (!Modifier.isFinal(field.getModifiers())) {
            ValueGenerator valueGen = valueFactory.forClass(field.getType());
            if (valueGen != null) {
                FieldUtils.writeField(field, populatedVO, valueGen.makeValues()[0], true);
            }/* w  w  w  . ja  v a2  s  .  c  o  m*/
        }
    }
}

From source file:org.force66.vobase.ValueObjectUtils.java

private static Object copyField(Object source, Object target, Object value, Field field) {
    try {/*from w w  w.j a v a 2  s  .  c  o  m*/
        value = FieldUtils.readField(source, field.getName(), true);
    } catch (Exception e) {
        throw new ValueObjectException("Problem reading value object field value", e)
                .addContextValue("fieldName", field.getName())
                .addContextValue("className", source.getClass().getName());
    }

    try {
        FieldUtils.writeField(target, field.getName(), value, true);
    } catch (Exception e) {
        throw new ValueObjectException("Problem setting value object field value", e)
                .addContextValue("fieldName", field.getName())
                .addContextValue("className", target.getClass().getName());
    }
    return value;
}