Java Code Examples for javax.tools.Diagnostic.Kind#WARNING

The following examples show how to use javax.tools.Diagnostic.Kind#WARNING . 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: ErrorUtil.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void parserDiagnostic(Diagnostic<? extends JavaFileObject> diagnostic) {
  Kind kind = diagnostic.getKind();
  if (kind == Kind.ERROR) {
    errorMessages.add(diagnostic.getMessage(null));
    errorCount++;
  } else if (kind == Kind.MANDATORY_WARNING || kind == Kind.WARNING) {
    warningMessages.add(diagnostic.getMessage(null));
    warningCount++;
  } else {
    return;
  }
  String msg;
  if (CLANG_STYLE_ERROR_MSG && diagnostic.getSource() != null) {
    msg = String.format("error: %s:%d: %s", diagnostic.getSource().getName(),
        diagnostic.getLineNumber(), diagnostic.getMessage(null).trim());
  } else {
    msg = diagnostic.toString().trim();
  }
  errorStream.println(msg);
}
 
Example 2
Source File: CompilerJavac.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
private MessageSeverity toSeverity(Diagnostic.Kind kind, boolean success) {
  // javac appears to report errors even when compilation was success.
  // I was only able to reproduce this with annotation processing on java 6
  // for consistency with forked mode, downgrade errors to warning here too
  if (success && kind == Kind.ERROR) {
    kind = Kind.WARNING;
  }

  MessageSeverity severity;
  switch (kind) {
    case ERROR:
      severity = MessageSeverity.ERROR;
      break;
    case NOTE:
      severity = MessageSeverity.INFO;
      break;
    default:
      severity = MessageSeverity.WARNING;
      break;
  }

  return severity;
}
 
Example 3
Source File: AnnotatedMixins.java    From Mixin with MIT License 5 votes vote down vote up
/**
 * Print a message to the AP messager
 */
@Override
public void printMessage(Kind kind, CharSequence msg, Element element, SuppressedBy suppressedBy) {
    if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) {
        this.processingEnv.getMessager().printMessage(kind, msg, element);
    }
}
 
Example 4
Source File: AnnotatedMixins.java    From Mixin with MIT License 5 votes vote down vote up
/**
 * Print a message to the AP messager
 */
@Override
public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, SuppressedBy suppressedBy) {
    if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) {
        this.processingEnv.getMessager().printMessage(kind, msg, element, annotation);
    }
}
 
Example 5
Source File: AnnotatedMixins.java    From Mixin with MIT License 5 votes vote down vote up
/**
 * Print a message to the AP messager
 */
@Override
public void printMessage(Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value,
        SuppressedBy suppressedBy) {
    if (kind != Kind.WARNING || !AnnotatedMixins.shouldSuppress(element, suppressedBy)) {
        this.processingEnv.getMessager().printMessage(kind, msg, element, annotation, value);
    }
}
 
Example 6
Source File: AnnotatedMixinElementHandlerOverwrite.java    From Mixin with MIT License 5 votes vote down vote up
public void registerOverwrite(AnnotatedElementOverwrite elem) {
    AliasedElementName name = new AliasedElementName(elem.getElement(), elem.getAnnotation());
    this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite", true, false);
    this.checkConstraints(elem.getElement(), elem.getAnnotation());
    
    if (elem.shouldRemap()) {
        for (TypeHandle target : this.mixin.getTargets()) {
            if (!this.registerOverwriteForTarget(elem, target)) {
                return;
            }
        }
    }
    
    if (!"true".equalsIgnoreCase(this.ap.getOption(SupportedOptions.DISABLE_OVERWRITE_CHECKER))) {
        Kind overwriteErrorKind = "error".equalsIgnoreCase(this.ap.getOption(SupportedOptions.OVERWRITE_ERROR_LEVEL))
                ? Kind.ERROR : Kind.WARNING;
        
        String javadoc = this.ap.getJavadocProvider().getJavadoc(elem.getElement());
        if (javadoc == null) {
            this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing javadoc comment", elem.getElement(), SuppressedBy.OVERWRITE);
            return;
        }
        
        if (!javadoc.toLowerCase(Locale.ROOT).contains("@author")) {
            this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @author tag", elem.getElement(), SuppressedBy.OVERWRITE);
        }
        
        if (!javadoc.toLowerCase(Locale.ROOT).contains("@reason")) {
            this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @reason tag", elem.getElement(), SuppressedBy.OVERWRITE);
        }
    }
}
 
Example 7
Source File: CompilationInfoImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Kind getKind() {
    return Kind.WARNING;
}
 
