RFR: 6381729: Javadoc for generic constructor doesn't document type parameter
Archie Cobbs
acobbs at openjdk.org
Fri Jul 5 16:00:58 UTC 2024
The standard Javadoc doclet has a long-standing bug (since 2006, old enough to vote :) whereby constructor type parameters are simply omitted from the output. This results in confusing documentation that look like this:
public MyClass(T obj) - Description of this constructor...
with no explanation of what type `T` is.
The fix itself is straightforward (a one liner), but it requires a bit of prerequisite refactoring:
1. The method `Signatures.appendTypeParameters()` would throw an NPE if given a constructor, because the `returnType` is null. This is easy to work around with a simple null check.
2. The code for generating the HTML for the type parameters was embedded in a method `AbstractMemberWriter.addModifiersAndType()` which assumed a normal method with a return type. In order to make this bit resuable for constructors too, it has been extracted out into a new method `AbstractExecutableMemberWriter.addTypeParameters()`.
3. `ConstructorWriter` has an optimization in which constructor modifiers are omitted if all constructors are `public`, a common case. This optimization is disabled using a field named `foundNonPubConstructor`. A side effect of this optimization is that the constructor summary table has only two columns instead of three (the first being unnecessary). However, type parameters should appear in the same column as modifiers, so this logic was generalized to check for both (a) all constructors `public`, and (b) no constructors with type parameters. The field `foundNonPubConstructor` has been renamed to `showConstructorModifiers`; this clarifies `ClassUseWriter` using it for that purpose when generating the use index.
The above steps have been broken out into separate commits.
-------------
Commit messages:
- Consolidate "testConstructorGenericParam" into existing "testTypeParams" test.
- Bump copyright year.
- Fix bug where constructor type parameters were not included in HTML output.
- Refactor ConstructorWriter to clarify the purpose of the "foundNonPubConstructor" field.
- Avoid NPE in Signatures.appendTypeParameters() if invoked on constructor.
- Small refactoring to create AbstractExecutableMemberWriter.addTypeParameters().
Changes: https://git.openjdk.org/jdk/pull/20044/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=20044&range=00
Issue: https://bugs.openjdk.org/browse/JDK-6381729
Stats: 125 lines in 8 files changed: 87 ins; 16 del; 22 mod
Patch: https://git.openjdk.org/jdk/pull/20044.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/20044/head:pull/20044
PR: https://git.openjdk.org/jdk/pull/20044
More information about the javadoc-dev
mailing list