public String toGetSourceString()

in src/main/java/org/apache/commons/ognl/ASTMethod.java [117:258]


    public String toGetSourceString( OgnlContext context, Object target )
    {
        /*
         * System.out.println("methodName is " + methodName + " for target " + target + " target class: " + (target !=
         * null ? target.getClass() : null) + " current type: " + context.getCurrentType());
         */
        if ( target == null )
        {
            throw new UnsupportedCompilationException( "Target object is null." );
        }

        String post = "";
        StringBuilder sourceStringBuilder;
        Method method;

        OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context );
        try
        {

            method = OgnlRuntime.getMethod( context, context.getCurrentType() != null
                ? context.getCurrentType()
                : target.getClass(), methodName, children, false );
            if ( method == null )
            {
                method = OgnlRuntime.getReadMethod( target.getClass(), methodName,
                                                    children != null ? children.length : -1 );
            }

            if ( method == null )
            {
                method = OgnlRuntime.getWriteMethod( target.getClass(), methodName,
                                                     children != null ? children.length : -1 );

                if ( method != null )
                {

                    context.setCurrentType( method.getReturnType() );
                    context.setCurrentAccessor(
                        compiler.getSuperOrInterfaceClass( method, method.getDeclaringClass() ) );

                    coreExpression = toSetSourceString( context, target );
                    if ( coreExpression == null || coreExpression.length() < 1 )
                    {
                        throw new UnsupportedCompilationException( "can't find suitable getter method" );
                    }

                    coreExpression += ";";
                    lastExpression = "null";

                    return coreExpression;
                }

                return "";
            }
            getterClass = method.getReturnType();

            // TODO: This is a hacky workaround until javassist supports varargs method invocations
            boolean varArgs = method.isVarArgs();

            if ( varArgs )
            {
                throw new UnsupportedCompilationException(
                    "Javassist does not currently support varargs method calls" );
            }

            sourceStringBuilder = new StringBuilder().append( "." ).append( method.getName() ).append( "(" );

            if ( ( children != null ) && ( children.length > 0 ) )
            {
                Class[] parms = method.getParameterTypes();
                String prevCast = (String) context.remove( ExpressionCompiler.PRE_CAST );
                /*
                 * System.out.println("before children methodName is " + methodName + " for target " + target +
                 * " target class: " + (target != null ? target.getClass() : null) + " current type: " +
                 * context.getCurrentType() + " and previous type: " + context.getPreviousType());
                 */

                for ( int i = 0; i < children.length; i++ )
                {
                    if ( i > 0 )
                    {
                        sourceStringBuilder.append( ", " );
                    }

                    Class prevType = context.getCurrentType();

                    Object root = context.getRoot();
                    context.setCurrentObject( root );
                    context.setCurrentType( root != null ? root.getClass() : null );
                    context.setCurrentAccessor( null );
                    context.setPreviousType( null );

                    Node child = children[i];

                    String parmString = ASTMethodUtil.getParmString( context, root, child, prevType );

                    Class valueClass = ASTMethodUtil.getValueClass( context, root, child );

                    if ( ( !varArgs || varArgs && ( i + 1 ) < parms.length ) && valueClass != parms[i] )
                    {
                        parmString = ASTMethodUtil.getParmString( context, parms[i], parmString, child, valueClass,
                                                                  ".class, true)" );
                    }

                    sourceStringBuilder.append( parmString );
                }

                if ( prevCast != null )
                {
                    context.put( ExpressionCompiler.PRE_CAST, prevCast );
                }
            }

        }
        catch ( Throwable t )
        {
            throw OgnlOps.castToRuntime( t );
        }

        try
        {
            Object contextObj = getValueBody( context, target );
            context.setCurrentObject( contextObj );
        }
        catch ( Throwable t )
        {
            throw OgnlOps.castToRuntime( t );
        }

        sourceStringBuilder.append( ")" ).append( post );

        if ( method.getReturnType() == void.class )
        {
            coreExpression = sourceStringBuilder.toString() + ";";
            lastExpression = "null";
        }

        context.setCurrentType( method.getReturnType() );
        context.setCurrentAccessor( compiler.getSuperOrInterfaceClass( method, method.getDeclaringClass() ) );

        return sourceStringBuilder.toString();
    }