public static void setProperty()

in src/main/java/org/apache/bsf/util/ReflectionUtils.java [414:468]


    public static void setProperty(final Object target, final String propName, final Integer index, final Object value, final Class valueType,
            final TypeConvertorRegistry tcr) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        // find the property descriptor
        final BeanInfo bi = Introspector.getBeanInfo(target.getClass());
        final PropertyDescriptor pd = (PropertyDescriptor) findFeatureByName("property", propName, bi.getPropertyDescriptors());
        if (pd == null) {
            throw new IllegalArgumentException("property '" + propName + "' is " + "unknown for '" + target + "'");
        }

        // get write method and type of property
        Method wm;
        Class propType;
        if (index != null) {
            // if index != null, then property is indexed - pd better be so too
            if (!(pd instanceof IndexedPropertyDescriptor)) {
                throw new IllegalArgumentException("attempt to set non-indexed " + "property '" + propName + "' as being indexed");
            }
            final IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
            wm = ipd.getIndexedWriteMethod();
            propType = ipd.getIndexedPropertyType();
        } else {
            wm = pd.getWriteMethod();
            propType = pd.getPropertyType();
        }

        if (wm == null) {
            throw new IllegalArgumentException("property '" + propName + "' is not writeable");
        }

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

        // now set the value
        if (index != null) {
            wm.invoke(target, new Object[] { index, propVal });
        } else {
            wm.invoke(target, new Object[] { propVal });
        }
    }