Java 反射研究Demo

需要JDom.jar



package test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class XmlUtil {

private static final String PREFIX_SET = "set";

private static final String TYPE = "type";
private static final String PATTERN = "pattern";
private static final String NAME = "name";
private static final String CLASS = "class";

private static final String DATE = "Date";
private static final String LIST = "List";
private static final String ENTITY = "entity";

public List<Object> parse(String filename) throws Exception {

SAXBuilder builder = new SAXBuilder();
InputStream in = null;
try {
in = new FileInputStream(filename);
Document document = builder.build(in);

Element root = document.getRootElement();
return convertToEntity(root, null);
}
finally {
if (in != null) {
in.close();
}

}

}

private Object getDateValue(String type, String valueText, String pattern) {
return new Date();
}

private Object convertTypeValue(String type, String valueText) throws InstantiationException, IllegalAccessException, ClassNotFoundException {

if ("java.lang.String".equals(type)) {
return valueText;
}

if ("byte".equals(type) || "java.lang.Byte".equals(type)) {
return Byte.valueOf(valueText);
}

if ("char".equals(type) || "java.lang.Character".equals(type)) {
return Character.valueOf(valueText.charAt(0));
}

if ("int".equals(type) || "java.lang.Integer".equals(type)) {
return Integer.valueOf(valueText);
}

if ("long".equals(type) || "java.lang.Long".equals(type)) {
return Long.valueOf(valueText);
}

if ("float".equals(type) || "java.lang.Float".equals(type)) {
return Float.valueOf(valueText);
}

if ("double".equals(type) || "java.lang.Double".equals(type)) {
return Double.valueOf(valueText);
}

if ("java.math.BigDecimal".equals(type)) {
return BigDecimal.valueOf(Double.valueOf(valueText));
}

if ("boolean".equals(type) || "java.lang.Boolean".equals(type)) {
return Boolean.valueOf(valueText);
}

return null;

}

private List<Object> convertToEntity(Element root, String clazz) throws Exception {

List<Object> entities = new ArrayList<Object>();

String entityName = root.getAttributeValue(CLASS);
if (clazz != null) {
entityName = clazz;
}

List<Element> elements = root.getChildren();
if (clazz == null) {
for (Element record : elements) {
Object entity = Class.forName(entityName).newInstance();
Method[] methods = Class.forName(entityName).getDeclaredMethods();
for (Element property : record.getChildren()) {
setProperty(entity, methods, property);
}
entities.add(entity);
}
}
else {
Object entity = Class.forName(entityName).newInstance();
Method[] methods = Class.forName(entityName).getDeclaredMethods();
for (Element property : elements) {
setProperty(entity, methods, property);
}
entities.add(entity);
}
return entities;
}

private void setProperty(Object entity, Method[] methods, Element property) throws Exception {
String name = property.getName();
String text = property.getTextTrim();
Object value = null;
String fieldType = property.getAttributeValue(TYPE);
if (DATE.equalsIgnoreCase(fieldType)) {
String pattern = property.getAttributeValue(PATTERN);
value = getDateValue(fieldType, text, pattern);
setPropertyValue(entity, methods, name, value, false);
}
else if (ENTITY.equalsIgnoreCase(property.getName())) {
name = property.getAttributeValue(NAME);
if (!isEmpty(name)) {
String clazzName = property.getAttributeValue(CLASS);
value = convertToEntity(property, clazzName).get(0);
}
setPropertyValue(entity, methods, name, value, false);
}
else if (LIST.equalsIgnoreCase(property.getName())) {
name = property.getAttributeValue(NAME);
List<Object> list = new ArrayList<Object>();
for (Element item : property.getChildren()) {
Object obj = convertToEntity(item, property.getAttributeValue(CLASS)).get(0);
list.add(obj);
}
value = list;
setPropertyValue(entity, methods, name, value, false);
}
else {
setPropertyValue(entity, methods, name, text, true);
}

}

private void setPropertyValue(Object entity, Method[] methods, String name, Object value, boolean isPrimate) throws Exception {
for (Method method : methods) {
String methodName = method.getName();
boolean isSetMethod = methodName.startsWith(PREFIX_SET) && methodName.equalsIgnoreCase(PREFIX_SET + name);
if (isSetMethod) {
if (isPrimate) {
Class<?>[] types = method.getParameterTypes();
Object obj = convertTypeValue(types[0].getName(), value.toString());
method.invoke(entity, obj);
}
else {
method.invoke(entity, value);
}
break;
}
}
}

private boolean isEmpty(String str) {
if (str == null || str.equals("")) {
return true;
}
else {
return false;
}
}

public static void main(String[] args) {
String input = "C:/dev/demo.xml";
XmlUtil test = new XmlUtil();
List<Object> entities;
try {
entities = test.parse(input);
System.out.println("Size: " + entities.size());
for (Object obj : entities) {
System.out.println("name: " + ((Person) obj).getName());
System.out.println("book: " + ((Person) obj).getBook().getName());
}
}
catch (Exception e) {
e.printStackTrace();
}
}

}





<?xml version="1.0" encoding="UTF-8"?>
<entities class="test.Person">
<entity id="234">
<name>David</name>
<age>12</age>
<birthday type="Date" pattern="yyyyy-mm-dd">2011-08-10</birthday>

<entity name="book" class="test.Book">
<name>Love 1+1</name>
<author>Victor</author>
</entity>
<list name="books" class="test.Book">
<entity id="" >
<name>simple love</name>
<author>Steven</author>
</entity>
<entity id="">
<name>deep love</name>
<author>David</author>
</entity>
</list>
</entity>
</entities>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值