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:edu.syr.pcpratts.rootbeer.runtime.Serializer.java

public void writeField(Object base, String name, Object value) {
    Class cls = base.getClass();// w  w  w.  j a  v  a2 s  .  c o m
    while (true) {
        try {
            Field f = cls.getDeclaredField(name);
            f.setAccessible(true);
            f.set(base, value);
            return;
        } catch (Exception ex) {
            cls = cls.getSuperclass();
        }
    }
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

/**
 * Read a double value from the Cell.//from w  w w  . ja  v a  2  s  . com
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void doubleReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                field.set(object, Double.valueOf(readCell(cell).replace(Constants.COMMA, Constants.DOT)));
            } else {
                field.set(object, Double.valueOf(readCell(cell)));
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_DOUBLE.getMessage(), e);
        }
    }
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

/**
 * Read a big decimal value from the Cell.
 * //from  ww  w . j a v  a2  s  . co  m
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void bigDecimalReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                field.set(object, BigDecimal
                        .valueOf(Double.valueOf(readCell(cell).replace(Constants.COMMA, Constants.DOT))));
            } else {
                field.set(object, BigDecimal.valueOf(Double.valueOf(readCell(cell))));
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_BIGDECIMAL.getMessage(), e);
        }
    }
}

From source file:org.o3project.ocnrm.rest.RMNodeRestApiTest.java

/**
 * Test method for//from   w w w . j  a v  a2s .  co  m
 * {@link org.o3project.ocnrm.rest.RMNodeRestApi#createNodeInfo()}
 */
@Test
public void testCreateNodeInfoWithLowerInfo() throws Exception {
    String lowertestData = "{\"lower\":" + "{\"flow\":["
            + "{\"dstTTP\":\"NW=SDN,NE=FW950073202,Layer=OCh,TTP=2\"," + "\"flowId\":\"NW=SDN,Layer=OCh,TL=1\","
            + "\"linkId\":[\"NW=SDN,Layer=OMS,TL=2\",\"NW=SDN,Layer=OMS,TL=3\"],"
            + "\"srcTTP\":\"NW=SDN,NE=FW950073201,Layer=OCh,TTP=1\"},"
            + "{\"dstTTP\":\"NW=SDN,NE=FW950073202,Layer=OCh,TTP=1\"," + "\"flowId\":\"NW=SDN,Layer=OCh,TL=2\","
            + "\"linkId\":[\"NW=SDN,Layer=OMS,TL=1\"],"
            + "\"srcTTP\":\"NW=SDN,NE=FW950073201,Layer=OCh,TTP=2\"}" + "]," + "\"link\":["
            + "{\"dstTTP\":\"NW=SDN,NE=FW950073202,Layer=OMS,TTP=2\"," + "\"linkId\":\"NW=SDN,Layer=OMS,TL=1\","
            + "\"srcTTP\":\"NW=SDN,NE=FW950073201,Layer=OMS,TTP=1\"},"
            + "{\"dstTTP\":\"NW=SDN,NE=FW950073203,Layer=OMS,TTP=2\"," + "\"linkId\":\"NW=SDN,Layer=OMS,TL=2\","
            + "\"srcTTP\":\"NW=SDN,NE=FW950073201,Layer=OMS,TTP=2\"},"
            + "{\"dstTTP\":\"NW=SDN,NE=FW950073203,Layer=OMS,TTP=1\"," + "\"linkId\":\"NW=SDN,Layer=OMS,TL=3\","
            + "\"srcTTP\":\"NW=SDN,NE=FW950073202,Layer=OMS,TTP=1\"}" + "]," + "\"node\":["
            + "{\"nodeId\":\"NW=SDN,NE=FW950073201\"}," + "{\"nodeId\":\"NW=SDN,NE=FW950073202\"},"
            + "{\"nodeId\":\"NW=SDN,NE=FW950073203\"}" + "]," + "\"port\":["
            + "{\"portId\":\"NW=SDN,NE=FW950073201,Layer=OCh,TTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073201,Layer=OCh,TTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073202,Layer=OCh,TTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073202,Layer=OCh,TTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073203,Layer=OCh,TTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073203,Layer=OCh,TTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073201,Layer=OCh,CTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073201,Layer=OCh,CTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073202,Layer=OCh,CTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073202,Layer=OCh,CTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073203,Layer=OCh,CTP=1\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073203,Layer=OCh,CTP=2\"},"
            + "{\"portId\":\"NW=SDN,NE=FW950073203,Layer=OCh,CTP=3\"}" + "]" + "}}";
    Logger dummyLogger = mock(Logger.class);

    Field field = target.getClass().getSuperclass().getDeclaredField("logger");
    field.setAccessible(true);
    field.set(target, dummyLogger);

    Representation response = target.createNodeInfo(lowertestData);
    JSONObject result = new JSONObject(response.getText());

    verify(dummyLogger, times(1)).debug("make lower topology.");
    assertThat(result.getString("ResultLevel"), is("0"));
}

From source file:org.envirocar.server.rest.guice.JSONSchemaFactoryProvider.java

protected void setDefaultLibrary(ValidationConfigurationBuilder vcb, Library lib) {
    try {//from w w  w .  java  2 s  .  co m
        Field defaultLibraryField = ValidationConfigurationBuilder.class.getDeclaredField("defaultLibrary");
        defaultLibraryField.setAccessible(true);
        defaultLibraryField.set(vcb, lib);
        defaultLibraryField.setAccessible(false);
    } catch (NoSuchFieldException ex) {
        throw new RuntimeException(ex);
    } catch (SecurityException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.seleniumtests.ut.connectors.selenium.TestSeleniumGridConnector.java

@Test(groups = { "ut" })
public void testRunTest() throws UnsupportedOperationException, IOException, NoSuchFieldException,
        SecurityException, IllegalArgumentException, IllegalAccessException, UnirestException {

    createServerMock("GET", "/grid/api/testsession/", 200, "{'proxyId': 'http://localhost:43210'}");

    // prepare app file
    ((DesiredCapabilities) capabilities).setCapability(CapabilityType.BROWSER_NAME, "firefox");
    ((DesiredCapabilities) capabilities).setCapability(CapabilityType.VERSION, "50.0");

    // prepare response
    InputStream is = new StringInputStream("{'proxyId':'proxy//node:0'}");
    when(entity.getContent()).thenReturn(is);

    SeleniumGridConnector connector = new SeleniumGridConnector(SERVER_URL);

    Logger logger = spy(SeleniumRobotLogger.getLogger(SeleniumGridConnector.class));
    Field loggerField = SeleniumGridConnector.class.getDeclaredField("logger");
    loggerField.setAccessible(true);/*from w  ww .  j  av  a 2  s .  c  o m*/
    loggerField.set(connector, logger);

    connector.runTest(driver);

    verify(logger).info("WebDriver is running on node localhost, firefox 50.0, session 0");
    Assert.assertEquals(connector.getNodeUrl(), "http://localhost:43210");
}

From source file:net.ceos.project.poi.annotated.core.CsvHandler.java

/**
 * Apply a Double value to the object.//from www .j  a va 2 s . c  om
 * 
 * @param o
 *            the object
 * @param field
 *            the field
 * @param xlsAnnotation
 *            the {@link XlsElement} annotation
 * @param values
 *            the array with the content at one line
 * @param idx
 *            the index of the field
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void doubleReader(final Object o, final Field field, final XlsElement xlsAnnotation,
        final String[] values, final int idx) throws ConverterException {
    String dPValue = values[idx];
    if (StringUtils.isNotBlank(dPValue)) {
        try {
            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                field.set(o, Double.parseDouble(dPValue.replace(Constants.COMMA, Constants.DOT)));
            } else if (StringUtils.isNotBlank(xlsAnnotation.formatMask())) {
                field.set(o, Double.parseDouble(dPValue.replace(Constants.COMMA, Constants.DOT)));
            } else {
                field.set(o, Double.parseDouble(dPValue));
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_DOUBLE.getMessage(), e);
        }
    }
}

From source file:com.example.bbbbbb.http.sample.util.SecureSocketFactory.java

/**
 * Pre-ICS Android had a bug resolving HTTPS addresses. This workaround fixes that bug.
 *
 * @param socket The socket to alter/*from  w ww. j  a v  a 2 s.  com*/
 * @param host   Hostname to connect to
 * @see <a href="https://code.google.com/p/android/issues/detail?id=13117#c14">https://code.google.com/p/android/issues/detail?id=13117#c14</a>
 */
private void injectHostname(Socket socket, String host) {
    try {
        if (Integer.valueOf(Build.VERSION.SDK) >= 4) {
            Field field = InetAddress.class.getDeclaredField("hostName");
            field.setAccessible(true);
            field.set(socket.getInetAddress(), host);
        }
    } catch (Exception ignored) {
    }
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClientOAuthTest.java

private void mockResponse(byte[] body, int statusCode, String reason) throws IOException {
    final InputStream bodyInputStream = new ByteArrayInputStream(body);
    final StatusLine statusLine = new StatusLine("HTTP/1.1 " + statusCode + " " + reason + "\n");

    when(httpClient.executeMethod(argThat(new ArgumentMatcher<HttpMethod>() {
        @Override//from w w w . j ava  2 s .  c o m
        public boolean matches(Object o) {
            if (o instanceof HttpMethodBase) {
                HttpMethodBase methodBase = (HttpMethodBase) o;

                try {
                    Field responseStreamField = HttpMethodBase.class.getDeclaredField("responseStream");
                    responseStreamField.setAccessible(true);
                    responseStreamField.set(methodBase, bodyInputStream);
                    responseStreamField.setAccessible(false);

                    Field statusLineField = HttpMethodBase.class.getDeclaredField("statusLine");
                    statusLineField.setAccessible(true);
                    statusLineField.set(methodBase, statusLine);
                    statusLineField.setAccessible(false);

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                return true;
            }
            return false;
        }
    }))).thenReturn(200);
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

/**
 * Read a date value from the Cell./*from w w  w .  j  ava2 s.  c  o m*/
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void dateReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isBlank(xlsAnnotation.transformMask())) {
                field.set(object, cell.getDateCellValue());
            } else {
                String date = cell.getStringCellValue();

                String tM = xlsAnnotation.transformMask();
                String fM = xlsAnnotation.formatMask();
                String decorator = StringUtils.isEmpty(tM)
                        ? (StringUtils.isEmpty(fM) ? CellStyleHandler.MASK_DECORATOR_DATE : fM)
                        : tM;

                SimpleDateFormat dt = new SimpleDateFormat(decorator);

                Date dateConverted = dt.parse(date);
                field.set(object, dateConverted);
            }
        } catch (ParseException | IllegalArgumentException | IllegalAccessException e) {
            /*
             * if date decorator do not match with a valid mask launch
             * exception
             */
            throw new ConverterException(ExceptionMessage.CONVERTER_DATE.getMessage(), e);
        }
    }
}