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

The following examples show how to use javax.tools.Diagnostic#getKind() . 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: RetentionAnnoCombo.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private boolean getCompileResult(String contents, String className,
        boolean shouldCompile) throws Exception{

    DiagnosticCollector<JavaFileObject> diagnostics =
            new DiagnosticCollector<JavaFileObject>();
    boolean ok = compileCode(className, contents, diagnostics);

    String expectedErrKey = "compiler.err.invalid.repeatable" +
                                    ".annotation.retention";
    if (!shouldCompile && !ok) {
        for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
            if (!((d.getKind() == Diagnostic.Kind.ERROR) &&
                d.getCode().contains(expectedErrKey))) {
                error("FAIL: Incorrect error given, expected = "
                        + expectedErrKey + ", Actual = " + d.getCode()
                        + " for className = " + className, contents);
            }
        }
    }

    return (shouldCompile  == ok);
}
 
Example 2
Source File: Warn5.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.WARNING) {
            if (diagnostic.getCode().
                    contains("unsafe.use.varargs.param")) {
                setWarning(WarningKind.UNSAFE_BODY);
            } else if (diagnostic.getCode().
                    contains("redundant.trustme")) {
                setWarning(WarningKind.REDUNDANT_SAFEVARARGS);
            }
    } else if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING &&
            diagnostic.getCode().
                contains("varargs.non.reifiable.type")) {
        setWarning(WarningKind.UNSAFE_DECL);
    } else if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
            diagnostic.getCode().contains("invalid.trustme")) {
        setWarning(WarningKind.MALFORMED_SAFEVARARGS);
    }
}
 
Example 3
Source File: T7042566.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errDiags = errDiags
                .append(diagnostic.getMessage(Locale.getDefault()));
        errorFound = true;
    }
}
 
Example 4
Source File: TreePosTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    out.println(diagnostic);
    switch (diagnostic.getKind()) {
        case ERROR:
            errors++;
    }
}
 
Example 5
Source File: BasicSyntaxCombo.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void checkErrorKeys (
        String className, DiagnosticCollector<JavaFileObject> diagnostics) throws Exception {
    String expectedErrKey = "compiler.err.illegal.start.of.type";
    for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
        if ((d.getKind() == Diagnostic.Kind.ERROR) &&
            d.getCode().contains(expectedErrKey)) {
            break; // Found the expected error
        } else {
            error("Incorrect error key, expected = "
                  + expectedErrKey + ", Actual = " + d.getCode()
                  + " for className = " + className, srcContent);
        }
    }
}
 
Example 6
Source File: T6458823.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    }
    DiagnosticCollector<JavaFileObject> diagColl =
        new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    List<String> options = new ArrayList<String>();
    options.add("-processor");
    options.add("MyProcessor");
    options.add("-proc:only");
    List<File> files = new ArrayList<File>();
    files.add(new File(T6458823.class.getResource("TestClass.java").toURI()));
    final CompilationTask task = compiler.getTask(null, fm, diagColl,
        options, null, fm.getJavaFileObjectsFromFiles(files));
    task.call();
    int diagCount = 0;
    for (Diagnostic<? extends JavaFileObject> diag : diagColl.getDiagnostics()) {
        if (diag.getKind() != Diagnostic.Kind.WARNING) {
            throw new AssertionError("Only warnings expected");
        }
        System.out.println(diag);
        if (diag.getPosition() == Diagnostic.NOPOS) {
            throw new AssertionError("No position info in message");
        }
        if (diag.getSource() == null) {
            throw new AssertionError("No source info in message");
        }
        diagCount++;
    }
    if (diagCount != 2) {
        throw new AssertionError("unexpected number of warnings: " +
            diagCount + ", expected: 2");
    }
}
 
Example 7
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 8
Source File: TestGetElementReference.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
    StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source));
    Trees trees = Trees.instance(ct);
    CompilationUnitTree cut = ct.parse().iterator().next();

    ct.analyze();

    for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
        if (d.getKind() == Diagnostic.Kind.ERROR) {
            throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
        }
    }

    Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
    Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));

    while (m.find()) {
        TreePath tp = pathFor(trees, cut, m.start() - 1);
        Element found = trees.getElement(tp);
        String expected = m.group(1);
        String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";

        if (!expected.equals(actual)) {
            throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
        }
    }
}
 
