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

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

Introduction

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

Prototype

public static Object readField(final Object target, final String fieldName, final boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Reads the named Field .

Usage

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  av 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;
}

From source file:org.initialize4j.service.InitializeServiceImpl.java

/**
 * Handles the field initialization of a given object.
 * /*from w  w w  . j a  va  2s  .c  o m*/
 * @param bean The object in the scope of initialization.
 * @param field The field which maybe gets initialized.
 * @param scopes An optional list of scopes to allow.
 */
private void initializeBeanFieldWithScopes(Object bean, Field field, String... scopes) throws Exception {
    Initialize initialize = field.getAnnotation(Initialize.class);

    if (scopes.length > 0 && !new InitializeScopeInspector().isScopeSatisfied(initialize, scopes)) {
        return;
    }

    Object value = FieldUtils.readField(field, bean, true);
    if (!new InitializePreconditionInspector().isPreconditionSatified(value, field)) {
        return;
    }
    CommandBuilder.of(initialize).useBean(bean).useField(field).build().execute();
}

From source file:org.jodconverter.document.DumpJsonDefaultDocumentFormatRegistry.java

public static void main(final String[] args) throws Exception {

    final DocumentFormatRegistry registry = DefaultDocumentFormatRegistry.getInstance();
    @SuppressWarnings("unchecked")
    final TreeMap<String, DocumentFormat> formats = new TreeMap<>(
            (Map<String, DocumentFormat>) FieldUtils.readField(registry, "fmtsByExtension", true));

    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    LOGGER.info(gson.toJson(formats.values()));
}

From source file:org.jodconverter.office.OfficeProcessManagerPoolEntryITest.java

private static OfficeProcess getOfficeProcess(final OfficeProcessManagerPoolEntry manager)
        throws IllegalAccessException {

    final OfficeProcessManager processManager = (OfficeProcessManager) FieldUtils.readField(manager,
            "officeProcessManager", true);
    return (OfficeProcess) FieldUtils.readField(processManager, "process", true);
}

From source file:org.jodconverter.office.OfficeProcessManagerPoolEntryITest.java

private static OfficeConnection getConnection(final OfficeProcessManagerPoolEntry manager)
        throws IllegalAccessException {

    final OfficeProcessManager processManager = (OfficeProcessManager) FieldUtils.readField(manager,
            "officeProcessManager", true);
    return (OfficeConnection) FieldUtils.readField(processManager, "connection", true);
}

From source file:org.jodconverter.office.OfficeProcessManagerPoolEntryITest.java

/**
 * Tests that an office process is restarted successfully after a crash.
 *
 * @throws Exception if an error occurs.
 *///from  w ww.  j av a 2s .c  om
@Test
public void execute_WhenOfficeProcessCrash_ShouldRestartAfterCrash() throws Exception {

    final OfficeProcessManagerPoolEntry officeManager = new OfficeProcessManagerPoolEntry(CONNECT_URL);

    try {
        officeManager.start();
        assertThat(officeManager.isRunning()).isTrue();
        assertThat(officeManager)
                .extracting("officeProcessManager.process.running", "officeProcessManager.connection.connected")
                .containsExactly(true, true);

        // Submit the task to an executor
        final ExecutorService pool = Executors.newFixedThreadPool(1);
        try {
            final Callable<Boolean> task = new RestartAfterCrashTask(officeManager);
            final Future<Boolean> future = pool.submit(task);

            Thread.sleep(500); // NOSONAR

            // Simulate crash
            final Process underlyingProcess = (Process) FieldUtils.readField(getOfficeProcess(officeManager),
                    "process", true);
            assertThat(underlyingProcess).isNotNull();
            LOGGER.debug("Simulating the crash");
            underlyingProcess.destroy();

            // Wait until the task is completed
            try {
                future.get();
                fail("Exception expected");
            } catch (ExecutionException ex) {
                assertThat(ex.getCause()).isInstanceOf(OfficeException.class);
                assertThat(ex.getCause().getCause()).isInstanceOf(CancellationException.class);
            }

        } finally {
            pool.shutdownNow();
        }

        assertRestartedAndReconnected(officeManager, RESTART_INITIAL_WAIT, RESTART_WAIT_TIMEOUT);

        final MockOfficeTask goodTask = new MockOfficeTask();
        officeManager.execute(goodTask);
        assertThat(goodTask.isCompleted()).isTrue();

    } finally {

        officeManager.stop();
        assertThat(officeManager.isRunning()).isFalse();
        assertThat(officeManager)
                .extracting("officeProcessManager.process.running", "officeProcessManager.connection.connected")
                .containsExactly(false, false);
        assertThat(getOfficeProcess(officeManager).getExitCode(0, 0)).isEqualTo(0);
    }
}

