Details
-
CSR
-
Resolution: Approved
-
P4
-
None
-
behavioral
-
minimal
-
Very small risk to add a default method to an existing interface.
-
Java API
-
SE
Description
Summary
For enum
constants with specialized class bodies, the JLS mandates a translation into an anonymous subclass. The javax.lang.model
API does not expose such subclasses when present.
Problem
In terms of providing as full a model of the language as reasonable, the lack of information on such class bodies is a gap. This gap has come up in writing facilities in various areas, including lint checks and fuller javadoc support.
Solution
Add a method to the Elements
interface to provide the missing functionality. A default method is added to provide a nominal implementation, as usual.
Specification
diff --git a/src/java.compiler/share/classes/javax/lang/model/util/Elements.java b/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
index d3326ba7018f..0b1e49768184 100644
--- a/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
+++ b/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
@@ -758,6 +758,30 @@ default boolean isAutomaticModule(ModuleElement module) {
return false;
}
+ /**
+ * {@return the class body of an {@code enum} constant if the
+ * argument is an {@code enum} constant declared with an optional
+ * class body, {@code null} otherwise}
+ *
+ * @implSpec
+ * The default implementation of this method throws {@code
+ * UnsupportedOperationException} if the argument is an {@code
+ * enum} constant and throws an {@code IllegalArgumentException}
+ * if it is not.
+ *
+ * @param enumConstant an enum constant
+ * @throws IllegalArgumentException if the argument is not an {@code enum} constant
+ * @jls 8.9.1 Enum Constants
+ * @since 22
+ */
+ default TypeElement getEnumConstantBody(VariableElement enumConstant) {
+ if (enumConstant.getKind() == ElementKind.ENUM_CONSTANT) {
+ throw new UnsupportedOperationException();
+ } else {
+ throw new IllegalArgumentException("Argument not an enum constant");
+ }
+ }
+
/**
* Returns the record component for the given accessor. Returns
* {@code null} if the given method is not a record component
Attachments
Issue Links
- csr of
-
JDK-8312418 Add Elements.getEnumConstantBody
- Resolved