⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ognlruntime.java

📁 此源代码是一个开源项目,可以实现不同类型之间的转换
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        if (Modifier.isStatic(modifiers))
            result = "static " + result;
        if (Modifier.isFinal(modifiers))
            result = "final " + result;
        if (Modifier.isNative(modifiers))
            result = "native " + result;
        if (Modifier.isSynchronized(modifiers))
            result = "synchronized " + result;
        if (Modifier.isTransient(modifiers))
            result = "transient " + result;
        return result;
    }

    public static final Class classForName( OgnlContext context, String className ) throws ClassNotFoundException
    {
        Class           result = (Class)primitiveTypes.get(className);

        if (result == null) {
            ClassResolver   resolver;

            if ((context == null) || ((resolver = context.getClassResolver()) == null)) {
                resolver = OgnlContext.DEFAULT_CLASS_RESOLVER;
            }
            result = resolver.classForName(className, context);
        }
        return result;
    }

    public static final boolean isInstance( OgnlContext context, Object value, String className ) throws OgnlException
    {
        try
          {
            Class c = classForName( context, className);
            return c.isInstance( value );
          }
        catch (ClassNotFoundException e)
          {
            throw new OgnlException( "No such class: " + className, e );
          }
    }

    public static Object getPrimitiveDefaultValue( Class forClass )
    {
        return primitiveDefaults.get(forClass);
    }

    public static Object getConvertedType( OgnlContext context, Object target, Member member, String propertyName, Object value, Class type)
    {
        return context.getTypeConverter().convertValue(context, target, member, propertyName, value, type);
    }

    public static boolean getConvertedTypes( OgnlContext context, Object target, Member member, String propertyName, Class[] parameterTypes, Object[] args, Object[] newArgs)
    {
        boolean         result = false;

        if (parameterTypes.length == args.length) {
            result = true;
            for (int i = 0, ilast = parameterTypes.length - 1; result && (i <= ilast); i++) {
                Object      arg = args[i];
                Class       type = parameterTypes[i];

                if (isTypeCompatible(arg, type)) {
                    newArgs[i] = arg;
                } else {
                    Object      v = getConvertedType(context, target, member, propertyName, arg, type);

                    if (v == OgnlRuntime.NoConversionPossible) {
                        result = false;
                    } else {
                        newArgs[i] = v;
                    }
                }
            }
        }
        return result;
    }

    public static Method getConvertedMethodAndArgs( OgnlContext context, Object target, String propertyName, List methods, Object[] args, Object[] newArgs)
    {
        Method          result = null;
        TypeConverter   converter = context.getTypeConverter();

        if ((converter != null) && (methods != null)) {
            for (int i = 0, icount = methods.size(); (result == null) && (i < icount); i++) {
                Method      m = (Method)methods.get(i);
                Class[]     parameterTypes = getParameterTypes(m);

                if (getConvertedTypes( context, target, m, propertyName, parameterTypes, args, newArgs )) {
                    result = m;
                }
            }
        }
        return result;
    }

    public static Constructor getConvertedConstructorAndArgs( OgnlContext context, Object target, List constructors, Object[] args, Object[] newArgs )
    {
        Constructor     result = null;
        TypeConverter   converter = context.getTypeConverter();

        if ((converter != null) && (constructors != null)) {
            for (int i = 0, icount = constructors.size(); (result == null) && (i < icount); i++) {
                Constructor     ctor = (Constructor)constructors.get(i);
                Class[]         parameterTypes = getParameterTypes(ctor);

                if (getConvertedTypes( context, target, ctor, null, parameterTypes, args, newArgs )) {
                    result = ctor;
                }
            }
        }
        return result;
    }

    /**
        Gets the appropriate method to be called for the given target, method name and arguments.
        If successful this method will return the Method within the target that can be called
        and the converted arguments in actualArgs.  If unsuccessful this method will return
        null and the actualArgs will be empty.
     */
    public static Method getAppropriateMethod( OgnlContext context, Object source, Object target, String methodName, String propertyName, List methods, Object[] args, Object[] actualArgs )
    {
        Method      result = null;
        Class[]     resultParameterTypes = null;

        if (methods != null) {
            for (int i = 0, icount = methods.size(); i < icount; i++) {
                Method  m = (Method)methods.get(i);
                Class[] mParameterTypes = getParameterTypes(m);

                if ( areArgsCompatible(args, mParameterTypes) && ((result == null) || isMoreSpecific(mParameterTypes, resultParameterTypes)) ) {
                    result = m;
                    resultParameterTypes = mParameterTypes;
                    System.arraycopy(args, 0, actualArgs, 0, args.length);
                    for (int j = 0; j < mParameterTypes.length; j++) {
                        Class       type = mParameterTypes[j];

                        if (type.isPrimitive() && (actualArgs[j] == null)) {
                            actualArgs[j] = getConvertedType(context, source, result, propertyName, null, type);
                        }
                    }
                }
            }
        }
        if ( result == null ) {
            result = getConvertedMethodAndArgs( context, target, propertyName, methods, args, actualArgs );
        }
        return result;
    }

    public static Object callAppropriateMethod( OgnlContext context, Object source, Object target, String methodName, String propertyName, List methods, Object[] args ) throws MethodFailedException
    {
        Throwable   reason = null;
        Object[]    actualArgs = objectArrayPool.create(args.length);

        try {
            Method      method = getAppropriateMethod( context, source, target, methodName, propertyName, methods, args, actualArgs );

            if ( (method == null) || !isMethodAccessible(context, source, method, propertyName) )
            {
                StringBuffer        buffer = new StringBuffer();

                if (args != null) {
                    for (int i = 0, ilast = args.length - 1; i <= ilast; i++) {
                        Object      arg = args[i];

                        buffer.append((arg == null) ? NULL_STRING : arg.getClass().getName());
                        if (i < ilast) {
                            buffer.append(", ");
                        }
                    }
                }
                throw new NoSuchMethodException( methodName + "(" + buffer + ")" );
            }
            return invokeMethod(target, method, actualArgs);
          }
        catch (NoSuchMethodException e)
          { reason = e; }
        catch (IllegalAccessException e)
          { reason = e; }
        catch (InvocationTargetException e)
          { reason = e.getTargetException(); }
        finally {
            objectArrayPool.recycle(actualArgs);
        }
        throw new MethodFailedException( source, methodName, reason );
    }

    public static final Object callStaticMethod( OgnlContext context, String className, String methodName, Object[] args ) throws OgnlException, MethodFailedException
    {
        try {
            Object          result;
            Class           targetClass = classForName(context, className);
            MethodAccessor  ma = getMethodAccessor(targetClass);

            return ma.callStaticMethod(context, targetClass, methodName, args);
        } catch (ClassNotFoundException ex) {
            throw new MethodFailedException(className, methodName, ex);
        }
    }

    public static final Object callMethod( OgnlContext context, Object target, String methodName, String propertyName, Object[] args ) throws OgnlException, MethodFailedException
    {
        Object          result;

        if (target != null) {
            MethodAccessor  ma = getMethodAccessor(target.getClass());

            result = ma.callMethod(context, target, methodName, args);
        } else {
            throw new NullPointerException("target is null for method " + methodName);
        }
        return result;
    }

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

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

            for (int i = 0, icount = constructors.size(); i < icount; i++)
            {
                Constructor     c = (Constructor)constructors.get(i);
                Class[]         cParameterTypes = getParameterTypes(c);

                if ( areArgsCompatible(args, cParameterTypes) && (ctor == null || isMoreSpecific(cParameterTypes, ctorParameterTypes)) ) {
                    ctor = c;
                    ctorParameterTypes = cParameterTypes;
                }
              }
            if ( ctor == null )
            {
                actualArgs = objectArrayPool.create(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 e)
          { reason = e; }
        catch (NoSuchMethodException e)
          { reason = e; }
        catch (IllegalAccessException e)
          { reason = e; }
        catch (InvocationTargetException e)
          { reason = e.getTargetException(); }
        catch (InstantiationException e)
          { reason = e; }
        finally {
            if (actualArgs != args) {
                objectArrayPool.recycle(actualArgs);
            }
        }

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

    public static final Object getMethodValue(OgnlContext context, Object target, String propertyName) throws OgnlException, IllegalAccessException, NoSuchMethodException, IntrospectionException
    {
        return getMethodValue(context, target, propertyName, false);
    }

    /**
        If the checkAccessAndExistence flag is true this method will check to see if the
        method exists and if it is accessible according to the context's MemberAccess.
        If neither test passes this will return NotFound.
     */
    public static final Object getMethodValue(OgnlContext context, Object target, String propertyName, boolean checkAccessAndExistence) throws OgnlException, IllegalAccessException, NoSuchMethodException, IntrospectionException
    {
        Object              result = null;
        Method              m = getGetMethod(context, (target == null) ? null : target.getClass(), propertyName);

        if (checkAccessAndExistence) {
            if ((m == null) || !context.getMemberAccess().isAccessible(context, target, m, propertyName)) {
                result = NotFound;
            }
        }
        if (result == null) {
            if (m != null)
            {
                try
                {
                    result = invokeMethod(target, m, NoArguments);
                }
                catch (InvocationTargetException ex)
                {
                    throw new OgnlException(propertyName, ex.getTargetException());
                }
            } else {
                throw new NoSuchMethodException(propertyName);
            }
        }
        return result;
    }

    public static final boolean setMethodValue(OgnlContext context, Object target, String propertyName, Object value) throws OgnlException, IllegalAccessException, NoSuchMethodException, MethodFailedException, IntrospectionException
    {
        return setMethodValue(context, target, propertyName, value, false);
    }

    public static final boolean setMethodValue(OgnlContext context, Object target, String propertyName, Object value, boolean checkAccessAndExistence) throws OgnlException, IllegalAccessException, NoSuchMethodException, MethodFailedException, IntrospectionException
    {
        boolean     result = true;
        Method      m = getSetMethod(context, (target == null) ? null : target.getClass(), propertyName);

        if (checkAccessAndExistence) {
            if ((m == null) || !context.getMemberAccess().isAccessible(context, target, m, propertyName)) {
                result = false;
            }
        }
        if (result) {
            if (m != null) {
                Object[]        args = objectArrayPool.create(value);

                try {
                    callAppropriateMethod(context, target, target, m.getName(), propertyName, Collections.nCopies(1, m), args);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -