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.lesscss.LessCompilerTest.java

@Test
public void testCompress() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    lessCompiler.setCompress(true);/*from  ww  w. jav  a  2s  .  c om*/
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "doIt", doIt, true);

    when(doIt.call(cx, scope, null, new Object[] { less, true })).thenReturn(css);

    assertEquals(css, lessCompiler.compile(less));

    verify(doIt).call(cx, scope, null, new Object[] { less, true });
}

From source file:org.lesscss.LessCompilerTest.java

@Test
public void testEncoding() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    lessCompiler.setEncoding("utf-8");
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "doIt", doIt, true);

    whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource);
    when(lessSource.getNormalizedContent()).thenReturn(less);

    when(doIt.call(cx, scope, null, new Object[] { less, false })).thenReturn(css);

    mockStatic(FileUtils.class);

    lessCompiler.compile(inputFile, outputFile);

    verifyNew(LessSource.class).withArguments(inputFile);
    verify(lessSource).getNormalizedContent();

    verify(doIt).call(cx, scope, null, new Object[] { less, false });

    verifyStatic();/*from  w w  w  .  jav a2  s. co  m*/
    FileUtils.writeStringToFile(outputFile, css, "utf-8");
}

From source file:org.lesscss.LessSourceTest.java

@Test
public void testLastModifiedIncludingImportsWhenNoImportModifiedLater() throws Exception {
    when(file.exists()).thenReturn(true);
    mockStatic(FileUtils.class);
    when(FileUtils.readFileToString(file)).thenReturn(content);
    when(file.getAbsolutePath()).thenReturn(absolutePath);
    when(file.lastModified()).thenReturn(1l);

    when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import2.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import3.getLastModifiedIncludingImports()).thenReturn(0l);

    lessSource = new LessSource(file);
    FieldUtils.writeField(lessSource, "imports", imports, true);

    assertEquals(1l, lessSource.getLastModifiedIncludingImports());
}

From source file:org.lesscss.LessSourceTest.java

@Test
public void testLastModifiedIncludingImportsWhenImportModifiedLater() throws Exception {
    when(file.exists()).thenReturn(true);
    mockStatic(FileUtils.class);
    when(FileUtils.readFileToString(file)).thenReturn(content);
    when(file.getAbsolutePath()).thenReturn(absolutePath);
    when(file.lastModified()).thenReturn(1l);

    when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import2.getLastModifiedIncludingImports()).thenReturn(2l);
    when(import3.getLastModifiedIncludingImports()).thenReturn(0l);

    lessSource = new LessSource(file);
    FieldUtils.writeField(lessSource, "imports", imports, true);

    assertEquals(2l, lessSource.getLastModifiedIncludingImports());
}

From source file:org.openlmis.fulfillment.web.validator.FtpTransferPropertiesValidatorTest.java

@Before
public void setUp() throws IllegalAccessException {
    FieldUtils.writeField(messageService, "messageSource", messageSource, true);
    when(messageSource.getMessage(anyString(), any(), any())).thenReturn("message");
    properties = new FtpTransferProperties();
    properties.setProtocol(FtpProtocol.FTP);
    properties.setUsername("name");
    properties.setPassword("password");
    properties.setServerPort(80);//from ww  w  .  j  a v  a2s .c om
    properties.setLocalDirectory("/home");
    properties.setRemoteDirectory("/remote");
    properties.setServerHost("host");
    properties.setPassiveMode(true);
}

From source file:org.openlmis.fulfillment.web.validator.LocalTransferPropertiesValidatorTest.java

@Before
public void setUp() throws IllegalAccessException {
    FieldUtils.writeField(messageService, "messageSource", messageSource, true);
    when(messageSource.getMessage(anyString(), any(), any())).thenReturn("message");
    properties = new LocalTransferProperties();
}

From source file:org.openlmis.fulfillment.web.validator.ProofOfDeliveryValidatorTest.java

