开发者

How to detect eclipse compile errors (Java - JDT)

开发者 https://www.devze.com 2022-12-10 23:22 出处:网络
I need to detect compile errors in the java source code after a POST_CHANGE event (usually it is fired after saving changes in the java file). I\'m using IElementChangedListener for doing that. So, fo

I need to detect compile errors in the java source code after a POST_CHANGE event (usually it is fired after saving changes in the java file). I'm using IElementChangedListener for doing that. So, for detecting errors I tried the two possibilities bellow:

1:

boolean error = IMarker.SEVERITY_ERROR == iFile.findMaxProblemSeverity(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);

2:

ICompilationUnit unit = ..; // get some compilation unit

  // create requestor for accumulating discovered problems
  IProblemRequestor problemRequestor = new IProblemRequestor() {
    public void acceptProblem(IProblem problem) {
      System.out.println(problem.getID() + ": " + problem.getMessage());
    }
    public void beginReporting() {}
    public void endReporting() {}
    public boolean isActive() { return true; } // will detect problems if active
  };

  // use working copy to hold source with error
  unit.getWorkingCopy(new WorkingCopyOwner() {}, problemRequestor, null);

The first solution doesn't work because the errors that I can get is from a previous state, i.e., the state before saving. It doesn't reflect the current source code at the time and hence is not reliable.

The second solution works fine for most of the cases. After saving a java file, I can detect all errors existing in that file. However, if I execute a svn update, this solution is not able to detect errors after merging. Basically what I could find out is by the time I receive the event notification, the ICompilationUnit shows the my version of the file, instead showing the new merged one. What is weird is the IFile object has already all changes(the merged file), and even if I create a ICompilationUnit object from the IFile object, it seems to point to the representation of my version of ICompilationUnit.

Someone can开发者_StackOverflow give me any ideas about that?

Thanks, Tiago


There is one alternate way too. Add a IDocumentListener to your current java editor and whenever the document changes recreate your ICompilationUnit. I think it should work. Or you can also try to add a BufferListener via FileBuffers.getTextFileBufferManager().addFileBufferListener() so that you can have more control of when you need to update your ICompilationUnit.

0

精彩评论

暂无评论...
验证码 换一张
取 消