修改pmd总结

上传人:kms****20 文档编号:39588776 上传时间:2018-05-17 格式:DOC 页数:10 大小:251.50KB
返回 下载 相关 举报
修改pmd总结_第1页
第1页 / 共10页
修改pmd总结_第2页
第2页 / 共10页
修改pmd总结_第3页
第3页 / 共10页
修改pmd总结_第4页
第4页 / 共10页
修改pmd总结_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《修改pmd总结》由会员分享,可在线阅读,更多相关《修改pmd总结(10页珍藏版)》请在金锄头文库上搜索。

1、PMD 报错原因修改总结报错原因修改总结1. Avoid unnecessary Comparisons in Boolean exception.(Boolean 类型重复判断)错误 例子: if(null! = a 正确 List list=new ArrayList(); 错误 例子: private static HashMap map=new HashMap();正确 private static Map map=new HashMap(); 错误 例子: private static LinkedHashMap map=new LinkedHashMap();正确 private

2、static Map map=new LinkedHashMap(); 3. Method names should not start with capital letters(方法名不能以大写开头)。错误 例子: public class Start() 正确 public class start() 可以用快捷键 Alt+shift+R 全部替4.varivable that are final and static should be in all caps.(定义的参数必须大写)错误 例子:public static final String root 正确 public stati

3、c final String ROOT5.Avoid appending charcutars as Strings in StringBuffer append (避免在 StringBuffer 里附加单个字符时附加成 String 类型) 错误 例子: buf.append(“)”)或者 buf.append(“a”) 正确 buf.append()或者 buf.append(a)6.use ArrayList instanded of vector(使用 ArrayList 替换 vector 后还是会报错,所以直接改成是以它的接口形式替换) 错误 例子: vector keys =

4、new vector(ELE); 正确 List keys = new ArrayList(ELE);7. Variables that are not final should not contain underscores (except for underscores in standard prefix/suffix) (变量不是 final 类型的不能包含下划线)错误 例子: private int DEAULT_PORT = 8001; 正确 private int DEAULTPORT = 8001;(调用它的所有类都需要改动)8. Variables should start

5、with a lowercase character(参数必须要以小写开始)错误 例子: private static int HANDLE_MAX = 200; 正确 private static int handleMax = 200; 9. Using equalsIgnoreCase() is cleaner than using toUpperCase/toLowerCase().equals().错误 例子: 正确10. Unnecessary wrapper object creation 错误 例子: intCurPage = Integer.valueOf(curPage).

6、intValue(); 正确 intCurPage = Integer.parseInt(curPage);11. This is an inefficient use of StringBuffer.toString; call StringBuffer.length instead. 错误 If(newPartLen+smsMOCommondArr.toString().getBytes(DB_CHARSET).length DB_VERCHAR_MAX_LEN + 1) 正确 if(newPartLen+(smsMOCommondArr.toString().getBytes(DB_CH

7、ARSET).length DB_VERCHAR_MAX_LEN + 1) 12. This final field could be made static错误 例子: private final String urlPrefix = “http:/“; 正确 private static final String URL_PREFIX = “http:/“;13. The field name indicates a constant but its modifiers do not错误 例子: private static String CONF_NAME = “version“; 正确

8、 private static final String CONF_NAME = “version“;14. System.out.print is used错误 例子: System.out.println(“PartalOneAppender-message=“+message+“); 正确 /System.out.println(“PartalOneAppender-message=“+message+“);15. Switch statements should have a default label错误 例子: switch (Type)case USER:displayType

9、= “USER“;break;case ADMIN:displayType = “ADMIN“;break; 正确 switch (Type)case USER:displayType = “USER“;break;case ADMIN:displayType = “ADMIN“;break;default:16. Substitute calls to size() = 0 (or size() != 0) with calls to isEmpty()错误 例子: if(null=Handlers | 0 =Handlers.size() 正确 if (null=Handlers | Ha

10、ndlers.isEmpty()17. Return an empty array rather than null.错误 例子: if (null != g System.arraycopy(g, 0, cloneGroups, 0, g.length);return cloneGroups;return null; 正确 if (null != g System.arraycopy(g, 0, cloneGroups, 0, g.length);return cloneGroups;return new String0;18. Deeply nested if.then statement

11、s are hard to read原因: 深嵌套的 if 循环很难读懂。 报错例子 if (null != portalScriptionInfo productName = chargeInfo.getProductName();break; 修改后的例子if (null != portalScriptionInfoproductName = chargeInfo.getProductName();break;19. Caught exception is rethrown, original stack trace may be lost原因: 捕捉一个异常后,再从新把异常扔出去,会把以

12、前的异常信息丢掉。 修改: 可以把在此扔出的异常信息以 Log 日志的形式打印出来。 例如: (错误) catch (IOException ioe) String msg = “Cant open config file: “ + xmlFile + “ due to: “+ ioe;throw new IOException(msg); 改正后:catch (IOException ioe)String msg = “Cant open config file: “ + xmlFile + “ due to: “+ ioe;LogFactory.getInstance().logActio

13、n(msg); 20.Avoid throwing raw Exception types(避免抛出一个生疏的异常类型)错误 例子: catch (IOException ioe)ioe.printStackTrace();String msg = “Cant open config file: “ + xmlFile.getAbsolutePath()+ “ due to: “ + ioe;throw new Exception(msg); 正确 catch (IOException ioe) ioe.printStackTrace();String msg = “Cant open con

14、fig file: “ + xmlFile.getAbsolutePath()+ “ due to: “ + ioe;LogFactory.getInstance().logAction(msg); 21. Method call on object which may be null错误 例子: if (GroupList = null public void stopServer() this.stopServer = true; 正确 可以把方法名称该换一个名字! 23. Do not use if statements that are always true or always fa

15、lse原因: 不要总使用 If 循环条件是 true 或者是 false。 修改: (错误的)String NameKey = request.getParameter(“NameKey“); if (true) request.setAttribute(“NameKey“, nameKey);ReleaseContent releaseContent = new ReleaseContent();releaseContent.setCatalogId(catalogId);User user= super.getUser(request);int userType = userInfo.getUserType(); (正确的)ift 条件是真的就可以去掉 if(true)String NameKey = request.getParameter(“NameKey“); /if (true) request.setAttribute(“NameKey“, nameKey);ReleaseContent releaseContent = new ReleaseContent();releaseContent.setCatalogId(catalogId);User user= super.getUser(request);in

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号