Example usage for javax.tools Diagnostic getKind

List of usage examples for javax.tools Diagnostic getKind

Introduction

In this page you can find the example usage for javax.tools Diagnostic getKind.

Prototype

Kind getKind();

Source Link

Document

Returns the kind of this diagnostic, for example, error or warning.

Usage

From source file:Main.java

public static void main(String args[]) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromStrings(Arrays.asList("YourFile.java"));
    Iterable<String> options = Arrays.asList("-d", "classes", "-sourcepath", "src");
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null,
            compilationUnits);//from w w w .  ja va 2 s .  c  om

    boolean success = task.call();
    for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
        System.out.println(diagnostic.getCode());
        System.out.println(diagnostic.getKind());
        System.out.println(diagnostic.getPosition());
        System.out.println(diagnostic.getStartPosition());
        System.out.println(diagnostic.getEndPosition());
        System.out.println(diagnostic.getSource());
        System.out.println(diagnostic.getMessage(null));
    }
    fileManager.close();
    System.out.println("Success: " + success);
}

From source file:CompileSourceInMemory.java

public static void main(String args[]) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    StringWriter writer = new StringWriter();
    PrintWriter out = new PrintWriter(writer);
    out.println("public class HelloWorld {");
    out.println("  public static void main(String args[]) {");
    out.println("    System.out.println(\"This is in another java file\");");
    out.println("  }");
    out.println("}");
    out.close();/*from   www.  j a  v  a 2 s .c o m*/
    JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString());

    Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
    CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);

    boolean success = task.call();
    for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
        System.out.println(diagnostic.getCode());
        System.out.println(diagnostic.getKind());
        System.out.println(diagnostic.getPosition());
        System.out.println(diagnostic.getStartPosition());
        System.out.println(diagnostic.getEndPosition());
        System.out.println(diagnostic.getSource());
        System.out.println(diagnostic.getMessage(null));

    }
    System.out.println("Success: " + success);

    if (success) {
        try {
            Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class }).invoke(null,
                    new Object[] { null });
        } catch (ClassNotFoundException e) {
            System.err.println("Class not found: " + e);
        } catch (NoSuchMethodException e) {
            System.err.println("No such method: " + e);
        } catch (IllegalAccessException e) {
            System.err.println("Illegal access: " + e);
        } catch (InvocationTargetException e) {
            System.err.println("Invocation target: " + e);
        }
    }
}

From source file:Main.java

private static String errorsToString(DiagnosticCollector<JavaFileObject> diagnosticCollector) {
    StringBuilder builder = new StringBuilder();
    for (javax.tools.Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
        if (diagnostic.getKind() == javax.tools.Diagnostic.Kind.ERROR) {
            builder.append(diagnostic.getSource().getName()).append(":").append(diagnostic.getLineNumber())
                    .append(":").append(diagnostic.getColumnNumber()).append(":").append(diagnostic.getCode())
                    .append("\n");
        }// w  w w  .j  av  a2s  .  c o m
    }
    return builder.toString();
}

From source file:nl.elucidator.patterns.builder.annotations.processor.AbstractAnnotationProcessorTest.java

/**
 * Asserts that the compilation produced no errors, i.e. no diagnostics of
 * type {@link Kind#ERROR}./*from w w  w  . ja v a  2 s .c o m*/
 *
 * @param diagnostics the result of the compilation
 * @see #assertCompilationReturned(Kind, long, List)
 * @see #assertCompilationReturned(Kind[], long[], List)
 */
protected static void assertCompilationSuccessful(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
    assert (diagnostics != null);

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
        assertFalse("Expected no errors", diagnostic.getKind().equals(Kind.ERROR));
    }

}

From source file:hydrograph.ui.expression.editor.util.ExpressionEditorUtil.java

/**
 * This method validates the given expression and updates the expression-editor's datasturcture accordingly
 * /*from w w  w .  j ava2s  . co  m*/
 * @param expressionText
 * @param inputFields
 * @param expressionEditorData
 */
public static void validateExpression(String expressionText, Map<String, Class<?>> inputFields,
        ExpressionEditorData expressionEditorData) {
    if (BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject() != null) {
        DiagnosticCollector<JavaFileObject> diagnosticCollector = null;
        try {
            inputFields.putAll(expressionEditorData.getExtraFieldDatatypeMap());
            diagnosticCollector = ValidateExpressionToolButton.compileExpresion(expressionText, inputFields,
                    expressionEditorData.getComponentName());
            if (diagnosticCollector != null && !diagnosticCollector.getDiagnostics().isEmpty()) {
                for (Diagnostic<?> diagnostic : diagnosticCollector.getDiagnostics()) {
                    if (StringUtils.equals(diagnostic.getKind().name(), Diagnostic.Kind.ERROR.name())) {
                        expressionEditorData.setValid(false);
                        return;
                    }
                }
            }
        } catch (JavaModelException | InvocationTargetException | ClassNotFoundException | MalformedURLException
                | IllegalAccessException | IllegalArgumentException e) {
            expressionEditorData.setValid(false);
            return;
        }
        expressionEditorData.setValid(true);
    }
}

