defaultconvertermanager.java

来自「反向的AJAX。最大的特性是我们成为反向的Ajax。DWR1.x允许你用java」· Java 代码 · 共 447 行 · 第 1/2 页

JAVA
447
字号
     * @return The converter for the given type     */    private Converter getConverter(Object object)    {        if (object == null)        {            return getConverter(Void.TYPE);        }        return getConverter(object.getClass());    }    /**     * When we are using typed Javascript names we sometimes want to get a     * specially named converter     * @param paramType The class that we are converting to     * @param type The type name as passed in from the client     * @return The Converter that matches this request (if any)     * @throws MarshallException     */    protected Converter getNamedConverter(Class paramType, String type) throws MarshallException    {        if (type.startsWith("Object_"))        {            // Extract the JavaScript classname from the inbound type            String javascriptClassName = type.substring("Object_".length());            // Locate a converter for this JavaScript classname            Iterator it = converters.entrySet().iterator();            while (it.hasNext())            {                Map.Entry entry = (Map.Entry) it.next();                String match = (String) entry.getKey();                Converter conv = (Converter) entry.getValue();                // JavaScript mapping is only applicable for compound converters                if (conv instanceof NamedConverter)                {                    NamedConverter boConv = (NamedConverter) conv;                    if (boConv.getJavascript() != null && boConv.getJavascript().equals(javascriptClassName))                    {                        // We found a potential converter! But is the                        // converter's Java class compatible with the                        // parameter type?                        try                        {                            Class inboundClass = LocalUtil.classForName(match);                            if (paramType.isAssignableFrom(inboundClass))                            {                                // Hack: We also want to make sure that the                                // converter creates its object based on the                                // inbound class instead of the parameter                                // type, and we have to use the other ref                                // for this:                                boConv.setInstanceType(inboundClass);                                return boConv;                            }                        }                        catch (ClassNotFoundException ex)                        {                            throw new MarshallException(paramType, ex);                        }                    }                }            }        }        return null;    }    /**     * @param paramType The type to find a converter for     * @return The converter for the given type, or null if one can't be found     */    private Converter getConverter(Class paramType)    {        // Can we find a converter assignable to paramType in the HashMap?        Converter converter = getConverterAssignableFrom(paramType);        if (converter != null)        {            return converter;        }        String lookup = paramType.getName();        // Before we start trying for a match on package parts we check for        // dynamic proxies        if (lookup.startsWith("$Proxy"))        {            converter = (Converter) converters.get("$Proxy*");            if (converter != null)            {                return converter;            }        }        while (true)        {            // Can we find a converter using wildcards?            converter = (Converter) converters.get(lookup + ".*");            if (converter != null)            {                return converter;            }            // Arrays can have wildcards like [L* so we don't require a '.'            converter = (Converter) converters.get(lookup + '*');            if (converter != null)            {                return converter;            }            // Give up if the name is now empty            if (lookup.length() == 0)            {                break;            }            // Strip of the component after the last .            int lastdot = lookup.lastIndexOf('.');            if (lastdot != -1)            {                lookup = lookup.substring(0, lastdot);            }            else            {                int arrayMarkers = 0;                while (lookup.charAt(arrayMarkers) == '[')                {                    arrayMarkers++;                }                if (arrayMarkers == 0)                {                    // so we are out of dots and out of array markers                    // bail out.                    break;                }                // We want to keep the type marker too                lookup = lookup.substring(arrayMarkers - 1, arrayMarkers + 1);                // Now can we find it?                converter = (Converter) converters.get(lookup);                if (converter != null)                {                    return converter;                }            }        }        return null;    }    /**     * @param paramType The type to find a converter for     * @return The converter assignable for the given type, or null if one can't be found     */    private Converter getConverterAssignableFrom(Class paramType)    {        if (paramType == null)        {            return null;        }        String lookup = paramType.getName();        // Can we find the converter for paramType in the converters HashMap?        Converter converter = (Converter) converters.get(lookup);        if (converter != null)        {            return converter;        }        // Lookup all of the interfaces of this class for a match        Class[] interfaces = paramType.getInterfaces();        for (int i = 0; i < interfaces.length; i++)        {            converter = getConverterAssignableFrom(interfaces[i]);            if (converter != null)            {                converters.put(lookup, converter);                return converter;            }        }        // Let's search it in paramType superClass        converter = getConverterAssignableFrom(paramType.getSuperclass());        if (converter != null)        {            converters.put(lookup, converter);        }        return converter;    }    /**     * Where we store real type information behind generic types     */    private Map extraTypeInfoMap = new HashMap();    /**     * The log stream     */    private static final Logger log = Logger.getLogger(DefaultConverterManager.class);    /**     * The list of the available converters     */    private Map converterTypes = new HashMap();    /**     * The list of the configured converters     */    private Map converters = new HashMap();    /**     * The properties that we don't warn about if they don't exist.     * @see DefaultConverterManager#addConverter(String, String, Map)     */    private static List ignore = Arrays.asList(new String[] { "converter", "match" });}

⌨️ 快捷键说明

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