CopyRight owned by the original author.--(www.MegaEntry.com)
简而言之,通过优化import的设置可以达到以下的目的: 去除无用的import语句:如在类中没有使用任何包中的类,则这个包的import语句可以删除。 设置包的阈值:当前类引用包中类的数目大于这个阈值时,引入整个包(如import java.io.*),否则为包中每个被引用的类单独指定的一个import语句(如import java.io.File)。 设置包的排列顺序:按照一般的习惯,按包的常用程度从高到低进行排列,常用的包放在前面引入。一般情况下,JDK经典的包放在最前面(以java.为前缀),JDK扩展包紧跟其后(以javax.为前缀),接着是第三方类库包(如org.apache.*),再次是自己开发的公用类库,最后才是工程中的其他类。 通过Project->Project Properties...->Java Formating->在Imports设置页中切换到Threshold标
图 25 设置包阈值对话框 |
MegaEntry - Social networking and discussion site!
在Imports设置页中切换到Sort Order标签页,在此指定import代码段的包引入顺序及格式。假设myrefactor.jpx工程中有一个myrefactor.sub1的子包,我们通过以下步骤将其置为import引入代码段的最后,并在前面添加一个空行: 1) 点击Add blank line在列表中添加一个<blank line>,表示在import代码段中添加一个空行。 2) 点击Add prefix...在弹出的Add Prefix对话框中选择myrefactor.sub1包。 3) 点击OK保存设置。
图 26 import代码段样式设置对话框 |
CopyRight owned by the original author.--(www.MegaEntry.com)
自动添加异常捕捉功能,从严格意义上说并不属性代码重构的范畴,因为编译期的异常是一定要有捕捉代码的,否则程序无法通过编译。但在某些情况下,自动添加异常捕捉就是地地道道的代码重构了,请看下面的代码: 代码清单 “面向编译”的异常捕捉| 1. public void autoCatch()2. {3. try {4. FileInputStream fis = null ;5. byte[] bArr = new byte[1024] ;6. //会抛出FileNotFoundException7. fis = new FileInputStream( "D:1.txt ") ;8. //会抛出IOException9. fis.read(bArr) ;10. }11. catch(Exception ex) { CopyRight owned by the original author.--(www.MegaEntry.com) 12. }13. } |
| 1. public void autoCatch()2. {3. FileInputStream fis = null ;4. byte[] bArr = new byte[1024] ;5. try {6. //会抛出FileNotFoundException7. fis = new FileInputStream( "D:1.txt ") ;8. //会抛出IOException MegaEntry - Social networking and discussion site! 9. fis.read(bArr) ;10. }11. catch(FileNotFoundException ex) {12. System.out.println( "D:1.txt文件不存在,请检查 ") ;13. }14. catch(IOException ex) {15. System.out.println( "D:1.txt文件读写发生异常,异常信息为: " + 16. ex.getMessage()) ;17. }18. finally {19. if(fis != null) {20. try {21. fis.close() ;22. }23. catch(IOException ex1) {24. System.out.println( "关闭文件输入流的时候发生异常,异常信息为:CopyRight owned by the original author.--(www.MegaEntry.com) 25. " + ex1.getMessage()) ;26. }27. }28. }29. } |
| 1. public void autoCatch()2. {3. FileInputStream fis = null ;4. byte[] bArr = new byte[1024] ;5. try {6. //会抛出FileNotFoundException7. fis = new FileInputStream( "D:1.txt ") ; CopyRight owned by the original author.--(www.MegaEntry.com) 8. //会抛出IOException9. fis.read(bArr) ;10. }11. catch(FileNotFoundException ex) {12. }13. catch(IOException ex) {14. }15. } |
MegaEntry - Social networking and discussion site!
代码清单 18 Struts有关formBean的部署描述文件| 1. <struts-config>2. <form-beans>3. <form-bean name= "untitled1ActionForm " type= "myrefactor.Untitled1ActionForm " />4. </form-beans>5. </struts-config> |
图 28 在部署描述文件中重构类名的对话框 |
CopyRight owned by the original author.--(www.MegaEntry.com)
因为EJB设计器中知道一个EJB所有引用的类和接口,所以必须通过EJB设计器对EJB类进行更名。如果在编辑器或UML浏览器中对EJB文件进行重命名重构,你将看到以下的警告信息:| WARNING: You are refactoring an EJB file. This may requirethat you change some source code and the deploymentdescriptor by hand. We recommend using the EJB designer formost refactoring scenarios. |
| A aObj = new A(); CopyRight owned by the original author.--(www.MegaEntry.com) aObj.bObj.foo(); |
| A aObj = new A();aObj.foo(); |
| 1. package myrefactor ;2. public class MultiAnimal3. {4. Cat cat = new Cat() ;5. Horse horse = new Horse() ; CopyRight owned by the original author.--(www.MegaEntry.com) 6. } |
图 29 代理方法 |
MegaEntry - Social networking and discussion site!
代码清单 20 生成成员类代理方法| 1. package myrefactor ;2. public class MultiAnimal3. {4. Cat cat = new Cat() ;5. Horse horse = new Horse() ;6. /**7. * Delegate method void howl() to cat : Cat8. */9. public void howl()10. {11. cat.howl() ;12. }13. } |
MegaEntry - Social networking and discussion site!
此外,Edit->Wizards下还提供了其他若干个有用的功能,它们分别是: ・Add:内部类、值域、构造函数、方法、Javadoc、代码模板。 ・Edit Fields/Properties:编辑值域/属性。 ・Edit Javadoc:编辑Javadoc。 ・Implement Interface:实现接口中的方法。 ・Override Methods:覆盖父类的方法。 但除Delegate to Member向导外,其他的向导使用的机会很少,你大可用其他的方法来完成这些功能。