-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8263102: Expand documention of Method.isBridge #2852
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
Conversation
👋 Welcome back darcy! A progress list of the required criteria for merging this PR into |
Webrevs
|
I suggest briefly mentioning generic specialization as an example as well. |
* method calls its non-bridge counterpart and returns its | ||
* result. (While the Java language specification forbids a class | ||
* declaring two methods with the same parameter types but a | ||
* different return type, the virtual machine does 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.
I'm of two minds about this. This is certainly a good example of a bridge method. It doesn't motivate why a bridge method is necessary in this case. It think it's because the language says you should be able to write code like
EnumSet<E> es2 = es.clone();
so there needs to be a method defined in the class file whose return type is assignment-compatible with EnumSet
. However, a clone
method that returns Object
is the only one that can override the Object::clone
method. Thus, a something is necessary to span this gap -- a bridge method.
On the other hand, this might be too much detail for here. This is a really obscure location. It seems like somewhere else would be better, and where a full explanation with examples can be provided.
* | ||
* <p>Bridge methods may also be used by Java compiler in other | ||
* circumstances to span across difference in Java Language | ||
* semantics and JVM semantics. |
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.
If you decide to put less detail here, you could start with this statement. I think the main point is that there are some semantic gaps between methods in the Java language and in the JVM; bridge methods are necessary to "span" this gap. You might simply list some examples without explaining them fully.
Would this be "used by a Java compiler" or "used by the Java compiler?
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.
Imo "created" should be better. Is there any case where a java compiler accepts bridge methods generated?
Mailing list message from Joe Darcy on core-libs-dev: Hi Stuart, On 3/8/2021 6:02 PM, Stuart Marks wrote:
If someone is looking at isBridge and wondering what it does, having The text in question is not intended to be exhaustive and the exact use Also, if in the future there are new JVM/platform facilities added so Thanks, -Joe |
* declaring two methods with the same parameter types but a | ||
* different return type, the virtual machine does not. | ||
* A | ||
* common case where covariant overrides are used is for a {@link |
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 the example should be clearer because here, you don't show the code of EnumSet.clone().
I wonder if it's not easier if all the code is visible
interface I {
Object m();
}
class A implements I {
String m() { return "hello"; }
}
so you can explain that the VM do the dispatch on I::m()Object so the compiler has to insert a method A::m()Object,
the bridge method with this pseudo-code
class A implements I {
/* bridge */ Object m() { return m(); } // calls m()String
String m() { return "hello"; }
}
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.
Hi Remi,
Thanks for the feedback. I did consider that kind of approach of an example from scratch. I judged referencing instances of bridge methods in the base module to be helpful to demonstrate it wasn't too esoteric of a feature in terms of it is something you, as a developer, may already have seen. Also, given the likely audience of the reading Class.isBridge, I didn't think a stand-alone example was warranted.
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.
The prose description here and is still rather clumsy, though. If you're going to use EnumSet.clone() as an example, maybe run with that and list the methods directly instead of saying "EnumSet.clone() returns EnumSet rather than Object", be more explicit. Maybe something like the following:
The Object class has the method:
Object clone()
whereas the EnumSet class has this method:
EnumSet<E> clone()
From the JVM's perspective, the second method doesn't override the first, because they have different return types. The Java compiler provides a proper override by inserting the following bridge method into the EnumSet class:
Object clone()
that simply calls the second method and returns its result.
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.
@jddarcy, the problem i see is that EnumSet
is not used a lot and clone
has another layer of complexity because of CloneNotSupportedException, etc.
So i'm not sure that using EnumSet.clone() is the right example here.
Perhaps String.compareTo()/Comparable.compareTo() is a better example ? But generics are in the middle too.
* code. | ||
* Bridge methods are used by Java compilers in various | ||
* circumstances to span across differences in Java programming | ||
* language semantics and JVM semantics. |
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.
Quibble: "span across" => "span"
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.
Oh, also maybe need a
tag here.
@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 44 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 |
Mailing list message from Joe Darcy on core-libs-dev: On 3/9/2021 12:03 PM, Stuart Marks wrote:
Pushed a refinement along those lines; new text: A bridge method is a synthetic method created by a Java compiler -Joe |
* circumstances to span differences in Java programming | ||
* language semantics and JVM semantics. | ||
* | ||
* One example use of bridge methods is as technique for a |
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.
"a technique"
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.
OK just a couple markup comments, otherwise good.
* code. | ||
* Bridge methods are used by Java compilers in various | ||
* circumstances to span across differences in Java programming | ||
* language semantics and JVM semantics. |
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.
Oh, also maybe need a
tag here.
* {@code protected Object clone() throws CloneNotSupportedException {...}}<br> | ||
* and {@code EnumSet<E>} declares its language-level {@linkplain java.util.EnumSet#clone() covariant override}<br> | ||
* {@code public EnumSet<E> clone() {...}}<br> | ||
* If this technique was being used, the resulting class |
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.
Not sure what the preferred code block markup style is here; maybe <pre>{@code ...}</pre>
instead of <br>
.
/integrate |
@jddarcy Since your change was applied there have been 44 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit 67ea3bd. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
The existing documentation of Method.isBridge isn't terribly helpful to the reader. This RFE proposes to given a common example of how bridge methods are used. The JLS does not have a section discussing bridge methods in detail; bridge methods are a compilation technique for lowering the Java language to the JVM, they are not a language feature per se. The example given is not exhaustive; there can be and are other uses of bridge methods.
Once the text is agreed to; I'll update the copyright year as well.
Progress
Issue
Reviewers
Download
$ git fetch https://git.openjdk.java.net/jdk pull/2852/head:pull/2852
$ git checkout pull/2852