From source file:org.jodconverter.office.OfficeProcessManagerPoolEntryITest.java

@Test
public void start_WithCustomProfileDir_ShouldCopyProfileDirToWorkingDir() throws Exception {

    final OfficeProcessManagerPoolEntryConfig config = new OfficeProcessManagerPoolEntryConfig();
    config.setOfficeHome(LocalOfficeUtils.getDefaultOfficeHome());
    config.setWorkingDir(new File(System.getProperty("java.io.tmpdir")));
    config.setOfficeHome(LocalOfficeUtils.getDefaultOfficeHome());
    config.setTemplateProfileDir(new File("src/integTest/resources/templateProfileDir"));

    final OfficeProcessManagerPoolEntry officeManager = new OfficeProcessManagerPoolEntry(CONNECT_URL, config);
    try {/* w  w  w. j  a v a  2s.c o  m*/
        officeManager.start();
        assertThat(officeManager.isRunning()).isTrue();
        assertThat(officeManager)
                .extracting("officeProcessManager.process.running", "officeProcessManager.connection.connected")
                .containsExactly(true, true);

        // Check the profile dir existence
        final File instanceProfileDir = (File) FieldUtils.readField(getOfficeProcess(officeManager),
                "instanceProfileDir", true);
        assertThat(new File(instanceProfileDir, "user/customFile")).isFile();

    } finally {

        officeManager.stop();
        assertThat(officeManager.isRunning()).isFalse();
        assertThat(officeManager)
                .extracting("officeProcessManager.process.running", "officeProcessManager.connection.connected")
                .containsExactly(false, false);
        assertThat(getOfficeProcess(officeManager).getExitCode(0, 0)).isEqualTo(0);
    }
}

From source file:org.jodconverter.process.ProcessManagerITest.java

/**
 * Tests that using an custom process manager that appears in the classpath will be used.
 *
 * @throws Exception if an error occurs.
 *//*from w  ww .j  a  v a2  s.  c o m*/
@Test
public void customProcessManager() throws Exception {

    final LocalOfficeManager officeManager = LocalOfficeManager.builder()
            .processManager("org.jodconverter.process.CustomProcessManager").build();

    final Object config = FieldUtils.readField(officeManager, "config", true);
    final ProcessManager manager = (ProcessManager) FieldUtils.readField(config, "processManager", true);
    assertThat(manager).isExactlyInstanceOf(CustomProcessManager.class);
}

From source file:org.ktc.soapui.maven.extension.impl.ErrorHandler.java

private static List<TestCase> getFailedTests(SoapUITestCaseRunner runner) {
    String fieldName = "failedTests";
    Field field = FieldUtils.getField(SoapUITestCaseRunner.class, fieldName, true);
    try {/* www.  j  a  v a  2 s .c  om*/
        @SuppressWarnings("unchecked")
        List<TestCase> failedTests = (List<TestCase>) FieldUtils.readField(field, runner, true);
        return failedTests;
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Unable to read field " + fieldName, e);
    }
}

From source file:org.ktc.soapui.maven.extension.impl.ErrorHandler.java

private static List<TestAssertion> getFailedAssertions(SoapUITestCaseRunner runner) {
    String fieldName = "assertions";
    Field field = FieldUtils.getField(SoapUITestCaseRunner.class, fieldName, true);
    try {/*from   w w w.j a v  a  2  s  . com*/
        @SuppressWarnings("unchecked")
        List<TestAssertion> failedAssertionss = (List<TestAssertion>) FieldUtils.readField(field, runner, true);
        return failedAssertionss;
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Unable to read field " + fieldName, e);
    }
}