Example usage for java.lang.reflect Field set

List of usage examples for java.lang.reflect Field set

Introduction

In this page you can find the example usage for java.lang.reflect Field set.

Prototype

@CallerSensitive
@ForceInline 
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the field represented by this Field object on the specified object argument to the specified new value.

Usage

From source file:org.shareok.data.kernel.api.services.islandora.IslandoraSshServiceImpl.java

@Override
public void loadJobInfoByJob(RedisJob job) {
    long jobId = job.getJobId();
    int jobType = job.getType();
    handler.setJobType(job.getType());/*from  w w w  . java  2 s .c  o m*/
    String jobFilePath = DataHandlersUtil.getJobReportPath(DataUtil.JOB_TYPES[jobType], jobId);
    handler.setReportFilePath(jobFilePath + File.separator + String.valueOf(jobId) + "-report.txt");
    handler.setServerId(String.valueOf(job.getServerId()));
    String schema = (String) DataUtil.JOB_TYPE_DATA_SCHEMA.get(DataUtil.JOB_TYPES[job.getType()]);
    Map data = RedisUtil.getJobDao().getJobInfoByAttributes(jobId, schema.split(","));
    for (Field f : IslandoraSshHandler.class.getDeclaredFields()) {
        try {
            String fieldName = f.getName();
            if (data.containsKey(fieldName)) {
                f.set(this, (String) data.get(fieldName));
            }
        } catch (SecurityException ex) {
        } catch (IllegalArgumentException | IllegalAccessException ex) {
        }
    }
}

From source file:com.cloud.api.ApiServletTest.java

/**
 * These are envinonment hacks, actually getting into the behavior of other
 * classes, but there is no other way to run the test.
 *//*  w  w  w  .ja  va  2s  .  c  o m*/
@Before
public void hackEnvironment() throws Exception {
    Field smsField = ApiDBUtils.class.getDeclaredField("s_ms");
    smsField.setAccessible(true);
    smsField.set(null, managementServer);
    Mockito.when(managementServer.getVersion()).thenReturn("LATEST-AND-GREATEST");
}

From source file:org.shareok.data.kernel.api.services.dspace.DspaceSshServiceImpl.java

@Override
public void loadJobInfoByJob(RedisJob job) {
    int jobType = job.getType();
    long jobId = job.getJobId();
    handler.setJobType(job.getType());/*from   w  w w  . ja v a 2 s .com*/
    String jobFilePath = DataHandlersUtil.getJobReportPath(DataUtil.JOB_TYPES[jobType], jobId);
    handler.setReportFilePath(jobFilePath + File.separator + String.valueOf(jobId) + "-report.txt");
    handler.setServerId(String.valueOf(job.getServerId()));
    String schema = (String) DataUtil.JOB_TYPE_DATA_SCHEMA.get(DataUtil.JOB_TYPES[job.getType()]);
    Map data = RedisUtil.getJobDao().getJobInfoByAttributes(jobId, schema.split(","));
    for (Field f : DspaceSshHandler.class.getDeclaredFields()) {
        try {
            String fieldName = f.getName();
            if (data.containsKey(fieldName)) {
                f.set(this, (String) data.get(fieldName));
            }
        } catch (SecurityException ex) {
        } catch (IllegalArgumentException | IllegalAccessException ex) {
        }
    }
}

From source file:net.mindengine.blogix.db.readers.ObjectReader.java

private void setFieldValue(T objectInstance, Field field, String fieldValue) {
    Object convertedFieldValue = convertFieldValue(fieldValue, field);
    field.setAccessible(true);/*from   w w w . j  a va2 s. c  o m*/

    try {
        field.set(objectInstance, convertedFieldValue);
    } catch (Exception e) {
        throw new RuntimeException("Cannot set value to field " + field.toString());
    }
}

From source file:de.lgblaumeiser.ptm.store.filesystem.FileStore.java

private Long getIndexObject(final T object) {
    checkState(object != null);/*from   w ww .  j  a  v  a2s  .  c o m*/
    try {
        Field f = object.getClass().getDeclaredField(ID);
        f.setAccessible(true);
        Long returnVal = (Long) f.get(object);
        if (returnVal == -1) {
            f.set(object, getNextId());
        }
        returnVal = (Long) f.get(object);
        f.setAccessible(false);
        return returnVal;
    } catch (IllegalAccessException | IllegalArgumentException | ClassCastException | NoSuchFieldException
            | SecurityException e) {
        throw new IllegalStateException(e);
    }

}

From source file:org.openmrs.module.radiology.order.RadiologyOrderServiceImplComponentTest.java

