2013-05-16 39 views
0

有人知道是否有用于java的在线代码分析器。我希望能够检查一些小部分的代码。在线Java代码分析器

例如:该方法具有这样的警告:(空解引用)

private XMLGregorianCalendar asXMLGregorianCalendar(Date data) { 
      DatatypeFactory dateformat = null; 
      try { 
      dateformat = DatatypeFactory.newInstance(); 
      } catch (MyException e) { 
       /// 
      } 
      if (data == null) { 
      return null; 
      } else { 
      GregorianCalendar gregorianCal = new GregorianCalendar(); 
      gregorianCal.setTimeInMillis(data.getTime()); 
      return dateformat.newXMLGregorianCalendar(gregorianCal); 
    } 
} 

我的新版本是:

private XMLGregorianCalendar asXMLGregorianCalendar(Date data) throws ComponentBlockingException { 
     if (data == null) { 
      return null; 
     } 
     DatatypeFactory dateformat = null; 
     try { 
      dateformat = DatatypeFactory.newInstance(); 
     } catch (MyException e) { 
      //// 
     } 
     GregorianCalendar gregorianCal = new GregorianCalendar(); 
     gregorianCal.setTimeInMillis(data.getTime()); 
     return dateformat.newXMLGregorianCalendar(gregorianCal); 

    } 

} 

我认为第二种方式应该没问题。

+3

我不知道在线工具,但最新的IDE应该涵盖这种类型的用例。 – assylias

+1

也许ide一个[ideone.com](http://ideone.com/)? – mleczey

+3

[FindBugs](http://findbugs.sourceforge.net/)不在线,但可以对这些事情有帮助,并且有一个体面的Eclipse插件。它还集成了Maven,Idea和NetBeans。 –

回答

4

我不确定任何可用的在线代码anlayzer工具,但让我试着帮助您进行代码分析。

如果由于某种原因,以下try块击中异常

try { 
     dateformat = DatatypeFactory.newInstance(); 
    } 

那么你的日期格式将保持为空。所以以下声明

return dateformat.newXMLGregorianCalendar(gregorianCal); 

很容易出现空指针异常。因此我相信你会得到静态代码anlayzer错误。

您必须确保dateformat在所有情况下初始化或非null,然后代码到达您正在执行返回的行。

希望它有帮助!

+0

其他例外,比我抓住它,因为如果它是MyException,一切都应该是确定的,因为它永远不会到达'返回' –

+0

没有关键的是,如果发生异常,无论MyException或其他异常,然后dateformat将为空。所以在这种情况下,没有一点要求返回dateformat.newXMLGregorianCalendar(gregorianCal); 所以你应该抛出一个异常或者直接从方法中返回。 –

+0

@Juned,你正在做分析,而不是回答这个问题! –