Java Code Examples for javax.tools.Diagnostic#getColumnNumber()

The following examples show how to use javax.tools.Diagnostic#getColumnNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrillDiagnosticListener.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  if (diagnostic.getKind() == javax.tools.Diagnostic.Kind.ERROR) {
    String message = diagnostic.toString() + " (" + diagnostic.getCode() + ")";
    logger.error(message);
    Location loc = new Location( //
        diagnostic.getSource().toString(), //
        (short) diagnostic.getLineNumber(), //
        (short) diagnostic.getColumnNumber() //
    );
    // Wrap the exception in a RuntimeException, because "report()"
    // does not declare checked exceptions.
    throw new RuntimeException(new CompileException(message, loc));
  } else if (logger.isTraceEnabled()) {
    logger.trace(diagnostic.toString() + " (" + diagnostic.getCode() + ")");
  }
}
 
Example 2
Source File: SpanUtil.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public static Spannable createSrcSpan(Resources resources, @NonNull Diagnostic diagnostic) {
    if (diagnostic.getSource() == null) {
        return new SpannableString("Unknown");
    }
    if (!(diagnostic.getSource() instanceof JavaFileObject)) {
        return new SpannableString(diagnostic.getSource().toString());
    }
    try {
        JavaFileObject source = (JavaFileObject) diagnostic.getSource();
        File file = new File(source.getName());
        String name = file.getName();
        String line = diagnostic.getLineNumber() + ":" + diagnostic.getColumnNumber();
        SpannableString span = new SpannableString(name + ":" + line);
        span.setSpan(new ForegroundColorSpan(resources.getColor(R.color.dark_color_diagnostic_file)),
                0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    } catch (Exception e) {

    }
    return new SpannableString(diagnostic.getSource().toString());
}
 
Example 3
Source File: CompilerHelper.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static String summarize(Diagnostic<? extends JavaFileObject> diagnostic) {
    StringBuilder sb = new StringBuilder();
    sb.append(diagnostic.getKind()).append(": ");
    JavaFileObject sourceObject = diagnostic.getSource();
    if (sourceObject != null) {
        sb.append("file ").append(sourceObject.toString()).append(" ");
    }
    String message = diagnostic.getMessage(Locale.ROOT);
    if (message != null && !message.isEmpty()) {
        sb.append(message).append(" ");
    }
    long line = diagnostic.getLineNumber();
    long column = diagnostic.getColumnNumber();
    if (line != -1 || column != -1) {
        sb.append("at line ").append(line).append(" column ").append(column);
    }
    return sb.toString().trim();
}
 
Example 4
Source File: DremioDiagnosticListener.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  if (diagnostic.getKind() == javax.tools.Diagnostic.Kind.ERROR) {
    String message = diagnostic.toString() + " (" + diagnostic.getCode() + ")";
    logger.error(message);
    Location loc = new Location( //
        diagnostic.getSource().toString(), //
        (short) diagnostic.getLineNumber(), //
        (short) diagnostic.getColumnNumber() //
    );
    // Wrap the exception in a RuntimeException, because "report()"
    // does not declare checked exceptions.
    throw new RuntimeException(new CompileException(message, loc));
  } else if (logger.isTraceEnabled()) {
    logger.trace(diagnostic.toString() + " (" + diagnostic.getCode() + ")");
  }
}
 
Example 5
Source File: DiagnosticPrettyPrinter.java    From buck with Apache License 2.0 6 votes vote down vote up
private static void appendContext(
    StringBuilder builder,
    Diagnostic<? extends JavaFileObject> diagnostic,
    @Nullable JavaFileObject source) {

  if (source == null) {
    return;
  }

  Optional<String> line = getLine(source, diagnostic.getLineNumber());
  if (line.isPresent()) {
    builder.append(line.get()).append(System.lineSeparator());
    for (long i = 1; i < diagnostic.getColumnNumber(); i++) {
      builder.append(" ");
    }
    builder.append("^");
  }
}
 
Example 6
Source File: JavacParser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String diagnosticToString(Diagnostic<JavaFileObject> d) {
    return d.getSource().toUri().toString() + ":" +
           d.getKind() + ":" +
           d.getStartPosition() + ":" +
           d.getPosition() + ":" +
           d.getEndPosition() + ":" +
           d.getLineNumber() + ":" +
           d.getColumnNumber() + ":" +
           d.getCode() + ":" +
           d.getMessage(null);
}
 
Example 7
Source File: PartialReparseTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public DiagnosticDescription(Diagnostic diag) {
    this.code = diag.getCode();
    this.message = diag.getMessage(Locale.ROOT);
    this.column = diag.getColumnNumber();
    this.endPos = diag.getEndPosition();
    this.kind = diag.getKind();
    this.line = diag.getLineNumber();
    this.pos = diag.getPosition();
    this.source = diag.getSource();
    this.startPos = diag.getStartPosition();
}
 
