Example usage for javax.tools Diagnostic getLineNumber

List of usage examples for javax.tools Diagnostic getLineNumber

Introduction

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

Prototype

long getLineNumber();

Source Link

Document

Returns the line number of the character offset returned by #getPosition() .

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  .java 2 s .  c om
}

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  av  a  2s  .c  om*/
}

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  ww w.  java2s . com*/
    }
    return builder.toString();
}

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

private static void assertNotContains(List<Diagnostic<? extends JavaFileObject>> diagnostics, Kind kind,
        String errorMessage) {//  w w  w. j  ava2 s .com
    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));
    }
}

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>// ww  w.j a  v  a 2s  . c  o m
 * 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/>//from  ww w. jav  a 2  s .  c o  m
 * 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.//from w w w.  j av  a2  s  .  co 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.joliciel.talismane.utils.compiler.DynamicCompilerTest.java

@Test
public void testCompileError() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    DiagnositicsLogger diagnosticsLogger = new DiagnositicsLogger();

    DynamicCompiler compiler = new DynamicCompiler(classLoader, diagnosticsLogger);

    String src = "package com.joliciel.talismane.utils.compiler.foo;\n"
            + "import com.joliciel.talismane.utils.compiler.DynamicCompilerTest.HelloInterface;\n"
            + "public class Foo implements HelloInterface {\n" + "        public String hello(String who) {\n"
            + "            return \"Hello \" + who\n" + "        }\n" + "    }";

    String fullName = "com.joliciel.talismane.utils.compiler.foo.Foo";

    try {//from  w w w.ja v a 2 s .c  om
        compiler.compile(fullName, src, null);
        fail("Expected exception");
    } catch (DynamicCompilerException e) {
        LOG.debug(e.getMessage());
    }

    Diagnostic<? extends JavaFileObject> diagnostic = diagnosticsLogger.getDiagnostics().get(0);
    assertEquals(5, diagnostic.getLineNumber());
    assertEquals("compiler.err.expected", diagnostic.getCode());
    assertEquals("Foo.java", diagnostic.getSource().getName());
}

From source file:neembuu.uploader.zip.generator.NUCompiler.java

/**
 * Compile all the given files in the given build directory.
 *
 * @param files The array of files to compiles.
 * @param buildDirectory The build directory in which put all the compiled
 * files./*from w  ww .  j a v a  2 s .  co m*/
 * @throws FileNotFoundException
 * @throws IOException
 */
private void compileFiles(File[] files, File buildDirectory) throws FileNotFoundException, IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    buildDirectory.mkdir();

    /**/
    List<String> optionList = new ArrayList<String>();
    // set compiler's classpath to be same as the runtime's
    //optionList.addAll(Arrays.asList("-classpath", "C:\\neembuuuploader\\modules\\libs\\jsoup-1.7.2.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\commons-codec-1.6.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\commons-logging-1.1.1.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpclient-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpclient-cache-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpcore-4.2.4.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpmime-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\json-java.jar;C:\\neembuuuploader\\modules\\neembuu-uploader-utils\\build\\classes;C:\\neembuuuploader\\modules\\neembuu-uploader-api\\build\\classes;C:\\neembuuuploader\\modules\\neembuu-uploader-interfaces-abstractimpl\\build\\classes;C:\\neembuuuploader\\modules\\libs\\neembuu-now-api-ui.jar;C:\\neembuuuploader\\modules\\libs\\neembuu-release1-ui-mc.jar;C:\\neembuuuploader\\modules\\neembuu-uploader-uploaders\\build\\classes;C:\\neembuuuploader\\modules\\NeembuuUploader\\build\\classes"));
    optionList.addAll(Arrays.asList("-classpath", classPath));
    optionList.addAll(Arrays.asList("-d", buildDirectory.getAbsolutePath()));

    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(Arrays.asList(files));

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

    JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, optionList, null,
            compilationUnits);
    boolean result = task.call();

    if (result) {
        System.out.println("Compilation was successful");
    } else {
        System.out.println("Compilation failed");

        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            System.out.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
        }
    }
}

From source file:com.qwazr.compiler.JavaCompiler.java

private void compile(javax.tools.JavaCompiler compiler, Collection<File> javaFiles) throws IOException {
    final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    try {// w  w  w.  j ava2  s . c  o m
        Iterable<? extends JavaFileObject> sourceFileObjects = fileManager
                .getJavaFileObjectsFromFiles(javaFiles);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        final List<String> options = new ArrayList<String>();
        if (classPath != null) {
            options.add("-classpath");
            options.add(classPath);
        }
        options.add("-d");
        options.add(javaClassesDirectory.getAbsolutePath());
        options.add("-sourcepath");
        options.add(javaSourceDirectory.getAbsolutePath());
        javax.tools.JavaCompiler.CompilationTask task = compiler.getTask(pw, fileManager, diagnostics, options,
                null, sourceFileObjects);
        if (!task.call()) {
            for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics())
                pw.format("Error on line %d in %s%n%s%n", diagnostic.getLineNumber(),
                        diagnostic.getSource().toUri(), diagnostic.getMessage(null));
            pw.flush();
            pw.close();
            sw.close();
            throw new IOException(sw.toString());
        }
    } finally {
        IOUtils.close(fileManager);
    }
}