Example 9
Source File: TestLambdaToMethodStats.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    try {
        if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
            switch (diagnostic.getCode()) {
                case "compiler.note.lambda.stat":
                    lambda = true;
                    break;
                case "compiler.note.mref.stat":
                    lambda = false;
                    bridge = false;
                    break;
                case "compiler.note.mref.stat.1":
                    lambda = false;
                    bridge = true;
                    break;
                default:
                    throw new AssertionError("unexpected note: " + diagnostic.getCode());
            }
            ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
                (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
            altMetafactory = (Boolean)dsu.d.getArgs()[0];
        }
    } catch (RuntimeException t) {
        t.printStackTrace();
        throw t;
    }
}
 
Example 10
Source File: GenericOverrideTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 11
Source File: SamConversionComboTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 12
Source File: IntersectionTypeParserTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 13
Source File: ParserTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 14
Source File: T6410706.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic d) {
    System.err.println(d);
    if (d.getKind() == Diagnostic.Kind.NOTE)
        notes++;
}
 
Example 15
Source File: FDTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 16
Source File: T6996914a.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    switch (diagnostic.getKind()) {
        case ERROR:
            errors++;
    }
}
 
Example 17
Source File: TestSelfRef.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
        errorFound = true;
    }
}
 
Example 18
Source File: CompletionFailure.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
            diagnostic.getCode().contains("compiler.err.cant.access")) {
        errorFound = true;
    }
}
 
Example 19
Source File: DeprecatedAnnoCombo.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void runTest() throws Exception {
    boolean ok = false;
    int testCtr = 0;

    for (TestCases clName : TestCases.values()) {
        testCtr++;

        // Create test source content
        String contents = getContent(clName.toString());

        // Compile the generated source file
        DiagnosticCollector<JavaFileObject> diagnostics =
                new DiagnosticCollector<JavaFileObject>();
        ok = compileCode(clName.toString(), contents, diagnostics);

        String errorKey1 = "compiler.note.deprecated.filename";
        String errorKey2 = "compiler.note.deprecated.recompile";
        List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics();

        //Check for deprecated warnings
        if (ok) {
            if (diags.size() == 0) {
                error("Did not get any warnings for @Deprecated usage");
            } else {
                for (Diagnostic<?> d : diags) {
                    if (d.getKind() == Diagnostic.Kind.NOTE) {
                        if (d.getCode().contains(errorKey1)
                            || d.getCode().contains(errorKey2)) {
                            System.out.println("TestCase =" + clName + " passed as expected");
                        } else {
                            error("TestCase =" + clName + " did not give correct warnings" +
                                "Expected warning keys: " +
                                "errorKey1 = " + errorKey1 +
                                "errorKey2 = " + errorKey2 +
                                "actualErrorKey = " + d.getCode(), contents);
                        }
                    } else {
                        error("Diagnostic Kind is incorrect, expected = " +
                             Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents);
                    }
                }
            }
        } else {
            error("TestCase =" + clName + " did not compile as expected", contents);
        }
    }

    System.out.println("Total number of tests run: " + testCtr);
    System.out.println("Total number of errors: " + errors);
    if (errors > 0)
        throw new Exception(errors + " errors found");
}
 
Example 20
Source File: DeprecatedAnnoCombo.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void runTest() throws Exception {
    boolean ok = false;
    int testCtr = 0;

    for (TestCases clName : TestCases.values()) {
        testCtr++;

        // Create test source content
        String contents = getContent(clName.toString());

        // Compile the generated source file
        DiagnosticCollector<JavaFileObject> diagnostics =
                new DiagnosticCollector<JavaFileObject>();
        ok = compileCode(clName.toString(), contents, diagnostics);

        String errorKey1 = "compiler.note.deprecated.filename";
        String errorKey2 = "compiler.note.deprecated.recompile";
        List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics();

        //Check for deprecated warnings
        if (ok) {
            if (diags.size() == 0) {
                error("Did not get any warnings for @Deprecated usage");
            } else {
                for (Diagnostic<?> d : diags) {
                    if (d.getKind() == Diagnostic.Kind.NOTE) {
                        if (d.getCode().contains(errorKey1)
                            || d.getCode().contains(errorKey2)) {
                            System.out.println("TestCase =" + clName + " passed as expected");
                        } else {
                            error("TestCase =" + clName + " did not give correct warnings" +
                                "Expected warning keys: " +
                                "errorKey1 = " + errorKey1 +
                                "errorKey2 = " + errorKey2 +
                                "actualErrorKey = " + d.getCode(), contents);
                        }
                    } else {
                        error("Diagnostic Kind is incorrect, expected = " +
                             Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents);
                    }
                }
            }
        } else {
            error("TestCase =" + clName + " did not compile as expected", contents);
        }
    }

    System.out.println("Total number of tests run: " + testCtr);
    System.out.println("Total number of errors: " + errors);
    if (errors > 0)
        throw new Exception(errors + " errors found");
}