2011-10-07 26 views
1

我有一個eclipse插件,它提供了一個菜單項,可以選擇該菜單項以在當前活動的文件上運行命令。如果當前活動文件有任何錯誤(如「問題」視圖中報告),我希望插件顯示一條警告消息,類似於當您嘗試運行帶有錯誤的Java項目時Eclipse的行爲。執行操作前檢查項目錯誤

回答

1

我知道這是一個老問題,但我找到了一個類似於所提出的解決方案。做你所描述的代碼是org.eclipse.debug.core.model.LaunchConfigurationDelegate。它檢查項目是否有錯誤,並在需要時顯示對話框。下面是相關的代碼,從Eclipse的月神:

/** 
* Returns whether the given project contains any problem markers of the 
* specified severity. 
* 
* @param proj the project to search 
* @return whether the given project contains any problems that should 
* stop it from launching 
* @throws CoreException if an error occurs while searching for 
* problem markers 
*/ 
protected boolean existsProblems(IProject proj) throws CoreException { 
    IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); 
    if (markers.length > 0) { 
     for (int i = 0; i < markers.length; i++) { 
      if (isLaunchProblem(markers[i])) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

/** 
* Returns whether the given problem should potentially abort the launch. 
* By default if the problem has an error severity, the problem is considered 
* a potential launch problem. Subclasses may override to specialize error 
* detection. 
* 
* @param problemMarker candidate problem 
* @return whether the given problem should potentially abort the launch 
* @throws CoreException if any exceptions occur while accessing marker attributes 
*/ 
protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException { 
    Integer severity = (Integer)problemMarker.getAttribute(IMarker.SEVERITY); 
    if (severity != null) { 
     return severity.intValue() >= IMarker.SEVERITY_ERROR; 
    } 

    return false; 
} 

相同的代碼可以在任何IResource,而不是一個IProject運行。

我設法很容易地找到它,方法是在顯示對話框時暫停調試器,並在相關類上設置斷點並從那裏追溯。

0

錯誤通常保存爲資源上的IMarkers(您的情況爲IFile),因此您可以在IFile中查詢您正在查找的標記。

您需要在查找之前知道標記的類型(通過調試並獲取所有當前標記,或者通過查看在文件的驗證過程中貢獻它們的代碼)。

希望有所幫助。

相關問題