Employee ct=new Contractor("Joe","IBM");
Employee sup=new Supplemental("Linda","IBM","8");
em.addMemeber(vn1);
em.addMemeber(vn2);
em.addMemeber(ct);
em.addMemeber(sup);
PrintWriter ps;
try {
ps = new PrintWriter(new FileOutputStream(new File("C:\\test.output"),true));
ExportableGenerator eg=new TXTExportableGenerator(ps);
eg.genDoc(em,0);
eg.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
清单23 ExportableGenerator
public abstract class ExportableGenerator {
PrintWriter out = null;
public ExportableGenerator(PrintWriter out) {
super();
this.out = out;
}
public void genDoc(Employee e, int tagNum) {
Class employee = e.getClass();
Field[] fields = employee.getDeclaredFields();
outputFieldHeader(out,e);
for (Field f : fields) {
if (f.isAnnotationPresent(Exportable.class)) {
if (f.getType() != ArrayList.class) {
for(int i=0; i<tagNum;i++){
out.print("***");
}
outputSimpleField(out, f, e);
}else{
try {
ArrayList team=(ArrayList)f.get(e);
out.println("-----------------------------");
for(int i=0;i <team.size();i++){
Employee member=(Employee)team.get(i);
genDoc(member,tagNum+1);
out.println("-----------------------------");
}
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
}
}
}
outputFieldFooter(out,e);
}
public void flush(){
out.flush();
out.close();
}
protected String value(Field f, Object obj) {
Class type = f.getType();
try {
if (type == String.class)
return (String) f.get(obj);
if (type == Date.class) {
return DateFormat.getDateInstance().format((Date)f.get(obj));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
return f.getName();
} catch (IllegalAccessException e) {
e.printStackTrace();
return f.getName();
}
return f.getName();
}
protected abstract void outputSimpleField(PrintWriter out, Field f,
Object obj);
protected abstract void outputFieldHeader(PrintWriter out,Object e);
protected abstract void outputFieldFooter(PrintWriter out,Object e);
在Eclipse 3.1中体验J2SE 5.0的新特性 第二部分 :注释类型(9)
时间:2011-04-02 IBM 邹青 吴嫣 吴疆
清单24 TXTExportableGenerator
public class TXTExportableGenerator exten
|