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:com.screenslicer.common.CommonUtil.java

public static void setMethod(HttpURLConnection httpURLConnection, String method) {
    try {//  w  w  w  .  jav  a  2  s  .  c  o  m
        httpURLConnection.setRequestMethod(method);
        // Check whether we are running on a buggy JRE
    } catch (final ProtocolException pe) {
        Class<?> connectionClass = httpURLConnection.getClass();
        Field delegateField = null;
        try {
            delegateField = connectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            HttpURLConnection delegateConnection = (HttpURLConnection) delegateField.get(httpURLConnection);
            setMethod(delegateConnection, method);
        } catch (NoSuchFieldException e) {
            // Ignore for now, keep going
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        try {
            Field methodField;
            while (connectionClass != null) {
                try {
                    methodField = connectionClass.getDeclaredField("method");
                } catch (NoSuchFieldException e) {
                    connectionClass = connectionClass.getSuperclass();
                    continue;
                }
                methodField.setAccessible(true);
                methodField.set(httpURLConnection, method);
                break;
            }
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:bdi4jade.util.ReflectionUtils.java

/**
 * Sets plan body fields annotated with {@link bdi4jade.annotation.Belief}.
 * /*from  www .  j  av a 2  s  .c om*/
 * @param planBody
 *            the plan body to be setup with beliefs.
 */
public static void setupBeliefs(PlanBody planBody) {
    Capability capability = planBody.getPlan().getPlanLibrary().getCapability();
    Class<?> currentClass = planBody.getClass();
    while (PlanBody.class.isAssignableFrom(currentClass)) {
        for (Field field : currentClass.getDeclaredFields()) {
            boolean b = field.isAccessible();
            field.setAccessible(true);
            try {
                if (field.isAnnotationPresent(bdi4jade.annotation.Belief.class)) {
                    if (Belief.class.isAssignableFrom(field.getType())) {
                        bdi4jade.annotation.Belief beliefAnnotation = field
                                .getAnnotation(bdi4jade.annotation.Belief.class);
                        String beliefName = beliefAnnotation.name();
                        if (beliefName == null || "".equals(beliefName)) {
                            beliefName = field.getName();
                        }
                        Belief<?, ?> belief = capability.getBeliefBase().getBelief(beliefName);
                        field.set(planBody, belief);
                    } else {
                        throw new ClassCastException("Field " + field.getName() + " should be a Belief");
                    }
                }
            } catch (Exception exc) {
                log.warn(exc);
            }
            field.setAccessible(b);
        }
        currentClass = currentClass.getSuperclass();
    }
}

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

/**
 * Apply a BigDecimal value to the object.
 * //from w  ww .  j a va 2s  .c  o m
 * @param o
 *            the object
 * @param field
 *            the field
 * @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 bigDecimalReader(final Object o, final Field field, final String[] values, final int idx)
        throws ConverterException {
    String dBdValue = values[idx];
    if (dBdValue != null) {
        try {
            field.set(o, BigDecimal.valueOf(Double.valueOf(dBdValue)));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_BIGDECIMAL.getMessage(), e);
        }
    }
}

From source file:se.altrusoft.docserv.models.TemplateModel.java

public void setField(String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
    Field field = getDeclaredField(fieldName);
    field.set(this, value);
}

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

/**
 * Read a string value from the Cell.//from   w  w w  .java 2 s .c  o m
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void stringReader(final Object object, final Field field, final Cell cell)
        throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            field.set(object, readCell(cell));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_STRING.getMessage(), e);
        }
    }
}

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

/**
 * Read a short value from the Cell./*from  w  w  w. j  a v  a 2 s  .c  o m*/
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void shortReader(final Object object, final Field field, final Cell cell)
        throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            field.set(object, Double.valueOf(readCell(cell)).shortValue());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_SHORT.getMessage(), e);
        }
    }
}

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

/**
 * Read a integer value from the Cell.//from  www.  j  a va 2  s.c  om
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void integerReader(final Object object, final Field field, final Cell cell)
        throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            field.set(object, Double.valueOf(readCell(cell)).intValue());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_INTEGER.getMessage(), e);
        }
    }
}

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

/**
 * Read a long value from the Cell./*from w w w. ja  va2  s  .com*/
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void longReader(final Object object, final Field field, final Cell cell)
        throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            field.set(object, Double.valueOf(readCell(cell)).longValue());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_LONG.getMessage(), e);
        }
    }
}

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

/**
 * Read a float value from the Cell.//from w ww  .  j a  va2  s  .  co m
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void floatReader(final Object object, final Field field, final Cell cell)
        throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            field.set(object, Double.valueOf(readCell(cell)).floatValue());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_FLOAT.getMessage(), e);
        }
    }
}

From source file:mailjimp.webhook.TestWebHookSecurityInterceptor.java

@Test
public void testPreHandle() throws Exception {
    MyTestWebHookAdapter adapter = new MyTestWebHookAdapter();
    WebHookSecurityInterceptor interceptor = new WebHookSecurityInterceptor();
    // set the adapter like spring would do.
    Field field = WebHookSecurityInterceptor.class.getDeclaredField("webHookAdapter");
    field.setAccessible(true);/* ww w  . j  a  v a 2  s  .  c  o  m*/
    field.set(interceptor, adapter);
    field.setAccessible(false);
    // call the interceptor.
    interceptor.preHandle(new MockHttpServletRequest(), new MockHttpServletResponse(), null);
    // all we have to check is if the adapter was called.
    assertTrue(adapter.wasCalled("isValidRequest"));
}