Example 8
Source File: Warn5.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void check(Result<?> res) {

        EnumSet<WarningKind> foundWarnings = EnumSet.noneOf(WarningKind.class);
        for (Diagnostic.Kind kind : new Kind[] { Kind.ERROR, Kind.MANDATORY_WARNING, Kind.WARNING}) {
            for (Diagnostic<? extends JavaFileObject> diag : res.diagnosticsForKind(kind)) {
                for (WarningKind wk : WarningKind.values()) {
                    if (wk.code.equals(diag.getCode())) {
                        foundWarnings.add(wk);
                    }
                }
            }
        }

        EnumSet<WarningKind> expectedWarnings =
                EnumSet.noneOf(WarningKind.class);

        if (sourceLevel.compareTo(SourceLevel.JDK_7) >= 0 &&
                trustMe == TrustMe.TRUST &&
                suppressLevel != SuppressLevel.VARARGS &&
                xlint != XlintOption.NONE &&
                sig.isVarargs &&
                !sig.isReifiableArg &&
                body.hasAliasing &&
                (methKind == MethodKind.CONSTRUCTOR ||
                (methKind == MethodKind.METHOD &&
                 modKind == ModifierKind.FINAL || modKind == ModifierKind.STATIC ||
                 (modKind == ModifierKind.PRIVATE && sourceLevel.compareTo(SourceLevel.JDK_9) >= 0)))) {
            expectedWarnings.add(WarningKind.UNSAFE_BODY);
        }

        if (sourceLevel.compareTo(SourceLevel.JDK_7) >= 0 &&
                trustMe == TrustMe.DONT_TRUST &&
                sig.isVarargs &&
                !sig.isReifiableArg &&
                xlint == XlintOption.ALL) {
            expectedWarnings.add(WarningKind.UNSAFE_DECL);
        }

        if (sourceLevel.compareTo(SourceLevel.JDK_7) >= 0 &&
                trustMe == TrustMe.TRUST &&
                (!sig.isVarargs ||
                 ((modKind == ModifierKind.NONE ||
                 modKind == ModifierKind.PRIVATE && sourceLevel.compareTo(SourceLevel.JDK_9) < 0 ) &&
                 methKind == MethodKind.METHOD))) {
            expectedWarnings.add(WarningKind.MALFORMED_SAFEVARARGS);
        }

        if (sourceLevel.compareTo(SourceLevel.JDK_7) >= 0 &&
                trustMe == TrustMe.TRUST &&
                xlint != XlintOption.NONE &&
                suppressLevel != SuppressLevel.VARARGS &&
                (modKind == ModifierKind.FINAL || modKind == ModifierKind.STATIC ||
                 (modKind == ModifierKind.PRIVATE && sourceLevel.compareTo(SourceLevel.JDK_9) >= 0) ||
                 methKind == MethodKind.CONSTRUCTOR) &&
                sig.isVarargs &&
                sig.isReifiableArg) {
            expectedWarnings.add(WarningKind.REDUNDANT_SAFEVARARGS);
        }

        if (!expectedWarnings.containsAll(foundWarnings) ||
                !foundWarnings.containsAll(expectedWarnings)) {
            fail("invalid diagnostics for source:\n" +
                    res.compilationInfo() +
                    "\nOptions: " + xlint.getXlintOption() +
                    "\nSource Level: " + sourceLevel +
                    "\nExpected warnings: " + expectedWarnings +
                    "\nFound warnings: " + foundWarnings);
        }
    }
 
Example 9
Source File: CompilerJavacForked.java    From takari-lifecycle with Eclipse Public License 1.0 4 votes vote down vote up
private static void compile(final CompilerConfiguration config, final CompilerOutput output) {

    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
      output.addMessage(".", 0, 0, "No compiler is provided in this environment. " + "Perhaps you are running on a JRE rather than a JDK?", Kind.ERROR);
      return;
    }

    final Charset sourceEncoding = config.getSourceEncoding();
    final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
    final StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(diagnosticCollector, null, sourceEncoding);
    final Iterable<? extends JavaFileObject> fileObjects = standardFileManager.getJavaFileObjectsFromFiles(config.getSources());
    final Iterable<String> options = config.getCompilerOptions();
    final RecordingJavaFileManager recordingFileManager = new RecordingJavaFileManager(standardFileManager, sourceEncoding) {
      @Override
      protected void record(File inputFile, File outputFile) {
        output.processOutput(inputFile, outputFile);
      }
    };

    Writer stdout = new PrintWriter(System.out, true);
    final JavaCompiler.CompilationTask task = compiler.getTask(stdout, // Writer out
        recordingFileManager, // file manager
        diagnosticCollector, // diagnostic listener
        options, //
        null, // Iterable<String> classes to process by annotation processor(s)
        fileObjects);

    boolean success = task.call();

    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
      JavaFileObject source = diagnostic.getSource();

      // when doing annotation processing, javac 6 reports errors when handwritten sources
      // depend on generated sources even when overall compilation is reported as success
      // to prevent false build failures, never issue ERROR messages after successful compilation
      Kind kind = diagnostic.getKind();
      if (success && kind == Kind.ERROR) {
        kind = Kind.WARNING;
      }

      if (source != null) {
        File file = FileObjects.toFile(source);
        if (file != null) {
          output.addMessage(file.getAbsolutePath(), (int) diagnostic.getLineNumber(), (int) diagnostic.getColumnNumber(), diagnostic.getMessage(null), kind);
        } else {
          output.addLogMessage(String.format("Unsupported compiler message on %s resource %s: %s", source.getKind(), source.toUri(), diagnostic.getMessage(null)));
        }
      } else {
        output.addMessage(".", 0, 0, diagnostic.getMessage(null), kind);
      }
    }
  }