public static Method getMethod()

in src/main/java/org/apache/commons/ognl/OgnlRuntime.java [1682:1768]


    public static Method getMethod( OgnlContext context, Class<?> target, String name, Node[] children,
                                    boolean includeStatic )
        throws Exception
    {
        Class<?>[] parms;
        if ( children != null && children.length > 0 )
        {
            parms = new Class[children.length];

            // used to reset context after loop
            Class<?> currType = context.getCurrentType();
            Class<?> currAccessor = context.getCurrentAccessor();
            Object cast = context.get( ExpressionCompiler.PRE_CAST );

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

            for ( int i = 0; i < children.length; i++ )
            {
                children[i].toGetSourceString( context, context.getRoot() );
                parms[i] = context.getCurrentType();
            }

            context.put( ExpressionCompiler.PRE_CAST, cast );

            context.setCurrentType( currType );
            context.setCurrentAccessor( currAccessor );
            context.setCurrentObject( target );
        }
        else
        {
            parms = new Class[0];
        }

        List<Method> methods = OgnlRuntime.getMethods( target, name, includeStatic );
        if ( methods == null )
        {
            return null;
        }

        for ( Method method : methods )
        {
            boolean varArgs = method.isVarArgs();

            if ( parms.length != method.getParameterTypes().length && !varArgs )
            {
                continue;
            }

            Class<?>[] methodParameterTypes = method.getParameterTypes();
            boolean matched = true;
            for ( int i = 0; i < methodParameterTypes.length; i++ )
            {
                Class<?> methodParameterType = methodParameterTypes[i];
                if ( varArgs && methodParameterType.isArray() )
                {
                    continue;
                }

                Class<?> parm = parms[i];
                if ( parm == null )
                {
                    matched = false;
                    break;
                }

                if ( parm == methodParameterType || methodParameterType.isPrimitive() && Character.TYPE != methodParameterType && Byte.TYPE != methodParameterType
                    && Number.class.isAssignableFrom(parm)
                    && OgnlRuntime.getPrimitiveWrapperClass(parm) == methodParameterType)
                {
                    continue;
                }

                matched = false;
                break;
            }

            if ( matched )
            {
                return method;
            }
        }

        return null;
    }