反射 XMLUtil

package com.dys.util;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.dys.annotation.DysAnnotation;
import com.dys.model.BeanDefinition;
import com.dys.model.PropertyDefinition;

public class XMLUtils {
    private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
    private Map<String, Object> singletons = new HashMap<String, Object>();
    public XMLUtils(String fileName) {
        this.readXML(fileName);
        this.initilizeBeans();
        this.annotationInject();
        this.injectObject();
    }
    private void annotationInject() {
        try {
            for(String beanName : singletons.keySet()) {
                Object bean = singletons.get(beanName);
                if(bean != null) {
                    PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
                    for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                        Method setMethod = propertyDescriptor.getWriteMethod();
                        if(setMethod != null && setMethod.isAnnotationPresent(DysAnnotation.class)) {
                            DysAnnotation dysA = setMethod.getAnnotation(DysAnnotation.class);
                            Object beanValue = null;
                            if(dysA.name() != null && !"".equals(dysA.name())) {
                                String name = dysA.name();
                                beanValue = singletons.get(name);
                                
                            } else {
                                String nam = propertyDescriptor.getName();
                                beanValue = singletons.get(nam);
                                if(beanValue == null) {
                                    for(String key : singletons.keySet()) {
                                        if(propertyDescriptor.getPropertyType().isAssignableFrom(singletons.get(key).getClass())) {
                                            beanValue = singletons.get(key);
                                            break;
                                        }
                                    }
                                }
                            }
                            setMethod.setAccessible(true);
                            setMethod.invoke(bean, beanValue);
                        }
                    }
                }
                Field[] fields = bean.getClass().getDeclaredFields();
                for(Field field : fields) {
                    if(field != null && field.isAnnotationPresent(DysAnnotation.class)) {
                        DysAnnotation dysA = field.getAnnotation(DysAnnotation.class);
                        Object beanValue = null;
                        if(dysA.name() != null && !"".equals(dysA.name())) {
                            String name = dysA.name();
                            beanValue = singletons.get(name);
                            
                        } else {
                            String nam = field.getName();
                            beanValue = singletons.get(nam);
                            for(String key : singletons.keySet()) {
                                if(field.getType().isAssignableFrom(singletons.get(key).getClass())) {
                                    beanValue = singletons.get(key);
                                    break;
                                }
                            }
                        }
                        field.setAccessible(true);
                        field.set(bean, beanValue);
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void injectObject(){
        try {
            for(BeanDefinition beanDefinition:beanDefinitions) {
                Object object = singletons.get(beanDefinition.getId());
                if(object != null) {
                    PropertyDescriptor[] pds = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
                    for(PropertyDefinition propertyDefinition : beanDefinition.getProperties()) {
                        for(PropertyDescriptor pd : pds) {
                            if(propertyDefinition.getName().equals(pd.getName())) {
                                Method setMethod = pd.getWriteMethod();
                                if(setMethod != null) {
                                    Object propertyV = singletons.get(propertyDefinition.getRef());
                                    setMethod.setAccessible(true);
                                    setMethod.invoke(object, propertyV); 
                                }
                                break;
                            }
                        }
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void initilizeBeans() {
        for(BeanDefinition beanDefinition : beanDefinitions) {
            try {
                if(beanDefinition.getName() != null && !"".equals(beanDefinition.getName().trim())) {
                    singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getName()).newInstance());
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void readXML(String filename) {
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            URL xmlpath = this.getClass().getClassLoader().getResource(filename);
            document = saxReader.read(xmlpath);
            Map<String, String> nsMap = new HashMap<String, String>();
            nsMap.put("ns", "http://www.springframework.org/schema/beans");
            XPath xsub = document.createXPath("//ns:beans/ns:bean");
            xsub.setNamespaceURIs(nsMap);
            List<Element> beans = xsub.selectNodes(document);
            for(Element element:beans){
                String id = element.attributeValue("id");
                String clazz = element.attributeValue("class");
                BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
                XPath xsubProperty = element.createXPath("ns:property");
                xsubProperty.setNamespaceURIs(nsMap);
                List<Element> propertyElement = xsubProperty.selectNodes(element);
                for(Element ele:propertyElement) {
                    String name = ele.attributeValue("name");
                    String ref = ele.attributeValue("ref");
                    PropertyDefinition propertyDefinition = new PropertyDefinition(name, ref);
                    beanDefinition.getProperties().add(propertyDefinition);
                }
                beanDefinitions.add(beanDefinition);
            }            
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    public Object getBean(String name) {
        Object beanClass = singletons.get(name);
        return beanClass;
    }
}

 

你可能感兴趣的:(util)