Details
-
Type:
CSR
-
Status: Closed
-
Priority:
P4
-
Resolution: Approved
-
Fix Version/s: 18
-
Component/s: core-libs
-
Labels:None
-
Subcomponent:
-
Compatibility Kind:behavioral
-
Compatibility Risk:minimal
-
Compatibility Risk Description:Adding default methods to an existing interface.
-
Interface Kind:Java API
-
Scope:SE
Description
Summary
Add convenience default methods to Messager
to make printing common errors, warnings, and notes easier.
Problem
The most common usage patterns of Messager
in annotation processors, such as printing a simple error or warning message, are unnecessarily verbose.
Solution
Add convenience default methods to Messager
for the most common cases.
Specification
--- a/src/java.compiler/share/classes/javax/annotation/processing/Messager.java
+++ b/src/java.compiler/share/classes/javax/annotation/processing/Messager.java
@@ -39,7 +39,8 @@ import javax.lang.model.element.*;
* javax.tools.Diagnostic.Kind#ERROR error kind} will {@linkplain
* RoundEnvironment#errorRaised raise an error}.
*
- * <p>Note that the messages "printed" by methods in this
+ * @apiNote
+ * The messages "printed" by methods in this
* interface may or may not appear as textual output to a location
* like {@link System#out} or {@link System#err}. Implementations may
* choose to present this information in a different fashion, such as
@@ -97,4 +98,90 @@ public interface Messager {
Element e,
AnnotationMirror a,
AnnotationValue v);
+ /**
+ * Prints an error.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.ERROR, msg)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @since 18
+ */
+ default void printError(CharSequence msg) {
+ printMessage(Diagnostic.Kind.ERROR, msg);
+ }
+
+ /**
+ * Prints an error at the location of the element.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.ERROR, msg, e)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @param e the element to use as a position hint
+ * @since 18
+ */
+ default void printError(CharSequence msg, Element e) {
+ printMessage(Diagnostic.Kind.ERROR, msg, e);
+ }
+
+ /**
+ * Prints a warning.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.WARNING, msg)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @since 18
+ */
+ default void printWarning(CharSequence msg) {
+ printMessage(Diagnostic.Kind.WARNING, msg);
+ }
+
+ /**
+ * Prints a warning at the location of the element.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.WARNING, msg, e)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @param e the element to use as a position hint
+ * @since 18
+ */
+ default void printWarning(CharSequence msg, Element e) {
+ printMessage(Diagnostic.Kind.WARNING, msg, e);
+ }
+
+ /**
+ * Prints a note.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.NOTE, msg)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @since 18
+ */
+ default void printNote(CharSequence msg) {
+ printMessage(Diagnostic.Kind.NOTE, msg);
+ }
+
+ /**
+ * Prints a note at the location of the element.
+ *
+ * @implSpec
+ * The default implementation is equivalent to {@code
+ * printMessage(Diagnostic.Kind.NOTE, msg, e)}.
+ *
+ * @param msg the message, or an empty string if none
+ * @param e the element to use as a position hint
+ * @since 18
+ */
+ default void printNote(CharSequence msg, Element e) {
+ printMessage(Diagnostic.Kind.NOTE, msg, e);
+ }
}
Attachments
Issue Links
- csr of
-
JDK-8273157 Add convenience methods to Messager
-
- Resolved
-