-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JDK-8312418: Add Elements.getEnumConstantBody #14939
Conversation
👋 Welcome back darcy! A progress list of the required criteria for merging this PR into |
/csr needed |
@jddarcy has indicated that a compatibility and specification (CSR) request is needed for this pull request. @jddarcy please create a CSR request for issue JDK-8312418 with the correct fix version. This pull request cannot be integrated until the CSR request is approved. |
Add code from @lahodaj and make the PR non-draft to continue the discussion. |
/contributor add @lahodaj |
@jddarcy |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this; it looks like it also brings jdk.javadoc closer to fixing JDK-8144631: No documentation is created for methods of enum constants' class bodies
* @implSpec | ||
* The default implementation of this method returns {@code null} | ||
* if the argument is an {@code enum} constant and throws an | ||
* {@code IllegalArgumentException} if it is not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
* {@code IllegalArgumentException} if it is not. | |
* {@code IllegalArgumentException} if it is not. |
@@ -725,6 +728,37 @@ public boolean isAutomaticModule(ModuleElement module) { | |||
return (msym.flags() & Flags.AUTOMATIC_MODULE) != 0; | |||
} | |||
|
|||
@Override @DefinedBy(Api.LANGUAGE_MODEL) | |||
public TypeElement getEnumConstantBody(VariableElement enumConstant) { | |||
if (enumConstant.getKind() == ElementKind.ENUM_CONSTANT) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inverting if
-else
(exception first) might be cleaner and can also save indentation if else
is not used. But it's a matter of style.
switch (field.getKind()) { | ||
case FIELD -> expectException(field); | ||
case ENUM_CONSTANT -> testEnumConstant(field, typeElt); | ||
default -> throw new RuntimeException("Unexpected field kind seen"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unusual indentation.
private void expectException0(Supplier<TypeElement> supplier, String message) { | ||
try { | ||
var typeElement = supplier.get(); | ||
messager.printError(message, typeElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing for an expected exception is surprisingly subtle. Testing for an expected unchecked exception is even more so.
If a test framework is not used, we should be very careful. Consider moving printError outside try-catch to exclude even the slightest possibility of false negative.
|
||
// Nested classes hosting a variety of different kinds of fields. | ||
|
||
private static enum Body { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to expand testing, to make sure there are no surprises with constants that override or implement methods, as those are the important cases as per JLS 8.9.1, which you quote.
A few explanatory notes on the changes in the printing processor. Consider an enum like
which uses enum constants with bodies. If this enum class is printed from a class file, but not source code, the output is like:
This does reflect how javac currently lowers an enum to a class file, but the permits information is not really helpful and is better to be elided, as the extra check now does. If one goes out of the way to explicitly request printing of the anonymous class used for an enum contanst, the output is similar to:
While this is reasonable for a general anonymous class, it isn't helpful for an enum body and I think it is best to just have no output in this case. Generally we haven't tested the exact printing processor output and I think it is best if we continue that policy here. |
To address some concerns raised off-list by @jonathan-gibbons, I modified the semantics of the default method to always throw some exception, either an IllegalArgumentException or an UnsupportedOperationException. This is (slightly) a more precise approximation to the "real" behavior of the method than always throwing UnsupportedOperationException. |
boolean elementSeen = false; | ||
|
||
for (TypeElement typeElt : ElementFilter.typesIn(typeRoot.getEnclosedElements()) ) { | ||
System.out.println("Testing type " + typeElt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should the println
invocations be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be a matter of style, but logs like this inside tests may help to determine where is the problem when the test fails on a CI server.
*/ | ||
default TypeElement getEnumConstantBody(VariableElement enumConstant) { | ||
switch(enumConstant.getKind()) { | ||
case ENUM_CONSTANT -> throw new UnsupportedOperationException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me
@jddarcy This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 55 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
/integrate |
Going to push as commit 578ded4.
Your commit was automatically rebased without conflicts. |
Trivial implementation to get feedback on the proposed API.
Progress
Issues
Reviewers
Contributors
<jlahoda@openjdk.org>
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/14939/head:pull/14939
$ git checkout pull/14939
Update a local copy of the PR:
$ git checkout pull/14939
$ git pull https://git.openjdk.org/jdk.git pull/14939/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 14939
View PR using the GUI difftool:
$ git pr show -t 14939
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/14939.diff
Webrev
Link to Webrev Comment