public static Object callConstructor()

in src/main/java/org/apache/commons/ognl/OgnlRuntime.java [976:1023]


    public static Object callConstructor( OgnlContext context, String className, Object[] args )
        throws OgnlException
    {
        Throwable cause = null;
        Object[] actualArgs = args;

        try
        {
            Constructor<?> ctor = null;
            Class<?>[] ctorParameterTypes = null;
            Class<?> target = classForName( context, className );
            List<Constructor<?>> constructors = getConstructors( target );

            for ( Constructor<?> constructor : constructors )
            {
                Class<?>[] cParameterTypes = getParameterTypes( constructor );

                if ( areArgsCompatible( args, cParameterTypes ) && ( ctor == null || isMoreSpecific( cParameterTypes,
                                                                                                     ctorParameterTypes ) ) )
                {
                    ctor = constructor;
                    ctorParameterTypes = cParameterTypes;
                }
            }
            if ( ctor == null )
            {
                actualArgs = new Object[args.length];
                if ( ( ctor = getConvertedConstructorAndArgs( context, target, constructors, args, actualArgs ) ) == null )
                {
                    throw new NoSuchMethodException();
                }
            }
            if ( !context.getMemberAccess().isAccessible( context, target, ctor, null ) )
            {
                throw new IllegalAccessException( "access denied to " + target.getName() + "()" );
            }
            return ctor.newInstance( actualArgs );
        }
        catch ( ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException e )
        {
            cause = e;
        } catch ( InvocationTargetException e )
        {
            cause = e.getTargetException();
        }

        throw new MethodFailedException( className, "new", cause );
    }