RFR: JDK-8312418: Add Elements.getEnumConstantBody [v5]

Pavel Rappo prappo at openjdk.org
Thu Aug 24 22:36:14 UTC 2023

On Thu, 24 Aug 2023 22:23:13 GMT, Jonathan Gibbons <jjg at openjdk.org> wrote:

>> src/java.compiler/share/classes/javax/lang/model/util/Elements.java line 779:
>>> 777:     default TypeElement getEnumConstantBody(VariableElement enumConstant) {
>>> 778:         switch(enumConstant.getKind()) {
>>> 779:         case ENUM_CONSTANT -> throw new UnsupportedOperationException();
>> Hm... Most retrofitted methods in this API return null or false, not throw UnsupportedOperationException. The only other exception seems to be Elements.getFileObjectOf. Is this the new approach, or it just does make more sense for these two particular methods?
> I think it depends whether a `null` result is permitted by the specification of the method.  For this method, while `null` would be the correct result most of the time, it is exactly the wrong result when it is most interesting, and so a non-exception result would be misleading.

I see your point and agree with it: the `default` implementation will return `null` if called for an enum constant with the class body. That said, isn't, say, `Elements.getTypeElement(ModuleElement, CharSequence)` in a similar situation? But it returns `null`.


PR Review Comment: https://git.openjdk.org/jdk/pull/14939#discussion_r1304937087

More information about the compiler-dev mailing list