admin管理员组文章数量:1530341
刚来公司老大吩咐我做Code Quality. 需要做到所有的项目的Blocker 和 Critical 的数量为0.当时我觉得很难,因为我已经没有做个Java项目,所以面对这些Issue有点害怕,毕竟我的改动会造成整个项目的问题。当时就连一个空格老大都严格的要求我,格式有一点点的错误老大就回reject的提交的代码。。。。。。一把辛酸泪啊!想想刚来的时候神马Jekins,神马Jira,神马Sonar简直了!!!
我遇到Issue主要包括:
— 1.Badpractice 定义 equals 却没有定义 hashCode — 2.Dodgy 从实例方法里直接修改类变量,即 static 属性 — 3.Performance HashMap 用 entrySet iterator insead of keySet iterator — 4.Correctness 空 指针被引用,或者没有检查指针是否为 null 我依稀记得我当时做的第一个Blocker就是,项目中出现最多的问题 “Catch Exception instead of Throwable” 这是一个很普遍的错误。
catch (Exception t) {
Log.logError(getClass(),"Error Collecting Metrics", t);
}
catch(Throwablet){
Log.logError(getClass(),"Error Collecting Metrics", t);
}
原因: Throwable 包括了Exception 和 error ,catch 的只是Exception, error 是不需要的catch的,如果catch有可能造成很严重的后果。
— 2 . Use a logger to log this exception
` This exceptionwill often get logged in a separate file (System.err) making debugging more complex than itneeds
— 3 .“equals(Object obj )” should be overridden along with the “ compareTo (T obj )” method
“equals(Object obj)” and “hashCode()“ shouldbe overridden in pairs(BadPractice)
这两个问题其实很深奥,必须了解hashMap的存储机制,后续我会详细解释,记住一点hashCode相同equals不一定相同。
下面介绍的都是Critical的问题, Critical是
— 1.Method may fail to close stream
try {
InputStreaminputStream =null;
inputStream= new FileInputStream(downloadFile);
byte[]buffer = new byte[BUFFER_SIZE];
intbytesRead =-1;
while((bytesRead= inputStream.read(buffer))!= -1) {
output.write(buffer,0, bytesRead);
}
} catch(Exception e) {
thrownew WebApplicationException(e);
}
try (InputStreaminput = newFileInputStream(downloadFile)){
byte[]buffer = new byte[BUFFER_SIZE];
intbytesRead =-1;
while ((bytesRead= input.read(buffer))!= -1) {
output.write(buffer,0, bytesRead);
}
}catch (Exception e) {
throw new WebApplicationException(e);
}
如果把stream在try的括号里面初始化就可以保证stream最后一定会被close
if (OPEN_FILE_COUNT== 0.00 || CONFIG_VALUE == 0.00)
if (OPEN_FILE_COUNT== 0L || CONFIG_VALUE == 0L)
原因:计算机表示实数的精度问题
for(Integer index : this.clients.keySet()){
Zclientc = this.clients.get(index);
—Map<Integer,ZClient> mapSet= this.clients;
for (Map.Entry<Integer,ZClient>entry : mapSet.entrySet()){
Integerindex = entry.getKey();
ZClientc = entry.getValue();
原因:提高效率,因为EntrySet 会比keySet访问的次数减少一次。— 4.In J2EE , getClassLoader () might not work as expected. UseThread currentThread () getContextClassLoader () instead.
在动态加载资源的时候如果使用class.getClassLoader(),可能会导致和当前线程所运行的类和加载器不一致。
这个问题没有实质性的理解。。。。。。。
— 5.Synchronize this lazy initialization of ' streamInstance ‘
private staticStreamDatastreamInstance= null;
public static StreamDatagetStreamInstance(){
if (streamInstance== null) {
streamInstance= new StreamData();
}
return streamInstance;
}
privatestatic StreamDatastreamInstance= new StreamData();
publicstatic StreamDatagetStreamInstance(){
return streamInstance;
}
后续将在单例模式中详细的解释— 6 . Possible null pointer dereference of null ( Correctness )
if (dir.isDirectory()){
String[]children = dir.list();
for (Stringchild : children) {
return traverse(newFile(dir,child));
}
}
—if (dir.isDirectory()){
String[]children = dir.list();
if(children !=null){
for (Stringchild : children) {
return traverse(newFile(dir,child));
}
}
}
很简单不解释
— 7. Use"object == null" instead of " object.equals (null)" to test for nullity toprevent null pointer exceptions
if (null!= reqHead&& !reqHead.equals(null))
if (null!= reqHead)
— 8 . Returning ' ruleName ' may exposean internal array这代码使一个指向外部多个对象的引用指向了一个内部对象存储地址
privateString[] ruleName;
publicString[] getName() {
String[]temp = ruleName;
return temp;
}
public void setName(String[]name) {
String[] temp = name;
this.ruleName= temp;
}
— 9. Useless Operation On ImmutableString是一个不可变类,调用String上的任何操作都会返回新的String对象,虽然String是一个class,但实际上对它的任何操作都可以把它看成基本数据类型,比如s.trim方法是不会改变s值。
Stringtext = null;
while ((text = reader.readLine())!= null) {
lineNumber++;
text.trim();
text = text.trim();
— 10. Method invokes inefficient new String(String) constructor( Performance )Integer portNumber= null;
if (port!= null) {
portNumber= new Integer((int)port.intValue());}
portNumber= Integer.valueOf(port.intValue());
— 11. Callto equals() comparing different types(Correctness)if (disabled.equals(true)){
log.info("NucleonExtension Framework is disabled...");
return;
}
if (disabled.equals("true"))
— 12.BUG : Write to static field frominstance method — BUG 描述: This instance method writes to a staticfield. This is tricky to get correct if multiple instances are beingmanipulated, and generally bad practice. (实例方法直接写静态变量。) — 问题原因: sInstance 是类的静态成员变量,非静态 方法直接 对其赋值,非静态方法是与对象相关联的,当多个对象同时对该变量进行赋值时可能出现问题。— 解决方法:在使用静态成员变量时使用 get 和 set 方法。
本文标签: 发现项目JavabuildingCritical
版权声明:本文标题:Java 项目Building之后发现的Blocker和Critical 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726271491a1063883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论