From source file:com.qrmedia.commons.test.annotation.processing.AbstractAnnotationProcessorTest.java

/**
 * Asserts that the compilation produced a result of the following
 * {@link Kind} at the given line number.
 * <p>//  w w  w . j  a v  a  2 s  .  c  om
 * Does not check that this is the <em>only</em> diagnostic kind returned!
 * 
 * @param expectedDiagnosticKind
 *            the kind of diagnostic expected
 * @param expectedLineNumber
 *            the line number at which the diagnostic is expected
 * @param diagnostics
 *            the result of the compilation
 * @see #assertCompilationSuccessful(List)
 * @see #assertCompilationReturned(Kind[], long[], List)
 */
protected static void assertCompilationReturned(Kind expectedDiagnosticKind, long expectedLineNumber,
        List<Diagnostic<? extends JavaFileObject>> diagnostics) {
    assert ((expectedDiagnosticKind != null) && (diagnostics != null));
    boolean expectedDiagnosticFound = false;

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {

        if (diagnostic.getKind().equals(expectedDiagnosticKind)
                && (diagnostic.getLineNumber() == expectedLineNumber)) {
            expectedDiagnosticFound = true;
        }

    }

    assertTrue("Expected a result of kind " + expectedDiagnosticKind + " at line " + expectedLineNumber,
            expectedDiagnosticFound);
}

From source file:nl.elucidator.patterns.builder.annotations.processor.AbstractAnnotationProcessorTest.java

/**
 * Asserts that the compilation produced a result of the following
 * {@link Kind} at the given line number.
 * <p/>/*  ww w  . j  a v a 2 s .c  om*/
 * Does not check that this is the <em>only</em> diagnostic kind returned!
 *
 * @param expectedDiagnosticKind the kind of diagnostic expected
 * @param expectedLineNumber     the line number at which the diagnostic is expected
 * @param diagnostics            the result of the compilation
 * @see #assertCompilationSuccessful(List)
 * @see #assertCompilationReturned(Kind[], long[], List)
 */
protected static void assertCompilationReturned(Kind expectedDiagnosticKind, long expectedLineNumber,
        List<Diagnostic<? extends JavaFileObject>> diagnostics) {
    assert ((expectedDiagnosticKind != null) && (diagnostics != null));
    boolean expectedDiagnosticFound = false;

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
        System.out.println("diagnostic = " + diagnostic);
        if (diagnostic.getKind().equals(expectedDiagnosticKind)
                && (diagnostic.getLineNumber() == expectedLineNumber)) {
            expectedDiagnosticFound = true;
        }

    }

    assertTrue("Expected a result of kind " + expectedDiagnosticKind + " at line " + expectedLineNumber,
            expectedDiagnosticFound);
}

From source file:com.tojc.ormlite.android.compiler.AbstractAnnotationProcessorTest.java

/**
 * Asserts that the compilation produced a result of the following {@link Kind} at the given
 * line number.// w  w w  . j  av  a  2s  .  c  o  m
 * <p>
 * Does not check that this is the <em>only</em> diagnostic kind returned!
 * @param expectedDiagnosticKind
 *            the kind of diagnostic expected
 * @param expectedLineNumber
 *            the line number at which the diagnostic is expected
 * @param diagnostics
 *            the result of the compilation
 * @see #assertCompilationSuccessful(List)
 * @see #assertCompilationReturned(Kind[], long[], List)
 */
protected static void assertCompilationReturned(Kind expectedDiagnosticKind, long expectedLineNumber,
        List<Diagnostic<? extends JavaFileObject>> diagnostics) {
    assert expectedDiagnosticKind != null && diagnostics != null;
    boolean expectedDiagnosticFound = false;

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {

        if (diagnostic.getKind().equals(expectedDiagnosticKind)
                && diagnostic.getLineNumber() == expectedLineNumber) {
            expectedDiagnosticFound = true;
        }

    }

    assertTrue("Expected a result of kind " + expectedDiagnosticKind + " at line " + expectedLineNumber,
            expectedDiagnosticFound);
}

From source file:com.tojc.ormlite.android.compiler.AbstractAnnotationProcessorTest.java

/**
 * Asserts that the compilation produced no errors, i.e. no diagnostics of type
 * {@link Kind#ERROR}./*ww  w  .  j  a  va 2 s.c  o m*/
 * @param diagnostics
 *            the result of the compilation
 * @see #assertCompilationReturned(Kind, long, List)
 * @see #assertCompilationReturned(Kind[], long[], List)
 */
protected static void assertCompilationSuccessful(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
    assert diagnostics != null;

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
        assertFalse("Expected no errors", diagnostic.getKind().equals(Kind.ERROR));
    }

}

From source file:com.qrmedia.commons.test.annotation.processing.AbstractAnnotationProcessorTest.java

private static void assertNotContains(List<Diagnostic<? extends JavaFileObject>> diagnostics, Kind kind,
        String errorMessage) {/*from w  w w  .  java 2  s .c o  m*/
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
        assertFalse(String.format("%s, found on line %d: %s", errorMessage, diagnostic.getLineNumber(),
                diagnostic.getMessage(Locale.getDefault())), diagnostic.getKind().equals(kind));
    }
}