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

📄 ognlruntime.java

📁 此源代码是一个开源项目,可以实现不同类型之间的转换
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                } finally {
                    objectArrayPool.recycle(args);
                }
            } else {
                result = false;
            }
        }
        return result;
    }

    public static final List getConstructors(Class targetClass)
    {
        List        result;

        synchronized(constructorCache) {
            if ((result = (List)constructorCache.get(targetClass)) == null) {
                constructorCache.put(targetClass, result = Arrays.asList(targetClass.getConstructors()));
            }
        }
        return result;
    }

    public static final Map getMethods( Class targetClass, boolean staticMethods )
    {
        ClassCache  cache = (staticMethods ? staticMethodCache : instanceMethodCache);
        Map         result;

        synchronized(cache) {
            if ((result = (Map)cache.get(targetClass)) == null)
            {
                cache.put(targetClass, result = new HashMap(23));
                for (Class c = targetClass; c != null; c = c.getSuperclass()) {
                    Method[]        ma = c.getDeclaredMethods();

                    for (int i = 0, icount = ma.length; i < icount; i++)
                    {
                        if (Modifier.isStatic(ma[i].getModifiers()) == staticMethods) {
                            List        ml = (List)result.get(ma[i].getName());

                            if (ml == null)
                                result.put(ma[i].getName(), ml = new ArrayList());
                            ml.add(ma[i]);
                        }
                    }
                }
            }
        }
        return result;
    }

    public static final List getMethods( Class targetClass, String name, boolean staticMethods )
    {
        return (List)getMethods(targetClass, staticMethods).get(name);
    }

    public static final Map getFields(Class targetClass)
    {
        Map         result;

        synchronized(fieldCache) {
            if ((result = (Map)fieldCache.get(targetClass)) == null)
            {
                Field       fa[];

                result = new HashMap(23);
                fa = targetClass.getDeclaredFields();
                for (int i = 0; i < fa.length; i++) {
                    result.put(fa[i].getName(), fa[i]);
                }
                fieldCache.put(targetClass, result);
            }
        }
        return result;
    }

    public static final Field getField(Class inClass, String name)
    {
        Field       result = null;

        synchronized(fieldCache)
        {
            Object      o = getFields(inClass).get(name);

            if (o == null)
            {
                superclasses.clear();
                for (Class sc = inClass; (sc != null) && (result == null); sc = sc.getSuperclass())
                {
                    if ((o = getFields(sc).get(name)) == NotFound)
                        break;
                    superclasses.add(sc);
                    if ((result = (Field)o) != null)
                        break;
                }
                /*
                    Bubble the found value (either cache miss or actual field)
                    to all supeclasses that we saw for quicker access next time.
                */
                for (int i = 0, icount = superclasses.size(); i < icount; i++)
                {
                    getFields((Class)superclasses.get(i)).put(name, (result == null) ? NotFound : result);
                }
            }
            else
            {
                if (o instanceof Field)
                {
                    result = (Field)o;
                }
                else
                {
                    if (result == NotFound)
                        result = null;
                }
            }
        }
        return result;
    }

    public static final Object getFieldValue(OgnlContext context, Object target, String propertyName) throws NoSuchFieldException
    {
        return getFieldValue(context, target, propertyName, false);
    }

    public static final Object getFieldValue(OgnlContext context, Object target, String propertyName, boolean checkAccessAndExistence) throws NoSuchFieldException
    {
        Object          result = null;
        Field           f = getField((target == null) ? null : target.getClass(), propertyName);

        if (checkAccessAndExistence) {
            if ((f == null) || !context.getMemberAccess().isAccessible(context, target, f, propertyName)) {
                result = NotFound;
            }
        }
        if (result == null) {
            if (f == null) {
                throw new NoSuchFieldException(propertyName);
            } else {
                try
                {
                    Object      state = null;

                    if ((f != null) && !Modifier.isStatic(f.getModifiers()))
                    {
                        state = context.getMemberAccess().setup(context, target, f, propertyName);
                        result = f.get(target);
                        context.getMemberAccess().restore(context, target, f, propertyName, state);
                    }
                    else
                        throw new NoSuchFieldException(propertyName);
                }
                catch (IllegalAccessException ex)
                {
                    throw new NoSuchFieldException(propertyName);
                }
            }
        }
        return result;
    }

    public static final boolean setFieldValue(OgnlContext context, Object target, String propertyName, Object value) throws OgnlException
    {
        boolean         result = false;

        try
        {
            Field       f = getField( (target == null) ? null : target.getClass(), propertyName );
            Object      state;

            if ((f != null) && !Modifier.isStatic(f.getModifiers()))
            {
                state = context.getMemberAccess().setup(context, target, f, propertyName);
                try
                {
                    if (isTypeCompatible(value, f.getType()) || ((value = getConvertedType( context, target, f, propertyName, value, f.getType())) != null)) {
                        f.set(target, value);
                        result = true;
                    }
                }
                finally
                {
                    context.getMemberAccess().restore(context, target, f, propertyName, state);
                }
            }
        }
        catch (IllegalAccessException ex)
        {
            throw new NoSuchPropertyException(target, propertyName, ex);
        }
        return result;
    }

    public static final boolean isFieldAccessible(OgnlContext context, Object target, Class inClass, String propertyName)
    {
        return isFieldAccessible(context, target, getField(inClass, propertyName), propertyName);
    }

    public static final boolean isFieldAccessible(OgnlContext context, Object target, Field field, String propertyName)
    {
        return context.getMemberAccess().isAccessible(context, target, field, propertyName);
    }

    public static final boolean hasField(OgnlContext context, Object target, Class inClass, String propertyName)
    {
        Field       f = getField(inClass, propertyName);

        return (f != null) && isFieldAccessible(context, target, f, propertyName);
    }

    public static final Object getStaticField( OgnlContext context, String className, String fieldName ) throws OgnlException
    {
        Exception reason = null;
        try
          {
            Class c = classForName(context, className);

            /*
                Check for virtual static field "class"; this cannot interfere with
                normal static fields because it is a reserved word.
             */
            if (fieldName.equals("class"))
              {
                return c;
              }
            else
              {
                Field f = c.getField(fieldName);
                if ( !Modifier.isStatic(f.getModifiers()) )
                    throw new OgnlException( "Field " + fieldName + " of class " + className + " is not static" );
                return f.get(null);
              }
          }
        catch (ClassNotFoundException e)
          { reason = e; }
        catch (NoSuchFieldException e)
          { reason = e; }
        catch (SecurityException e)
          { reason = e; }
        catch (IllegalAccessException e)
          { reason = e; }

        throw new OgnlException( "Could not get static field " + fieldName + " from class " + className, reason );
    }

    public static final List getDeclaredMethods(Class targetClass, String propertyName, boolean findSets)
    {
        List        result = null;
        ClassCache  cache = declaredMethods[findSets ? 0 : 1];

        synchronized(cache) {
            Map         propertyCache = (Map)cache.get(targetClass);

            if ((propertyCache == null) || ((result = (List)propertyCache.get(propertyName)) == null)) {
                String      baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
                int         len = baseName.length();

                for (Class c = targetClass; c != null; c = c.getSuperclass()) {
                    Method[]        methods = c.getDeclaredMethods();

                    for (int i = 0; i < methods.length; i++) {
                        String      ms = methods[i].getName();

                        if (ms.endsWith(baseName)) {
                            boolean     isSet = false,
                                        isGet = false,
                                        isIs = false;

                            if ((isSet = ms.startsWith(SET_PREFIX)) || (isGet = ms.startsWith(GET_PREFIX)) || (isIs = ms.startsWith(IS_PREFIX))) {
                                int     prefixLength = (isIs ? 2 : 3);

                                if (isSet == findSets) {
                                    if (baseName.length() == (ms.length() - prefixLength)) {
                                        if (result == null) {
                                            result = new ArrayList();
                                        }
                                        result.add(methods[i]);
                                    }
                                }
                            }
                        }
                    }
                }
                if (propertyCache == null) {
                    cache.put(targetClass, propertyCache = new HashMap(101));
                }
                propertyCache.put(propertyName, (result == null) ? NotFoundList : result);
            }
            return (result == NotFoundList) ? null : result;
        }
    }

    public static final Method getGetMethod(OgnlContext context, Class targetClass, String propertyName) throws IntrospectionException, OgnlException
    {
        Method              result = null;
        PropertyDescriptor  pd = getPropertyDescriptor(targetClass, propertyName);

        if (pd == null) {
            List        methods = getDeclaredMethods(targetClass, propertyName, false /* find 'get' methods */);

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

                    if (mParameterTypes.length == 0) {
                        result = m;
                        break;
                    }
                }
            }
        } else {
            result = pd.getReadMethod();
        }
        return result;
    }

    public static final boolean isMethodAccessible(OgnlContext context, Object target, Method method, String propertyName)
    {
        return (method == null) ? false : context.getMemberAccess().isAccessible(context, target, method, propertyName);
    }

    public static final boolean hasGetMethod(OgnlContext context, Object target, Class targetClass, String propertyName) throws IntrospectionException, OgnlException
    {
        return isMethodAccessible(context, target, getGetMethod(context, targetClass, propertyName), propertyName);
    }

⌨️ 快捷键说明

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