反射实战
从xml文件读取到对象的相关信息,根据xml文件中给出的对象类的包名,利用反射创建一个对象,并添加到集合中
Student类
package com.situ.chapterwork;
public class Student {
private String id;
private String name;
private String sex;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private Student(String id, String name, String sex, int age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<class>
<students>
<student class="com.situ.chapterwork.Student" stuid="001">
<name>张三</name>
<sex>男</sex>
<age>22</age>
</student>
<student class="com.situ.chapterwork.Student" stuid="002">
<name>李四</name>
<sex>女</sex>
<age>18</age>
</student>
</students>
</class>
FoundNewStudent类
package com.situ.chapterwork;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class FoundNewStudent {
public static void main(String[] args) {
List<Student> studentslist = new ArrayList<>();
File file = new File("d:/Test/1102.xml");
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(is);
Element root = doc.getRootElement();
Element students = root.element("students");
List<Element> stulist = students.elements();
for(Element e : stulist) {
String id = e.attributeValue("stuid");//获取student标签的stuid属性值
String classname = e.attributeValue("class");//获取student标签的class属性值
String name = e.element("name").getText();//获取student标签内的name标签的值
String sex = e.element("sex").getText();//获取student标签内的sex标签的值
int age = Integer.valueOf(e.element("age").getText()).intValue();//String转整型
Class<?> clazz = Class.forName(classname);//获取classname包的类型
Constructor<?> con = clazz.getDeclaredConstructor(String.class,String.class,String.class,int.class);//注意参数是否与包里的构造方法的参数对应
con.setAccessible(true);//设置访问权限
Student stu = (Student) con.newInstance(id,name,sex,age);//new一个student对象,并利用con.newInstance()给对象的属性赋值
con.setAccessible(false);//关闭访问权限
System.out.println(stu);//打印看看,对象的属性是否赋值
studentslist.add(stu);//存到学生集合中
}
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行效果:
虽然jdk1.7就对异常做了优化,但是仅仅是对IO,不用自己手动关闭流,仅仅方便了程序员一点点,但是依然是异常地狱,现在又出现了一个新的异常地狱,即反射。