Example usage for javax.tools DiagnosticCollector getDiagnostics

List of usage examples for javax.tools DiagnosticCollector getDiagnostics

Introduction

In this page you can find the example usage for javax.tools DiagnosticCollector getDiagnostics.

Prototype

public List<Diagnostic<? extends S>> getDiagnostics() 

Source Link

Document

Returns a list view of diagnostics collected by this object.

Usage

From source file:Main.java

public static void main(String[] args) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager sjfm = compiler.getStandardFileManager(dc, null, null);
    Iterable<? extends JavaFileObject> fileObjects = sjfm.getJavaFileObjects(args);
    compiler.getTask(null, sjfm, dc, null, null, fileObjects).call();
    for (Diagnostic d : dc.getDiagnostics()) {
        System.out.println(d.getMessage(null));
        System.out.printf("Line number = %d\n", d.getLineNumber());
        System.out.printf("File = %s\n", d.getSource());
    }/*from   w w w  . j a v  a 2s.c  o  m*/
}

From source file:CompileFiles2.java

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

    StandardJavaFileManager sjfm;
    sjfm = compiler.getStandardFileManager(dc, null, null);

    Iterable<? extends JavaFileObject> fileObjects;
    fileObjects = sjfm.getJavaFileObjects(args);

    compiler.getTask(null, sjfm, dc, null, null, fileObjects).call();

    for (Diagnostic d : dc.getDiagnostics()) {
        System.out.println(d.getMessage(null));
        System.out.printf("Line number = %d\n", d.getLineNumber());
        System.out.printf("File = %s\n", d.getSource());
    }/* w ww .j a v a2  s  .  c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    task.call();/*www .  j a  v a 2 s . c  om*/
    fileManager.close();
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

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  ww .j a  v  a 2s.  co  m

    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:DiagnosticDemo.java

public static void main(String[] args) {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager =

            compiler.getStandardFileManager(diagnostics);

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    task.run();/*ww  w .ja v a2 s  .c om*/
    try {
        fileManager.close();
    } catch (IOException e) {
    }
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

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  w w  w. ja v a2s.  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");
        }//from   w  w  w .  j  a v a 2s .c  o m
    }
    return builder.toString();
}

From source file:de.alpharogroup.runtime.compiler.CompilerExtensions.java

/**
 * Generate a compilation stacktrace from the given parameter.
 *
 * @param diagnosticCollectors//from   w w w.  ja v a  2 s . co  m
 *            the diagnostic collectors
 * @return the generated stacktrace string.
 */
public static String generateCompilationStacktrace(
        final DiagnosticCollector<JavaFileObject> diagnosticCollectors) {
    final StringBuilder sb = new StringBuilder();
    for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollectors.getDiagnostics()) {
        sb.append(diagnostic.getMessage(null));
        sb.append(SeparatorConstants.SEMI_COLON_WHITE_SPACE);
    }
    return sb.toString();
}

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 a v  a 2s  . 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.github.pellaton.jazoon2012.AnnotationProcessorTestCompiler.java

/**
 * Processes the java class specified. This implementation only parses and processes the java classes and does not
 * fully compile them - i.e. it does not write class files back to the disk. Basically, {@code javac} is called with
 * {@code -proc:only}.//  w  w w . java 2 s  .c  o m
 *
 * @param classToCompile the Java class to compile
 * @param processor the annotation {@link Processor} to use during compilation
 * @return a list of {@link Diagnostic} messages emitted during the compilation
 */
public static List<Diagnostic<? extends JavaFileObject>> compileClass(String classToCompile,
        Processor processor) throws IOException {

    DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();

    StandardJavaFileManager fileManager = null;
    try {
        fileManager = getFileManager(collector);
        Iterable<? extends JavaFileObject> compilationUnits = getCompilationUnitOfClass(fileManager,
                classToCompile);

        CompilationTask task = COMPILER.getTask(null, fileManager, collector, COMPILER_OPTIONS, null,
                compilationUnits);
        task.setProcessors(Arrays.asList(processor));
        task.call();

        return collector.getDiagnostics();
    } finally {
        if (fileManager != null) {
            fileManager.close();
        }
    }
}