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

📄 methodutils.java

📁 apache beanutils开源项目源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                                 specVersion.charAt(0) == '2' ||                                 specVersion.charAt(0) == '3')) {                                                             vunerableJVM = true;                        }                    } catch (SecurityException e) {                        // don't know - so display warning                        vunerableJVM = true;                    }                    if (vunerableJVM) {                        log.warn(                            "Current Security Manager restricts use of workarounds for reflection bugs "                            + " in pre-1.4 JVMs.");                    }                    loggedAccessibleWarning = true;                }                log.debug(                        "Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.",                         se);            }            cache.put(md, method);            return method;                    } catch (NoSuchMethodException e) { /* SWALLOW */ }                // search through all methods         int paramSize = parameterTypes.length;        Method[] methods = clazz.getMethods();        for (int i = 0, size = methods.length; i < size ; i++) {            if (methods[i].getName().equals(methodName)) {	                // log some trace information                if (log.isTraceEnabled()) {                    log.trace("Found matching name:");                    log.trace(methods[i]);                }                                                // compare parameters                Class[] methodsParams = methods[i].getParameterTypes();                int methodParamSize = methodsParams.length;                if (methodParamSize == paramSize) {                              boolean match = true;                    for (int n = 0 ; n < methodParamSize; n++) {                        if (log.isTraceEnabled()) {                            log.trace("Param=" + parameterTypes[n].getName());                            log.trace("Method=" + methodsParams[n].getName());                        }                        if (!isAssignmentCompatible(methodsParams[n], parameterTypes[n])) {                            if (log.isTraceEnabled()) {                                log.trace(methodsParams[n] + " is not assignable from "                                             + parameterTypes[n]);                            }                                match = false;                            break;                        }                    }                                        if (match) {                        // get accessible version of method                        Method method = getAccessibleMethod(methods[i]);                        if (method != null) {                            if (log.isTraceEnabled()) {                                log.trace(method + " accessible version of "                                             + methods[i]);                            }                            try {                                //                                // XXX Default access superclass workaround                                // (See above for more details.)                                //                                method.setAccessible(true);                                                            } catch (SecurityException se) {                                // log but continue just in case the method.invoke works anyway                                if (!loggedAccessibleWarning) {                                    log.warn(            "Cannot use JVM pre-1.4 access bug workaround due to restrictive security manager.");                                    loggedAccessibleWarning = true;                                }                                log.debug(            "Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.",                                         se);                            }                            cache.put(md, method);                            return method;                        }                                                log.trace("Couldn't find accessible method.");                    }                }            }        }                // didn't find a match        log.trace("No match found.");        return null;                                            }    /**     * <p>Determine whether a type can be used as a parameter in a method invocation.     * This method handles primitive conversions correctly.</p>     *     * <p>In order words, it will match a <code>Boolean</code> to a <code>boolean</code>,     * a <code>Long</code> to a <code>long</code>,     * a <code>Float</code> to a <code>float</code>,     * a <code>Integer</code> to a <code>int</code>,     * and a <code>Double</code> to a <code>double</code>.     * Now logic widening matches are allowed.     * For example, a <code>Long</code> will not match a <code>int</code>.     *     * @param parameterType the type of parameter accepted by the method     * @param parameterization the type of parameter being tested      *     * @return true if the assignement is compatible.     */    public static final boolean isAssignmentCompatible(Class parameterType, Class parameterization) {        // try plain assignment        if (parameterType.isAssignableFrom(parameterization)) {            return true;        }                if (parameterType.isPrimitive()) {            // this method does *not* do widening - you must specify exactly            // is this the right behaviour?            Class parameterWrapperClazz = getPrimitiveWrapper(parameterType);            if (parameterWrapperClazz != null) {                return parameterWrapperClazz.equals(parameterization);            }        }                return false;    }        /**     * Gets the wrapper object class for the given primitive type class.     * For example, passing <code>boolean.class</code> returns <code>Boolean.class</code>     * @param primitiveType the primitive type class for which a match is to be found     * @return the wrapper type associated with the given primitive      * or null if no match is found     */    public static Class getPrimitiveWrapper(Class primitiveType) {        // does anyone know a better strategy than comparing names?        if (boolean.class.equals(primitiveType)) {            return Boolean.class;        } else if (float.class.equals(primitiveType)) {            return Float.class;        } else if (long.class.equals(primitiveType)) {            return Long.class;        } else if (int.class.equals(primitiveType)) {            return Integer.class;        } else if (short.class.equals(primitiveType)) {            return Short.class;        } else if (byte.class.equals(primitiveType)) {            return Byte.class;        } else if (double.class.equals(primitiveType)) {            return Double.class;        } else if (char.class.equals(primitiveType)) {            return Character.class;        } else {                        return null;        }    }    /**     * Gets the class for the primitive type corresponding to the primitive wrapper class given.     * For example, an instance of <code>Boolean.class</code> returns a <code>boolean.class</code>.      * @param wrapperType the      * @return the primitive type class corresponding to the given wrapper class,     * null if no match is found     */    public static Class getPrimitiveType(Class wrapperType) {        // does anyone know a better strategy than comparing names?        if (Boolean.class.equals(wrapperType)) {            return boolean.class;        } else if (Float.class.equals(wrapperType)) {            return float.class;        } else if (Long.class.equals(wrapperType)) {            return long.class;        } else if (Integer.class.equals(wrapperType)) {            return int.class;        } else if (Short.class.equals(wrapperType)) {            return short.class;        } else if (Byte.class.equals(wrapperType)) {            return byte.class;        } else if (Double.class.equals(wrapperType)) {            return double.class;        } else if (Character.class.equals(wrapperType)) {            return char.class;        } else {            if (log.isDebugEnabled()) {                log.debug("Not a known primitive wrapper class: " + wrapperType);            }            return null;        }    }        /**     * Find a non primitive representation for given primitive class.     *     * @param clazz the class to find a representation for, not null     * @return the original class if it not a primitive. Otherwise the wrapper class. Not null     */    public static Class toNonPrimitiveClass(Class clazz) {        if (clazz.isPrimitive()) {            Class primitiveClazz = MethodUtils.getPrimitiveWrapper(clazz);            // the above method returns             if (primitiveClazz != null) {                return primitiveClazz;            } else {                return clazz;            }        } else {            return clazz;        }    }        /**     * Represents the key to looking up a Method by reflection.     */    private static class MethodDescriptor {        private Class cls;        private String methodName;        private Class[] paramTypes;        private boolean exact;        private int hashCode;        /**         * The sole constructor.         *         * @param cls  the class to reflect, must not be null         * @param methodName  the method name to obtain         * @param paramTypes the array of classes representing the paramater types         * @param exact whether the match has to be exact.         */        public MethodDescriptor(Class cls, String methodName, Class[] paramTypes, boolean exact) {            if (cls == null) {                throw new IllegalArgumentException("Class cannot be null");            }            if (methodName == null) {                throw new IllegalArgumentException("Method Name cannot be null");            }            if (paramTypes == null) {                paramTypes = emptyClassArray;            }            this.cls = cls;            this.methodName = methodName;            this.paramTypes = paramTypes;            this.exact= exact;            this.hashCode = methodName.length();        }        /**         * Checks for equality.         * @param obj object to be tested for equality         * @return true, if the object describes the same Method.         */        public boolean equals(Object obj) {            if (!(obj instanceof MethodDescriptor)) {                return false;            }            MethodDescriptor md = (MethodDescriptor)obj;            return (                exact == md.exact &&                methodName.equals(md.methodName) &&                cls.equals(md.cls) &&                java.util.Arrays.equals(paramTypes, md.paramTypes)            );        }        /**         * Returns the string length of method name. I.e. if the         * hashcodes are different, the objects are different. If the         * hashcodes are the same, need to use the equals method to         * determine equality.         * @return the string length of method name.         */        public int hashCode() {            return hashCode;        }    }}

⌨️ 快捷键说明

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