xworkconverter.java

来自「在Struts2中的jar包xwork的源代码.版本为2.0.7」· Java 代码 · 共 840 行 · 第 1/3 页

JAVA
840
字号
        } catch (Exception ex) {            LOG.error("Problem loading properties for " + clazz.getName(), ex);        }        // Process annotations        Annotation[] annotations = clazz.getAnnotations();        for (Annotation annotation : annotations) {            if (annotation instanceof Conversion) {                Conversion conversion = (Conversion) annotation;                for (TypeConversion tc : conversion.conversions()) {                    String key = tc.key();                    if (mapping.containsKey(key)) {                        break;                    }                    if (LOG.isDebugEnabled()) {                        LOG.debug(key + ":" + key);                    }                    if (key != null) {                        try {                        	if (tc.type()  == ConversionType.APPLICATION) {                                defaultMappings.put(key, createTypeConverter(tc.converter()));                            } else {                                if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY) || tc.rule().toString().equals(ConversionRule.CREATE_IF_NULL)) {                                    mapping.put(key, tc.value());                                }                                //for properties of classes                                else if (!(tc.rule().toString().equals(ConversionRule.ELEMENT.toString())) ||                                        tc.rule().toString().equals(ConversionRule.KEY.toString()) ||                                        tc.rule().toString().equals(ConversionRule.COLLECTION.toString())                                        ) {                                    mapping.put(key, createTypeConverter(tc.converter()));                                }                                //for keys of Maps                                else if (tc.rule().toString().equals(ConversionRule.KEY.toString())) {                                    Class converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());                                    if (LOG.isDebugEnabled()) {                                        LOG.debug("Converter class: " + converterClass);                                    }                                    //check if the converter is a type converter if it is one                                    //then just put it in the map as is. Otherwise                                    //put a value in for the type converter of the class                                    if (converterClass.isAssignableFrom(TypeConverter.class)) {                                        mapping.put(key, createTypeConverter(tc.converter()));                                    } else {                                        mapping.put(key, converterClass);                                        if (LOG.isDebugEnabled()) {                                            LOG.debug("Object placed in mapping for key "                                                    + key                                                    + " is "                                                    + mapping.get(key));                                        }                                    }                                }                                //elements(values) of maps / lists                                else {                                    mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));                                }                            }                        } catch (Exception e) {                        }                    }                }            }        }        Method[] methods = clazz.getMethods();        for (Method method : methods) {            annotations = method.getAnnotations();            for (Annotation annotation : annotations) {                if (annotation instanceof TypeConversion) {                    TypeConversion tc = (TypeConversion) annotation;                    String key = tc.key();                    if (mapping.containsKey(key)) {                        break;                    }                    // Default to the property name                    if ( key != null && key.length() == 0) {                        key = AnnotationUtils.resolvePropertyName(method);                        LOG.debug("key from method name... " + key + " - " + method.getName());                    }                    if (LOG.isDebugEnabled()) {                        LOG.debug(key + ":" + key);                    }                    if (key != null) {                        try {                        	if (tc.type() == ConversionType.APPLICATION) {                                defaultMappings.put(key, createTypeConverter(tc.converter()));                            } else {                                if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY)) {                                    mapping.put(key, tc.value());                                }                                //for properties of classes                                else if (!(tc.rule().toString().equals(ConversionRule.ELEMENT.toString())) ||                                        tc.rule().toString().equals(ConversionRule.KEY.toString()) ||                                        tc.rule().toString().equals(ConversionRule.COLLECTION.toString())                                        ) {                                    mapping.put(key, createTypeConverter(tc.converter()));                                }                                //for keys of Maps                                else if (tc.rule().toString().equals(ConversionRule.KEY.toString())) {                                    Class converterClass = Thread.currentThread().getContextClassLoader().loadClass(tc.converter());                                    if (LOG.isDebugEnabled()) {                                        LOG.debug("Converter class: " + converterClass);                                    }                                    //check if the converter is a type converter if it is one                                    //then just put it in the map as is. Otherwise                                    //put a value in for the type converter of the class                                    if (converterClass.isAssignableFrom(TypeConverter.class)) {                                        mapping.put(key, createTypeConverter(tc.converter()));                                    } else {                                        mapping.put(key, converterClass);                                        if (LOG.isDebugEnabled()) {                                            LOG.debug("Object placed in mapping for key "                                                    + key                                                    + " is "                                                    + mapping.get(key));                                        }                                    }                                }                                //elements(values) of maps / lists                                else {                                    mapping.put(key, Thread.currentThread().getContextClassLoader().loadClass(tc.converter()));                                }                            }                        } catch (Exception e) {                        }                    }                }            }        }            }    /**     * Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding     * any additional mappings it may find.  Mappings lower in the hierarchy have priority over those higher in the     * hierarcy.     *     * @param clazz the class to look for converter mappings for     * @return the converter mappings     */    private Map<String, Object> buildConverterMapping(Class clazz) throws Exception {        Map<String, Object> mapping = new HashMap<String, Object>();        // check for conversion mapping associated with super classes and any implemented interfaces        Class curClazz = clazz;        while (!curClazz.equals(Object.class)) {            // add current class' mappings            addConverterMapping(mapping, curClazz);            // check interfaces' mappings            Class[] interfaces = curClazz.getInterfaces();            for (int x = 0; x < interfaces.length; x++) {                addConverterMapping(mapping, interfaces[x]);            }            curClazz = curClazz.getSuperclass();        }        if (mapping.size() > 0) {            mappings.put(clazz, mapping);        } else {            noMapping.add(clazz);        }        return mapping;    }    private Map<String, Object> conditionalReload(Class clazz, Map<String, Object> oldValues) throws Exception {        Map<String, Object> mapping = oldValues;        if (FileManager.isReloadingConfigs()) {            if (FileManager.fileNeedsReloading(buildConverterFilename(clazz))) {                mapping = buildConverterMapping(clazz);            }        }        return mapping;    }    TypeConverter createTypeConverter(String className) throws Exception {        // type converters are used across users        return (TypeConverter) ObjectFactory.getObjectFactory().buildBean(className, null);    }    public void loadConversionProperties(String propsName) throws IOException {        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsName);        Properties props = new Properties();        props.load(is);        if (LOG.isDebugEnabled()) {            LOG.debug("processing conversion file ["+propsName+"]");        }                for (Iterator iterator = props.entrySet().iterator();             iterator.hasNext();) {            Map.Entry entry = (Map.Entry) iterator.next();            String key = (String) entry.getKey();            try {                TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());                if (LOG.isDebugEnabled()) {                    LOG.debug("\t"+key + ":" + entry.getValue()+" [treated as TypeConverter "+_typeConverter+"]");                }                defaultMappings.put(key, _typeConverter);            } catch (Exception e) {                LOG.error("Conversion registration error", e);            }        }    }    /**     * Recurses through a class' interfaces and class hierarchy looking for a TypeConverter in the default mapping that     * can handle the specified class.     *     * @param clazz the class the TypeConverter must handle     * @return a TypeConverter to handle the specified class or null if none can be found     */    TypeConverter lookupSuper(Class clazz) {        TypeConverter result = null;        if (clazz != null) {            result = (TypeConverter) defaultMappings.get(clazz.getName());            if (result == null) {                // Looks for direct interfaces (depth = 1 )                Class[] interfaces = clazz.getInterfaces();                for (int i = 0; i < interfaces.length; i++) {                    if (defaultMappings.containsKey(interfaces[i].getName())) {                        result = (TypeConverter) defaultMappings.get(interfaces[i].getName());                        break;                    }                }                if (result == null) {                    // Looks for the superclass                    // If 'clazz' is the Object class, an interface, a primitive type or void then clazz.getSuperClass() returns null                    result = lookupSuper(clazz.getSuperclass());                }            }        }        return result;    }    public ObjectTypeDeterminer getObjectTypeDeterminer() {        if (objectTypeDeterminer == null) {        	return ObjectTypeDeterminerFactory.getInstance();        } else {        	return objectTypeDeterminer;        }    }    /**     * @param determiner     */    public void setObjectTypeDeterminer(ObjectTypeDeterminer determiner) {        objectTypeDeterminer = determiner;    }}

⌨️ 快捷键说明

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