package com.co.lane.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
/**
* 配置文件载入到指定对象
* TODO 暂时不睬用自动装配
* @author Xuyd
*/
public class XmlContextUtil {
/**
* 载入的类
*/
private Class<?> clazz;
/**
* 载入的配置文件
*/
private ConfigUtil configUtil;
/**
* 字段容器
*/
private List<Field> listField = new ArrayList<Field>();
/**
*
* @param type
*/
private <T>XmlContextUtil(Class<T> type, ConfigUtil configUtil) {
this.clazz = type;
this.configUtil = configUtil;
// add field
int modify = (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED);
for (Field f : clazz.getDeclaredFields()) {
if (f.getModifiers() < modify) {
Annotation[] annotationX = f.getAnnotations();
if(annotationX != null && annotationX.length > 0){
listField.add(f);
}
}
}
}
public static <T> XmlContextUtil forClass(Class<T> type, ConfigUtil configUtil) {
return new XmlContextUtil(type, configUtil);
}
public <T> void loadXml(T t){
for(Field f : listField){
Annotation a = f.getAnnotations()[0];
String name = "";
if(a instanceof XmlElement){
name = ((XmlElement) a).name();
}else if(a instanceof XmlAttribute){
name = ((XmlAttribute) a).name();
}
}
}
}