public static void setField()

in src/main/java/org/apache/bsf/util/ReflectionUtils.java [363:396]


    public static void setField(final Object target, final String fieldName, final Bean value, final TypeConvertorRegistry tcr)
            throws IllegalArgumentException, IllegalAccessException {
        // This is to handle how we do static fields.
        final Class targetClass = (target instanceof Class) ? (Class) target : target.getClass();

        try {
            final Field f = targetClass.getField(fieldName);
            final Class fieldType = f.getType();

            // type convert the value if necessary
            Object fieldVal = null;
            boolean okeydokey = true;
            if (fieldType.isAssignableFrom(value.type)) {
                fieldVal = value.value;
            } else if (tcr != null) {
                final TypeConvertor cvtor = tcr.lookup(value.type, fieldType);
                if (cvtor != null) {
                    fieldVal = cvtor.convert(value.type, fieldType, value.value);
                } else {
                    okeydokey = false;
                }
            } else {
                okeydokey = false;
            }
            if (!okeydokey) {
                throw new IllegalArgumentException("unable to assign '" + value.value + "' to field '" + fieldName + "'");
            }

            // now set the value
            f.set(target, fieldVal);
        } catch (final NoSuchFieldException e) {
            throw new IllegalArgumentException("field '" + fieldName + "' is " + "unknown for '" + target + "'");
        }
    }