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

📄 localutil.java

📁 反向的AJAX。最大的特性是我们成为反向的Ajax。DWR1.x允许你用javascript异步的访问java代码。DWR2.0在这上允许你建立异步java访问javascript代码。 反向的Aj
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            Object value = entry.getValue();            try            {                setProperty(object, key, value);            }            catch (NoSuchMethodException ex)            {                if (ignore != null && !ignore.contains(key))                {                    log.warn("No property '" + key + "' on " + object.getClass().getName());                }            }            catch (InvocationTargetException ex)            {                log.warn("Error setting " + key + "=" + value + " on " + object.getClass().getName(), ex.getTargetException());            }            catch (Exception ex)            {                log.warn("Error setting " + key + "=" + value + " on " + object.getClass().getName(), ex);            }        }    }    /**     * Set a property on an object using reflection     * @param object The object to call the setter on     * @param key The name of the property to set.     * @param value The new value to use for the property     * @throws NoSuchMethodException Passed on from reflection code     * @throws SecurityException Passed on from reflection code     * @throws IllegalAccessException Passed on from reflection code     * @throws IllegalArgumentException Passed on from reflection code     * @throws InvocationTargetException Passed on from reflection code     */    public static void setProperty(Object object, String key, Object value) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException    {        Class real = object.getClass();        String setterName = "set" + key.substring(0, 1).toUpperCase(Locale.ENGLISH) + key.substring(1);        try        {            // Can we work with whatever type we were given?            Method method = real.getMethod(setterName, new Class[] { value.getClass() });            method.invoke(object, new Object[] { value });            return;        }        catch (NoSuchMethodException ex)        {            // If it is a string then next we try to coerce it to the right type            // otherwise we give up.            if (!(value instanceof String))            {                throw ex;            }        }        Method[] methods = real.getMethods();        for (int i = 0; i < methods.length; i++)        {            Method setter = methods[i];            if (setter.getName().equals(setterName) && setter.getParameterTypes().length == 1)            {                Class propertyType = setter.getParameterTypes()[0];                try                {                    Object param = LocalUtil.simpleConvert((String) value, propertyType);                    setter.invoke(object, new Object[] { param });                    return;                }                catch (IllegalArgumentException ex)                {                    // The conversion failed - it was speculative anyway so we                    // don't worry now                }            }        }        throw new NoSuchMethodException("Failed to find a property called: " + key + " on " + object.getClass().getName());    }    /**     * Can the type be used in a call to {@link #simpleConvert(String, Class)}?     * @param paramType The type to test     * @return true if the type is acceptable to simpleConvert()     */    public static boolean isTypeSimplyConvertable(Class paramType)    {        return paramType == String.class ||            paramType == Integer.class ||            paramType == Integer.TYPE ||            paramType == Short.class ||            paramType == Short.TYPE ||            paramType == Byte.class ||            paramType == Byte.TYPE ||            paramType == Long.class ||            paramType == Long.TYPE ||            paramType == Float.class ||            paramType == Float.TYPE ||            paramType == Double.class ||            paramType == Double.TYPE ||            paramType == Character.class ||            paramType == Character.TYPE ||            paramType == Boolean.class ||            paramType == Boolean.TYPE;    }    /**     * A very simple conversion function for all the IoC style setup and     * reflection that we are doing.     * @param value The value to convert     * @param paramType The type to convert to. Currently ony primitive types and     * String are supported.     * @return The converted object.     */    public static Object simpleConvert(String value, Class paramType)    {        if (paramType == String.class)        {            return value;        }        if (paramType == Character.class || paramType == Character.TYPE)        {            if (value.length() == 1)            {                return new Character(value.charAt(0));            }            else            {                throw new IllegalArgumentException("Can't more than one character in string - can't convert to char: '" + value + "'");            }        }        String trimValue = value.trim();        if (paramType == Boolean.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Boolean.valueOf(trimValue);        }        if (paramType == Boolean.TYPE)        {            return Boolean.valueOf(trimValue);        }        if (paramType == Integer.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Integer.valueOf(trimValue);        }        if (paramType == Integer.TYPE)        {            if (trimValue.length() == 0)            {                return new Integer(0);            }            return Integer.valueOf(trimValue);        }        if (paramType == Short.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Short.valueOf(trimValue);        }        if (paramType == Short.TYPE)        {            if (trimValue.length() == 0)            {                return new Short((short) 0);            }            return Short.valueOf(trimValue);        }        if (paramType == Byte.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Byte.valueOf(trimValue);        }        if (paramType == Byte.TYPE)        {            if (trimValue.length() == 0)            {                return new Byte((byte) 0);            }            return Byte.valueOf(trimValue);        }        if (paramType == Long.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Long.valueOf(trimValue);        }        if (paramType == Long.TYPE)        {            if (trimValue.length() == 0)            {                return new Long(0);            }            return Long.valueOf(trimValue);        }        if (paramType == Float.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Float.valueOf(trimValue);        }        if (paramType == Float.TYPE)        {            if (trimValue.length() == 0)            {                return new Float(0);            }            return Float.valueOf(trimValue);        }        if (paramType == Double.class)        {            if (trimValue.length() == 0)            {                return null;            }            return Double.valueOf(trimValue);        }        if (paramType == Double.TYPE)        {            if (trimValue.length() == 0)            {                return new Double(0);            }            return Double.valueOf(trimValue);        }        throw new IllegalArgumentException("Unsupported conversion type: " + paramType.getName());    }    /**     * Get the short class name (i.e. without the package part)     * @param clazz the class to get the short name of     * @return the class name of the class without the package name     */    public static String getShortClassName(Class clazz)    {        String className = clazz.getName();        char[] chars = className.toCharArray();        int lastDot = 0;        for (int i = 0; i < chars.length; i++)        {            if (chars[i] == '.')            {                lastDot = i + 1;            }            else if (chars[i] == '$')            {                chars[i] = '.';            }        }        // This might come up in scans for locale/charset issues. It's not an        // issue since we are talking about chars.        return new String(chars, lastDot, chars.length - lastDot);    }    /**     * Is this object property one that we can use in a JSON style or do we need     * to get fancy. i.e does it contain only letters and numbers with an     * initial letter.     * @param name The name to test for JSON compatibility     * @return true if the name is simple     */    public static boolean isSimpleName(String name)    {        if (name.length() == 0)        {            return false;        }        if (JavascriptUtil.isReservedWord(name))        {            return false;        }        boolean isSimple = Character.isLetter(name.charAt(0));        for (int i = 1; isSimple && i < name.length(); i++)        {            if (!Character.isLetterOrDigit(name.charAt(i)))            {                isSimple = false;            }        }        return isSimple;    }    /**     * Utility to essentially do Class forName and allow configurable     * Classloaders.     * <p>The initial implementation makes use of the context classloader for     * the current thread.     * @param className The class to create     * @return The class if it is safe or null otherwise.

⌨️ 快捷键说明

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