在Eclipse 3.1体验J2SE 5.0新特性五(图)

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

清单19 Vendor的类定义

public class Vendor extends Employee {

@EXPortable String name;

@Exportable String company;

@Exportable String team;

@Exportable String workingHours;

String empNo;

public Vendor(String name, String company, String team, String hours) {

super();

this.name = name;

this.company = company;

this.team = team;

workingHours = hours;

}

}

清单20 Contractor的类定义

public class Contractor extends Employee{

@Exportable String name;

@Exportable String company;

@Exportable String contractDuration;

String empNo;

public Contractor(String name, String company) {

super();

// TODO Auto-generated constrUCtor stub

this.name = name;

this.company = company;

contractDuration ="1";

}

}

清单21 Supplemental的类定义

public class Contractor extends Employee{

@Exportable String name;

@Exportable String company;

@Exportable String contractDuration;

String empNo;

public Contractor(String name, String company) {

super();

this.name = name;

this.company = company;

contractDuration ="1";

}

}

清单22使用ExportableGenerator的程序

public class TestExportable {

public TestExportable() {

super();

}

public static void main(String[] args) {

Regular em=new Regular

("Catherine","IBM","Software Engineer","82888288","BJ", new Date());

Employee vn1=new Vendor("Steve","IBM","PVC","8");

Employee vn2=new Vendor("Steve","IBM","PVC","8");

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

清单24 TXTExportableGenerator

public class TXTExportableGenerator extends ExportableGenerator {

public TXTExportableGenerator(PrintWriter out) {

super(out);

}

@Override

protected void outputSimpleField(PrintWriter out, Field f,Object obj) {

out.print(f.getName());

out.print("=");

out.print(value(f,obj));

out.print(";");

out.println();

}

@Override

protected void outputFieldHeader(PrintWriter out,Object e) {

}

@Override

protected void outputFieldFooter(PrintWriter out,Object e) {

//out.println(e.getClass().getName()+":");

}

}

在这个例子中,我们将一个Employee对象的部分内容输出到文件C:\test.output中。图8显示了这个例子的输出结果。

图8 输出结果

通过这种方法,我们可以动态生成Employee对象的域输出,而不需要在程序中写明要输出哪些确定的域。假如需要更为丰富的格式,我们可以定义多个注释类型。通过对不同注释以及属性的解析,实现格式化的文件输出。

2.4注释类型的小结

所谓元数据,指的是关于信息的信息。一般而言,代码分析工具,测试工具或者部署工具会使用元数据来产生配置信息以及使用配置信息产生控制逻辑。这些工具通常使用java的反射特性,重构元数据的信息,并对这些信息进行解释。

新的技术会不断改变程序设计和开发人员的设计思想。那么注释(Annotation)给我们带来了什么呢? 仅仅在代码分析,或者是开发测试框架和部署框架的时候才有用么?

我认为并不是这样。从上面的例子可以看出,注释(Annotation)的应用范围其实是很广泛的。在我们的应用中充分的利用元数据,可以提高的软件的质量和可维护性。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航