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

📄 ognlruntime.java

📁 此源代码是一个开源项目,可以实现不同类型之间的转换
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    public static final Method getSetMethod(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, true /* find 'set' 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 == 1) {
                        result = m;
                        break;
                    }
                }
            }
        } else {
            result = pd.getWriteMethod();
        }
        return result;
    }

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

    public static final boolean hasGetProperty( OgnlContext context, Object target, Object oname ) throws IntrospectionException, OgnlException
    {
        Class       targetClass = (target == null) ? null : target.getClass();
        String      name = oname.toString();

        return hasGetMethod( context, target, targetClass, name ) || hasField( context, target, targetClass, name );
    }

    public static final boolean hasSetProperty( OgnlContext context, Object target, Object oname ) throws IntrospectionException, OgnlException
    {
        Class       targetClass = (target == null) ? null : target.getClass();
        String      name = oname.toString();

        return hasSetMethod( context, target, targetClass, name ) || hasField( context, target, targetClass, name );
    }

    private static final boolean indexMethodCheck(List methods)
    {
        boolean         result = false;

        if (methods.size() > 0) {
            Method          fm = (Method)methods.get(0);
            Class[]         fmpt = getParameterTypes(fm);
            int             fmpc = fmpt.length;
            Class           lastMethodClass = fm.getDeclaringClass();

            result = true;
            for (int i = 1; result && (i < methods.size()); i++) {
                Method      m = (Method)methods.get(i);
                Class       c = m.getDeclaringClass();

                // Check to see if more than one method implemented per class
                if (lastMethodClass == c) {
                    result = false;
                } else {
                    Class[]     mpt = getParameterTypes(fm);
                    int         mpc = fmpt.length;

                    if (fmpc != mpc) {
                        result = false;
                    }
                    for (int j = 0; j < fmpc; j++) {
                        if (fmpt[j] != mpt[j]) {
                            result = false;
                            break;
                        }
                    }
                }
                lastMethodClass = c;
            }
        }
        return result;
    }

    private static final void findObjectIndexedPropertyDescriptors(Class targetClass, Map intoMap) throws OgnlException
    {
        Map     allMethods = getMethods(targetClass, false);
        Map     pairs = new HashMap(101);

        for (Iterator it = allMethods.keySet().iterator(); it.hasNext(); ) {
            String      methodName = (String)it.next();
            List        methods = (List)allMethods.get(methodName);

            /*
                Only process set/get where there is exactly one implementation
                of the method per class and those implementations are all the
                same
             */
            if (indexMethodCheck(methods)) {
                boolean     isGet = false,
                            isSet = false;
                Method      m = (Method)methods.get(0);

                if (((isSet = methodName.startsWith(SET_PREFIX)) || (isGet = methodName.startsWith(GET_PREFIX))) && (methodName.length() > 3)) {
                    String      propertyName = Introspector.decapitalize(methodName.substring(3));
                    Class[]     parameterTypes = getParameterTypes(m);
                    int         parameterCount = parameterTypes.length;

                    if (isGet && (parameterCount == 1) && (m.getReturnType() != Void.TYPE)) {
                        List        pair = (List)pairs.get(propertyName);

                        if (pair == null) {
                            pairs.put(propertyName, pair = new ArrayList());
                        }
                        pair.add(m);
                    }
                    if (isSet && (parameterCount == 2) && (m.getReturnType() == Void.TYPE)) {
                        List        pair = (List)pairs.get(propertyName);

                        if (pair == null) {
                            pairs.put(propertyName, pair = new ArrayList());
                        }
                        pair.add(m);
                    }
                }
            }
        }
        for (Iterator it = pairs.keySet().iterator(); it.hasNext();) {
            String      propertyName = (String)it.next();
            List        methods = (List)pairs.get(propertyName);

            if (methods.size() == 2) {
                Method      method1 = (Method)methods.get(0),
                            method2 = (Method)methods.get(1),
                            setMethod = (method1.getParameterTypes().length == 2) ? method1 : method2,
                            getMethod = (setMethod == method1) ? method2 : method1;
                Class       keyType = getMethod.getParameterTypes()[0],
                            propertyType = getMethod.getReturnType();

                if (keyType == setMethod.getParameterTypes()[0]) {
                    if (propertyType == setMethod.getParameterTypes()[1]) {
                        ObjectIndexedPropertyDescriptor     propertyDescriptor;

                        try {
                            propertyDescriptor = new ObjectIndexedPropertyDescriptor(propertyName, propertyType, getMethod, setMethod);
                        } catch (Exception ex) {
                            throw new OgnlException("creating object indexed property descriptor for '" + propertyName + "' in " + targetClass, ex);
                        }
                        intoMap.put(propertyName, propertyDescriptor);
                    }
                }

            }
        }
    }

    /**
        This method returns the property descriptors for the given class as a Map
     */
    public static final Map getPropertyDescriptors(Class targetClass) throws IntrospectionException, OgnlException
    {
        Map     result;

        synchronized(propertyDescriptorCache) {
            if ((result = (Map)propertyDescriptorCache.get(targetClass)) == null) {
                PropertyDescriptor[]    pda = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();

                result = new HashMap(101);
                for (int i = 0, icount = pda.length; i < icount; i++) {
                    result.put(pda[i].getName(), pda[i]);
                }
                findObjectIndexedPropertyDescriptors(targetClass, result);
                propertyDescriptorCache.put(targetClass, result);
            }
        }
        return result;
    }

    /**
        This method returns a PropertyDescriptor for the given class and property name using
        a Map lookup (using getPropertyDescriptorsMap()).
     */
    public static final PropertyDescriptor getPropertyDescriptor(Class targetClass, String propertyName) throws IntrospectionException, OgnlException
    {
        return (targetClass == null) ? null : (PropertyDescriptor)getPropertyDescriptors(targetClass).get(propertyName);
    }

    public static final PropertyDescriptor[] getPropertyDescriptorsArray(Class targetClass) throws IntrospectionException
    {
        PropertyDescriptor[]    result = null;

        if (targetClass != null) {
            synchronized(propertyDescriptorCache) {
                if ((result = (PropertyDescriptor[])propertyDescriptorCache.get(targetClass)) == null) {
                    propertyDescriptorCache.put(targetClass, result = Introspector.getBeanInfo(targetClass).getPropertyDescriptors());
                }
            }
        }
        return result;
    }

    /**
        Gets the property descriptor with the given name for the target class given.
        @param targetClass      Class for which property descriptor is desired
        @param name             Name of property
        @return                 PropertyDescriptor of the named property or null if
                                the class has no property with the given name
     */
    public static final PropertyDescriptor getPropertyDescriptorFromArray(Class targetClass, String name) throws IntrospectionException
    {
        PropertyDescriptor      result = null;
        PropertyDescriptor[]    pda = getPropertyDescriptorsArray(targetClass);

        for (int i = 0, icount = pda.length; (result == null) && (i < icount); i++) {
            if (pda[i].getName().compareTo(name) == 0) {
                result = pda[i];
            }
        }
        return result;
    }

    public static final void setMethodAccessor(Class cls, MethodAccessor accessor)
    {
        synchronized(methodAccessors) {
            methodAccessors.put( cls, accessor );
        }
    }

    public static final MethodAccessor getMethodAccessor( Class cls ) throws OgnlException
    {
        MethodAccessor answer = (MethodAccessor)getHandler( cls, methodAccessors );
        if ( answer != null )
            return answer;
        throw new OgnlException( "No method accessor for " + cls );
    }

    public static final void setPropertyAccessor(Class cls, PropertyAccessor accessor)
    {
        synchronized(propertyAccessors) {
            propertyAccessors.put( cls, accessor );
        }
    }

    public static final PropertyAccessor getPropertyAccessor( Class cls ) throws OgnlException
    {
        PropertyAccessor answer = (PropertyAccessor)getHandler( cls, propertyAccessors );
        if ( answer != null )
            return answer;

        throw new OgnlException( "No property accessor for class " + cls );
    }

    public static final ElementsAccessor getElementsAccessor( Class cls ) throws OgnlException
    {
        ElementsAccessor answer = (ElementsAccessor)getHandler( cls, elementsAccessors );
        if ( answer != null )
            return answer;
        throw new OgnlException( "No elements accessor for class " + cls );
    }

    public static final void setElementsAccessor( Class cls, ElementsAccessor accessor )
    {
        synchronized(elementsAccessors) {
            elementsAccessors.put( cls, accessor );
        }
    }

    public static final NullHandler getNullHandler( Class cls ) throws OgnlException
    {
        NullHandler answer = (NullHandler)getHandler( cls, nullHandlers );
        if ( answer != null )
            return answer;
        throw new OgnlException( "No null handler for class " + cls );
    }

    public static final void setNullHandler( Class cls, NullHandler handler )
    {
        synchronized(nullHandlers) {
            nullHandlers.put( cls, handler );
        }
    }

    private static final Object getHandler( Class forClass, ClassCache handlers )
    {
        Object answer = null;

        synchronized(handlers) {
            if ((answer = handlers.get(forClass)) == null)
            {
                Class   keyFound;

                if (forClass.isArray())
                {
                    answer = handlers.get(Object[].class);
                    keyFound = null;
                }
                else
                {
                    keyFound = forClass;
                    outer:
                        for ( Class c = forClass; c != null; c = c.getSuperclass() )
                        {
                            answer = handlers.get(c);
                            if ( answer == null )
                            {
                                Class[] interfaces = c.getInterfaces();
                                for ( int index=0, count=interfaces.length; index < count; ++index )
                                {
                                    Class   iface = interfaces[index];

                                    answer = handlers.get(iface);
                                    if (answer == null)
                                    {
                                        /* Try super-interfaces */
                                        answer = getHandler(iface, handlers);
                                    }
                                    if ( answer != null )
                                    {
                                        keyFound = iface;
                                        break outer;
                                    }
                              

⌨️ 快捷键说明

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