Example 8
Source File: HintTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void ensureCompilable(FileObject file) throws IOException, AssertionError, IllegalArgumentException {
    CompilationInfo info = parse(file);

    assertNotNull(info);

    for (Diagnostic d : info.getDiagnostics()) {
        if (d.getKind() == Diagnostic.Kind.ERROR)
            throw new AssertionError(d.getLineNumber() + ":" + d.getColumnNumber() + " " + d.getMessage(null));
    }
}
 
Example 9
Source File: HintTestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void ensureCompilable(FileObject file) throws IOException, AssertionError, IllegalArgumentException {
    CompilationInfo info = parse(file);

    assertNotNull(info);

    for (Diagnostic d : info.getDiagnostics()) {
        if (d.getKind() == Diagnostic.Kind.ERROR)
            throw new AssertionError(d.getLineNumber() + ":" + d.getColumnNumber() + " " + d.getMessage(null));
    }
}
 
Example 10
Source File: JavaErrorHandling.java    From Recaf with MIT License 5 votes vote down vote up
@Override
public void report(Diagnostic<? extends VirtualJavaFileObject> diagnostic) {
	// Skip non-errors
	if (diagnostic.getKind() != Diagnostic.Kind.ERROR)
		return;
	// Convert the diagnostic to location data
	// 0-index the line number
	int line = (int) diagnostic.getLineNumber() - 1;
	int column = (int) diagnostic.getColumnNumber();
	int literalStart = calculate(Position.pos(line + 1, column));
	// TODO: Properly fix this not fetching the correct section of text in weird cases
	String[] split = codeArea.getText().substring(literalStart).split("[^\\w.]+");
	int wordLength = split.length == 0 ? 1 : split[0].length();
	int to = column + wordLength;
	String msg = diagnostic.getMessage(Locale.ENGLISH);
	Diagnostic.Kind kind = diagnostic.getKind();
	switch(kind) {
		case ERROR:
		case WARNING:
		case MANDATORY_WARNING:
			Log.warn("Line {}, Col {}: {}", line, column, msg);
			break;
		case NOTE:
		case OTHER:
		default:
			Log.info("Line {}, Col {}: {}", line, column, msg);
			break;
	}
	getProblems().add(new Pair<>(line, msg));
	setProblems(getProblems());
	// Mark problem, 0-indexing the column
	markProblem(line, column - 1, to - 1, literalStart, msg);
	refreshProblemGraphics();
}
 
Example 11
Source File: JavaCustomIndexer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static void brokenPlatform(
        @NonNull final Context ctx,
        @NonNull final Iterable<? extends CompileTuple> files,
        @NullAllowed final Diagnostic<JavaFileObject> diagnostic) {
    if (diagnostic == null) {
        return;
    }
    final Diagnostic<JavaFileObject> error = new Diagnostic<JavaFileObject>() {

        @Override
        public Kind getKind() {
            return Kind.ERROR;
        }

        @Override
        public JavaFileObject getSource() {
            return diagnostic.getSource();
        }

        @Override
        public long getPosition() {
            return diagnostic.getPosition();
        }

        @Override
        public long getStartPosition() {
            return diagnostic.getStartPosition();
        }

        @Override
        public long getEndPosition() {
            return diagnostic.getEndPosition();
        }

        @Override
        public long getLineNumber() {
            return diagnostic.getLineNumber();
        }

        @Override
        public long getColumnNumber() {
            return diagnostic.getColumnNumber();
        }

        @Override
        public String getCode() {
            return diagnostic.getCode();
        }

        @Override
        public String getMessage(Locale locale) {
            return diagnostic.getMessage(locale);
        }
    };
    for (CompileTuple file : files) {
        if (!file.virtual) {
            ErrorsCache.setErrors(
                ctx.getRootURI(),
                file.indexable,
                Collections.<Diagnostic<JavaFileObject>>singleton(error),
                ERROR_CONVERTOR);
        }
    }
}
 
Example 12
Source File: CompileResult.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void store(StoreTransaction txn, Entity entity) {

  long now = Instant.now().getEpochSecond();
  entity.setProperty("createdAt", now);
  entity.setProperty("result", this.success);
  entity.setProperty("problems", this.diagnostics.size());

  for (Diagnostic<? extends JavaFileObject> diagnostic : this.diagnostics) {
    String kind = diagnostic.getKind().toString();
    long line = diagnostic.getLineNumber();
    long column = diagnostic.getColumnNumber();

    String message = diagnostic.getMessage(null);
    if (isNull(message)) {
      message = "";
    }
    JavaFileObject fileObject = diagnostic.getSource();
    String path = null;
    if (fileObject != null) {
      final URI uri = fileObject.toUri();
      final File file = new File(uri);
      try {
        path = file.getCanonicalPath();
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    }

    String code = diagnostic.getCode();
    Entity subEntity = txn.newEntity(CompileResult.DIAGNOSTIC_ENTITY_TYPE);
    subEntity.setProperty("kind", kind);
    subEntity.setProperty("line", line);
    subEntity.setProperty("column", column);
    subEntity.setProperty("message", message);
    if (nonNull(path)) {
      subEntity.setProperty("path", path);
    }
    if (nonNull(code)) {
      subEntity.setProperty("code", code);
    }

    entity.addLink("diagnostic", entity);
  }
}