protected Object getValueBody()

in src/main/java/org/apache/commons/ognl/ASTCtor.java [76:139]


    protected Object getValueBody( OgnlContext context, Object source )
        throws OgnlException
    {
        Object result, root = context.getRoot();
        int count = jjtGetNumChildren();
        Object[] args = new Object[count];

        for ( int i = 0; i < count; ++i )
        {
            args[i] = children[i].getValue( context, root );
        }
        if ( isArray )
        {
            if ( args.length != 1 ) {
                throw new OgnlException( "only expect array size or fixed initializer list" );
            }
            try
            {
                Class componentClass = OgnlRuntime.classForName( context, className );
                List sourceList = null;
                int size;

                if ( args[0] instanceof List )
                {
                    sourceList = (List) args[0];
                    size = sourceList.size();
                }
                else
                {
                    size = (int) OgnlOps.longValue( args[0] );
                }
                result = Array.newInstance( componentClass, size );
                if ( sourceList != null )
                {
                    TypeConverter converter = context.getTypeConverter();

                    for ( int i = 0, icount = sourceList.size(); i < icount; i++ )
                    {
                        Object o = sourceList.get( i );

                        if ( ( o == null ) || componentClass.isInstance( o ) )
                        {
                            Array.set( result, i, o );
                        }
                        else
                        {
                            Array.set( result, i,
                                       converter.convertValue( context, null, null, null, o, componentClass ) );
                        }
                    }
                }
            }
            catch ( ClassNotFoundException ex )
            {
                throw new OgnlException( "array component class '" + className + "' not found", ex );
            }
        }
        else
        {
            result = OgnlRuntime.callConstructor( context, className, args );
        }

        return result;
    }