@Before
public void setUp() throws Exception {

    if (radiologyOrderServiceImpl == null) {
        radiologyOrderServiceImpl = new RadiologyOrderServiceImpl();
        Field radiologyStudyServiceField = RadiologyOrderServiceImpl.class
                .getDeclaredField("radiologyStudyService");
        radiologyStudyServiceField.setAccessible(true);
        radiologyStudyServiceField.set(radiologyOrderServiceImpl, radiologyStudyService);
        Field orderServiceField = RadiologyOrderServiceImpl.class.getDeclaredField("orderService");
        orderServiceField.setAccessible(true);
        orderServiceField.set(radiologyOrderServiceImpl, orderService);
        Field encounterServiceField = RadiologyOrderServiceImpl.class.getDeclaredField("encounterService");
        encounterServiceField.setAccessible(true);
        encounterServiceField.set(radiologyOrderServiceImpl, encounterService);
        Field radiologyPropertiesField = RadiologyOrderServiceImpl.class
                .getDeclaredField("radiologyProperties");
        radiologyPropertiesField.setAccessible(true);
        radiologyPropertiesField.set(radiologyOrderServiceImpl, radiologyProperties);
    }/*from w  w w  . ja  v  a2  s.co  m*/

    saveRadiologyOrderEncounterMethod = RadiologyOrderServiceImpl.class.getDeclaredMethod(
            "saveRadiologyOrderEncounter", new Class[] { Patient.class, Provider.class, Date.class });
    saveRadiologyOrderEncounterMethod.setAccessible(true);

    executeDataSet(TEST_DATASET);
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testDirectory() throws NoSuchFieldException, IllegalAccessException, IOException {
    File ctlFile = Mockito.mock(File.class);
    Mockito.when(ctlFile.getName()).thenReturn("Test");
    Mockito.when(ctlFile.isFile()).thenReturn(false);

    Logger log = Mockito.mock(Logger.class);
    Field logField = validationController.getClass().getDeclaredField("logger");
    logField.setAccessible(true);//w w  w  .  ja  v a2  s  .co m
    logField.set(null, log);

    validationController.doValidation(ctlFile);
    Mockito.verify(log, Mockito.atLeastOnce()).error("Invalid input: No clt/zip file found");
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testDoValidationInvalidFile() throws NoSuchFieldException, IllegalAccessException, IOException {
    Logger log = Mockito.mock(Logger.class);
    Field logField = validationController.getClass().getDeclaredField("logger");
    logField.setAccessible(true);//from   www  .  j  av a 2  s. c  om
    logField.set(null, log);

    File invalidFile = Mockito.mock(File.class);
    Mockito.when(invalidFile.getName()).thenReturn("Test.txt");
    Mockito.when(invalidFile.isFile()).thenReturn(true);

    validationController.doValidation(invalidFile);
    Mockito.verify(log, Mockito.atLeastOnce()).error("Invalid input: No clt/zip file found");
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testInvalidProcessControlFile() throws IOException, SecurityException, NoSuchFieldException,
        IllegalArgumentException, IllegalAccessException {
    Resource ctlFileResource = new ClassPathResource(invalidControlFile);
    File ctlFile = ctlFileResource.getFile();

    Logger log = Mockito.mock(Logger.class);
    Field logField = validationController.getClass().getDeclaredField("logger");
    logField.setAccessible(true);/* w ww .j a  va2 s  . c o  m*/
    logField.set(null, log);

    validationController.processControlFile(ctlFile);

    Mockito.verify(log).error(Mockito.anyString());
}

From source file:com.glaf.core.util.ReflectUtils.java

public static void setFieldValue(Object target, String name, Class<?> type, Object value) {
    if (target == null || StringUtils.isEmpty(name)
            || (value != null && !type.isAssignableFrom(value.getClass()))) {
        return;//w  w w. ja v  a  2 s.c  o m
    }
    Class<?> clazz = target.getClass();
    try {
        Method method = clazz
                .getDeclaredMethod("set" + Character.toUpperCase(name.charAt(0)) + name.substring(1), type);
        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }
        method.invoke(target, value);
    } catch (Exception ex) {
        if (LogUtils.isDebug()) {
            logger.debug(ex);
        }
        try {
            Field field = clazz.getDeclaredField(name);
            if (!Modifier.isPublic(field.getModifiers())) {
                field.setAccessible(true);
            }
            field.set(target, value);
        } catch (Exception e) {
            if (LogUtils.isDebug()) {
                logger.debug(e);
            }
        }
    }
}