Example #1
0
  @NotNull
  private static String errorsToString(
      @NotNull DiagnosticCollector<JavaFileObject> diagnosticCollector, boolean humanReadable) {
    StringBuilder builder = new StringBuilder();
    for (javax.tools.Diagnostic<? extends JavaFileObject> diagnostic :
        diagnosticCollector.getDiagnostics()) {
      if (diagnostic.getKind() != javax.tools.Diagnostic.Kind.ERROR) continue;

      if (humanReadable) {
        builder.append(diagnostic).append("\n");
      } else {
        builder
            .append(diagnostic.getSource().getName())
            .append(":")
            .append(diagnostic.getLineNumber())
            .append(":")
            .append(diagnostic.getColumnNumber())
            .append(":")
            .append(diagnostic.getCode())
            .append("\n");
      }
    }
    return builder.toString();
  }
Example #2
0
  private void markError(
      DiagnosticCollector<JavaFileObject> diagnosticsCollector, boolean markerCreation) {

    for (Diagnostic diagnostic : diagnosticsCollector.getDiagnostics()) {
      System.out.format("Error on line %d" + " -> ", diagnostic.getLineNumber(), diagnostic);
      System.out.println(diagnostic.getMessage(null) + "\n");
      System.err.println("*** " + diagnostic.toString() + " *** " + diagnostic.getCode());
      JavaFileObject source = (JavaFileObject) diagnostic.getSource();
      String longFileName = source == null ? null : source.toUri().getPath();
      // String shortFileName = source == null ? null : source.getName();

      // System.out.println("Error in: " + longFileName);
      // Path path = new Path(longFileName);
      // IFile ifile =
      // ResourcesPlugin.getWorkspace().getRoot().getFile(path);
      if (diagnostic.getLineNumber() > -1) {
        File fileToOpen = new File(longFileName);

        if (fileToOpen.exists() && fileToOpen.isFile()) {
          final IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());

          Display display = PlatformUI.getWorkbench().getDisplay();
          display.syncExec(
              new Runnable() {

                public void run() {

                  try {
                    IEditorPart part = IDE.openEditorOnFileStore(pag, fileStore);

                    if (part != null) {
                      IEditorInput input = part.getEditorInput();
                      ifile = ((IFileEditorInput) input).getFile();

                      // ... use activeProjectName
                    }

                  } catch (PartInitException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  }
                }
              });

        } else {
          // Do something if the file does not exist
        }
      }

      // System.out.format("Error on line %d in %s"+"\n",
      // diagnostic.getLineNumber(), diagnostic);
      if (markerCreation == true) {
        int lnr = (int) diagnostic.getLineNumber();
        int startPos = (int) diagnostic.getStartPosition();
        int stopPos = (int) diagnostic.getEndPosition();
        if (ifile != null) {
          IMarker marker;
          try {
            marker = ifile.createMarker(IMarker.PROBLEM);
            marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
            marker.setAttribute(IMarker.MESSAGE, diagnostic.getMessage(null));
            marker.setAttribute(IMarker.LINE_NUMBER, lnr);
            // if (pos.offset != 0) {
            // System.out.println(startPos);
            // System.out.println(stopPos);

            marker.setAttribute(IMarker.CHAR_START, startPos);
            marker.setAttribute(IMarker.CHAR_END, stopPos - 1);
          } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    }
  }