sonar介绍
Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。总而言之,它可以告知你代码中的不规范和可能存在的bug
BUG级别
Blocker:阻断块。最严重的错误类型;
Critical:严重的错误类型;
Major:重要的错误类型;
Minor:次要的错误类型;
Info:一般信息;
Java规范总结
1.静态对象,用标准的静态块初始化
Noncompliant Code Example
class MyClass { private static final Map<String, String> MY_MAP = new HashMap<String, String>() { // Noncompliant - HashMap should be extended only to add behavior, not for initialization { put("a", "b"); } }; }
Compliant Solution
class MyClass { private static final Map<String, String> MY_MAP = new HashMap<String, String>(); static { MY_MAP.put("a", "b"); } }
2.尽量不要使用synchronized包装过的Java API
最好用没有synchronized的API ,多线程可以考虑用显示锁
ArrayList
orLinkedList
instead ofVector
Deque
instead ofStack
HashMap
instead ofHashtable
StringBuilder
instead ofStringBuffer
3.工具类加上私有的构造参数
对于工具类,一般是,static 对象的集合,一般不会实例化,java会对类默认加上无参的构造参数,所以显示的加上私有的构造参数
避免被实例化,抽象类也不应该有公共的构造方法。
Noncompliant Code Example
class StringUtils { // Noncompliant public static String concatenate(String s1, String s2) { return s1 + s2; } }
Compliant Solution
class StringUtils { // Compliant private StringUtils() { } public static String concatenate(String s1, String s2) { return s1 + s2; } }
4.不要新建一个对象只为了return
能直接return 就直接return
Noncompliant Code Example
public long computeDurationInMilliseconds() { long duration = (((hours * 60) + minutes) * 60 + seconds ) * 1000 ; return duration; } public void doSomething() { RuntimeException myException = new RuntimeException(); throw myException; }
Compliant Solution
public long computeDurationInMilliseconds() { return (((hours * 60) + minutes) * 60 + seconds ) * 1000 ; } public void doSomething() { throw new RuntimeException(); }
5.覆盖方法要加上@Override注解
这有两个好处
1.编辑器可以帮助检查是否覆盖正确,比如父类是否有这个方法,有可能拼写错了
2.增加代码可读性
6.数组的hashcode和toString
数组的hashCode和toString,他们基本上是无用的。hashCode返回数组的“身份散列码”,几乎和toString返回相同的值。他们的输出实际上没有反映数组的内容。相反,你应该将数组传递给相关的静态数组方法。
Noncompliant Code Example
public static void main( String[] args ) { String argStr = args.toString(); // Noncompliant int argHash = args.hashCode(); // Noncompliant
Compliant Solution
public static void main( String[] args ) { String argStr = Arrays.toString(args); int argHash = Arrays.hashCode(args);
7.不应该直接调用Thread.run() and Runnable.run()
Thread.run和Runnable.run()的目的都是来使代码在一个单独的、专用的线程执行。直接调用这些方法没有意义,因为它使他们的代码在当前线程执行。
为了得到预期效果,必须用Thread.start()方法
Noncompliant Code Example
Thread myThread = new Thread(runnable); myThread.run(); // Noncompliant
Compliant Solution
Thread myThread = new Thread(runnable); myThread.start(); // Compliant
8.字符串检查应该将字符创放在最左边
可以防止空指针异常
Noncompliant Code Example
System.out.println("Equal? " + myString.equals("foo")); // Noncompliant - will raise a NPE System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Noncompliant - null check could be removed
Compliant Solution
System.out.println("Equal?" + "foo".equals(myString)); // Compliant - properly deals with the null case
9.不应该抛出通用的异常
抛出通用的异常,会把正确的信息掩盖
Noncompliant Code Example
public void foo(String bar) throws Throwable { // Noncompliant throw new RuntimeException("My Message"); // Noncompliant }
Compliant Solution
public void foo(String bar) { throw new MyOwnRuntimeException("My Message"); }
10.定义集合
Java Collections API的目的是提供一个定义良好的接口来隐藏实现细节。实现类必须用来实例化新的集合,而一个实例化的结果应该被存储在一个变量,其类型是一个Java集合接口。
Noncompliant Code Example
public class Employees { private HashSet<Employee> employees = new HashSet<Employee>(); // Noncompliant - "employees" should have type "Set" rather than "HashSet" public HashSet<Employee> getEmployees() { // Noncompliant return employees; } }
Compliant Solution
public class Employees { private Set<Employee> employees = new HashSet<Employee>(); // Compliant public Set<Employee> getEmployees() { // Compliant return employees; } }