@Before
public void setUp() throws Exception {
    FieldUtils.writeField(messageService, "messageSource", messageSource, true);

    line = new ProofOfDeliveryLineItem();
    line.setQuantityReceived(10L);/*  w w  w .  j av  a2 s  . co m*/

    pod = new ProofOfDelivery();
    pod.setDeliveredBy("Deliver guy");
    pod.setReceivedBy("Receiver");
    pod.setReceivedDate(ZonedDateTime.now());
    pod.setProofOfDeliveryLineItems(Lists.newArrayList(line));

    mockMessageSource(VALIDATION_ERROR_MUST_CONTAIN_VALUE, DELIVERED_BY, MUST_CONTAIN_A_VALUE);
    mockMessageSource(VALIDATION_ERROR_MUST_CONTAIN_VALUE, RECEIVED_BY, MUST_CONTAIN_A_VALUE);
    mockMessageSource(VALIDATION_ERROR_MUST_CONTAIN_VALUE, RECEIVED_DATE, MUST_CONTAIN_A_VALUE);
    mockMessageSource(VALIDATION_ERROR_MUST_CONTAIN_VALUE,
            PROOF_OF_DELIVERY_LINE_ITEMS + '.' + QUANTITY_RECEIVED, MUST_CONTAIN_A_VALUE);
    mockMessageSource(VALIDATION_ERROR_MUST_BE_GREATER_THAN_OR_EQUAL_TO_ZERO,
            PROOF_OF_DELIVERY_LINE_ITEMS + '.' + QUANTITY_RECEIVED,
            MUST_CONTAIN_A_VALUE_THAT_IS_GREATER_THAN_OR_EQUAL_TO_ZERO);
}

From source file:org.openmrs.module.sync.SyncUtil.java

public static void setProperty(Object o, String propName, Object propVal)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object[] setterParams = new Object[] { propVal };

    log.debug("getting setter method");
    Method m = SyncUtil.getSetterMethod(o.getClass(), propName, propVal.getClass());
    if (m == null) {
        // We couldn't find a setter method. Let's try setting the field directly instead.
        log.debug("couldn't find setter method, setting field '" + propName + "' directly.");
        FieldUtils.writeField(o, propName, propVal, true);
        return;//from  ww w  . jav a2 s  .  c o m
    }

    boolean acc = m.isAccessible();
    m.setAccessible(true);
    log.debug("about to call " + m.getName());
    try {
        m.invoke(o, setterParams);
    } finally {
        m.setAccessible(acc);
    }
}

From source file:org.pepstock.jem.annotations.SetFields.java

private static void setFieldByAnnotation(InitialContext ic, Object object, String name, Field field)
        throws NamingException, IllegalAccessException {
    // gets object via JNDI
    Object objectJNDI = (Object) ic.lookup(name);
    // if is static or it's a java main class (object = null) sets statically
    if (Modifier.isStatic(field.getModifiers()) || object == null) {
        FieldUtils.writeStaticField(field, objectJNDI, true);
    } else {//from   w  w w.j  a  v a  2  s  . co m
        // sets field
        FieldUtils.writeField(field, object, objectJNDI, true);
    }
}

From source file:org.pepstock.jem.springbatch.tasks.utilities.LauncherTasklet.java

/**
 * Assigns the value of step contribution and chunk context 
 * @param clazz class for reflection//w  w w  .j ava2  s. com
 * @param stepContribution step contribution, passed by SpringBatch core
 * @param chunkContext chunk context, passed by SpringBatch core
 * @throws IllegalAccessException if any error occurs
 */
private void applyByAnnotation(Class<?> clazz, Object instance, StepContribution stepContribution,
        ChunkContext chunkContext) throws IllegalAccessException {
    // scans all declared fields
    for (Field field : clazz.getDeclaredFields()) {
        // if has got data description annotation
        if (field.isAnnotationPresent(AssignStepContribution.class)) {
            if (Modifier.isStatic(field.getModifiers())) {
                FieldUtils.writeStaticField(field, stepContribution, true);
            } else {
                FieldUtils.writeField(field, instance, stepContribution, true);
            }
        } else if (field.isAnnotationPresent(AssignChunkContext.class)) {
            if (Modifier.isStatic(field.getModifiers())) {
                FieldUtils.writeStaticField(field, chunkContext, true);
            } else {
                FieldUtils.writeField(field, instance, chunkContext, true);
            }
        }
    }
}