public Object getValue()

in struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/XWorkArrayELResolver.java [38:91]


    public Object getValue(ELContext elContext, Object target, Object property) {
        if (target != null && property != null && target.getClass().isArray()) {

            Map<String, Object> valueStackContext = getValueStackContext(elContext);

            Class lastClass = (Class) valueStackContext.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
            String lastProperty = (String) valueStackContext.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED);

            Integer index = null;

            if (property instanceof Number)
                index = ((Number) property).intValue();
            else {
                try {
                    index = Integer.valueOf(property.toString());
                } catch (NumberFormatException e) {
                    //ignore
                }
            }

            if (index != null) {
                if (ReflectionContextState.isCreatingNullObjects(valueStackContext) && objectTypeDeterminer.shouldCreateIfNew(lastClass, lastProperty, target, null, true)) {
                    Class clazz = target.getClass().getComponentType();

                    if (index < 0 || index >= Array.getLength(target)) {
                        //nothing to do here, as we cannot set a new array
                        throw new ELException("Index [" + index + "] is out of bounds");
                    } else {
                        //valid index
                        Object obj = Array.get(target, index);
                        if (obj == null) {
                            try {
                                obj = objectFactory.buildBean(clazz, valueStackContext);
                                Array.set(target, index, obj);
                            } catch (Exception e) {
                                throw new ELException("unable to instantiate a new object for property [" + lastProperty + "]", e);
                            }
                        }

                        elContext.setPropertyResolved(true);
                        return obj;
                    }
                } else {
                    //try normal list
                    if (index < Array.getLength(target)) {
                        elContext.setPropertyResolved(true);
                        return Array.get(target, index);
                    }
                }
            }
        }

        return null;
    }