-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8373186: Improve readability of core reflection toString specifications #28688
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -346,24 +346,24 @@ public int hashCode() { | |
| } | ||
|
|
||
| /** | ||
| * Returns a string describing this {@code Field}. The format is | ||
| * the access modifiers for the field, if any, followed | ||
| * by the field type, followed by a space, followed by | ||
| * the fully-qualified name of the class declaring the field, | ||
| * followed by a period, followed by the name of the field. | ||
| * {@return a string describing this {@code Field}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a compelling reason to backtrack on the format of toString.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the reason to change the format is some future modifiers, like ACC_STRICT_INIT, won't get printed.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it helps avoid "attractive nuisance" questions like "how does the toString() of a bridge method indicate it is a bridge method"? (There is no such indication in the toString output, other than indirectly via the return type, but there is a Method.isBridge() predicate.) |
||
| * | ||
| * The string includes information about access modifiers of the | ||
| * field, the type of the field, the type declaring the field, | ||
| * and the name of the field. | ||
| * | ||
| * For example: | ||
| * <pre> | ||
| * public static final int java.lang.Thread.MIN_PRIORITY | ||
| * private int java.io.FileDescriptor.fd | ||
| * public static java.util.List Foo.bar | ||
| * </pre> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having such examples here means this method has a specified output that users can parse. We should move this to some non-normative section like an implementation note. (Don't know why github mobile didn't capture my comment before, I already commented yesterday)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. it is possible to move the examples so they are more clearly informative rather than normative. As a counterpoint, the toString() spec for java.lang.annotation.Annotation states:
Developers used this string successfully for informative purposes, even though the exact details have been changed (improved and corrected) a number of times over the years. |
||
| * | ||
| * <p>The modifiers are placed in canonical order as specified by | ||
| * "The Java Language Specification". This is {@code public}, | ||
| * {@code protected} or {@code private} first, and then other | ||
| * modifiers in the following order: {@code static}, {@code final}, | ||
| * {@code transient}, {@code volatile}. | ||
| * @apiNote | ||
| * Specific information about {@linkplain #getModifiers() | ||
| * modifiers} or other aspects of the field should be retrieved | ||
| * using methods for that purpose. | ||
|
Comment on lines
+362
to
+365
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needlessly vague and gives an opportunity to reinforce the preferred order of modifiers. |
||
| * | ||
| * @return a string describing this {@code Field} | ||
| * @jls 8.3.1 Field Modifiers | ||
| */ | ||
| public String toString() { | ||
|
|
@@ -380,21 +380,24 @@ String toShortString() { | |
| } | ||
|
|
||
| /** | ||
| * Returns a string describing this {@code Field}, including | ||
| * its generic type. The format is the access modifiers for the | ||
| * field, if any, followed by the generic field type, followed by | ||
| * a space, followed by the fully-qualified name of the class | ||
| * declaring the field, followed by a period, followed by the name | ||
| * of the field. | ||
| * | ||
| * <p>The modifiers are placed in canonical order as specified by | ||
| * "The Java Language Specification". This is {@code public}, | ||
| * {@code protected} or {@code private} first, and then other | ||
| * modifiers in the following order: {@code static}, {@code final}, | ||
| * {@code transient}, {@code volatile}. | ||
| * | ||
| * @return a string describing this {@code Field}, including | ||
| * its generic type | ||
| * {@return a string describing this {@code Field}, including | ||
| * its generic type} | ||
| * | ||
| * The string includes information about access modifiers of the | ||
| * field, the type of the field, the type declaring the field, | ||
| * and the name of the field. | ||
| * | ||
| * For example: | ||
| * <pre> | ||
| * public static final int java.lang.Thread.MIN_PRIORITY | ||
| * private int java.io.FileDescriptor.fd | ||
| * public static java.util.List<java.lang.String> Foo.bar | ||
| * </pre> | ||
| * | ||
| * @apiNote | ||
| * Specific information about {@linkplain #getModifiers() | ||
| * modifiers} or other aspects of the field should be retrieved | ||
| * using methods for that purpose. | ||
| * | ||
| * @since 1.5 | ||
| * @jls 8.3.1 Field Modifiers | ||
|
|
||
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.
Field::toString has been specified this way since JDK 1.1 and we have to assume there are frameworks or libraries that would break if we were ever change the format of the String representation. Would it be possible to put more context for the proposal in the JBS or PR? I think I'm mostly trying to understand if this is just about readability or whether an incompatible change to the string representation might follow.
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 don't have any implementation changes planned, but if there are new kinds of things added, I prefer to avoid treating the toString() method as a home for a context free grammar to provide all the necessary information about the item.