Skip to content

Conversation

@stuart-marks
Copy link
Member

@stuart-marks stuart-marks commented May 29, 2025

For a full explanation, see the bug report JDK-8358015.

This PR includes three related changes:

  • New overrides in SequencedMap view collection implementations, which improve their behavior.
  • Update to @implSpec clauses to reflect this behavior (which was previously ill-specified).
  • Update to a test which relied on unspecified behavior.

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8358081 to be approved

Issues

  • JDK-8358015: Fix SequencedMap sequenced view method specifications (Bug - P4)
  • JDK-8358081: Fix SequencedMap sequenced view method specifications (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25515/head:pull/25515
$ git checkout pull/25515

Update a local copy of the PR:
$ git checkout pull/25515
$ git pull https://git.openjdk.org/jdk.git pull/25515/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25515

View PR using the GUI difftool:
$ git pr show -t 25515

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25515.diff

Using Webrev

Link to Webrev Comment

@stuart-marks
Copy link
Member Author

/csr

@bridgekeeper
Copy link

bridgekeeper bot commented May 29, 2025

👋 Welcome back smarks! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented May 29, 2025

@stuart-marks 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:

8358015: Fix SequencedMap sequenced view method specifications

Reviewed-by: jpai, bchristi

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label May 29, 2025
@openjdk
Copy link

openjdk bot commented May 29, 2025

@stuart-marks has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@stuart-marks please create a CSR request for issue JDK-8358015 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@openjdk
Copy link

openjdk bot commented May 29, 2025

@stuart-marks The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label May 29, 2025
@stuart-marks stuart-marks marked this pull request as ready for review May 29, 2025 18:22
@openjdk openjdk bot added the rfr Pull request is ready for review label May 29, 2025
@mlbridge
Copy link

mlbridge bot commented May 29, 2025

Webrevs

/* non-public */ abstract static class ViewCollection<E> implements Collection<E> {
UnsupportedOperationException uoe() { return new UnsupportedOperationException(); }
// convert null entry return values into NSEE
<T> T nsee(T entry) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method can be static:

Suggested change
<T> T nsee(T entry) {
static <T> T nsee(T entry) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, unqualified call to inherited static method. I guess that's ok.

/* non-public */ abstract static class ViewCollection<E> implements Collection<E> {
UnsupportedOperationException uoe() { return new UnsupportedOperationException(); }
// convert null entry return values into NSEE
<T> T nsee(T entry) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<T> T nsee(T entry) {
<T extends Map.Entry<?, ?>> T nsee(T entry) {

So we don't accidentally use this on map keys or values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable.

* SequencedSet#addLast addLast} methods throw {@link UnsupportedOperationException}.
* Its {@link SequencedSet#getFirst getFirst} and {@link SequencedSet#getLast getLast}
* methods are implemented in terms of the {@link #firstEntry firstEntry} and {@link
* #lastEntry lastEntry} methods of this class, respectively. Its {@link
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Stuart, some parts of this and other updated method docs use "this interface", "this map", "this class". Was it intentional to use "this class" in this newly added text?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The other @implSpec clauses here all say "this interface" so I'll change the newly added "this class" to "this interface" in order to try to avoid confusion.

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good to me. I just have a small question about the newly introduced text.

The copyright years on BasicMap.java and SequencedMap.java will need a update before integrating.

@jaikiran
Copy link
Member

I just have a small question about the newly introduced text.

I just realized one other thing - the updated BasicMap.java fixes an issue in an existing test. Do we already have tests that might cover the new proposed semantics of these methods on the returned Set/Collection from the SequencedMap.sequencedEntrySet/sequencedKeySet/sequenceValues? Or should those be added?

@stuart-marks
Copy link
Member Author

@jaikiran

Do we already have tests that might cover the new proposed semantics of these methods on the returned Set/Collection from the SequencedMap.sequencedEntrySet/sequencedKeySet/sequenceValues?

Good question. There are several existing tests in BasicMap.java; see checkKeySet, checkValues, and checkEntrySet. There is also a set of tests starting at testKeySetRemoves and following which test removals on several variations of the map views, and also other mutator methods. I don't see anything obvious missing, but that doesn't mean everything is actually tested! Probably at some point it would be good to do a comprehensive analysis of the collections tests and see if they can be unified. We should probably take a look at the test coverage report too. But this is all about functional testing, in the sense of: starting from this state and performing this operation, do we get the right results and side effects? (This is mostly state verification.)

The stuff in @implSpec that specifies how something is done might not be tested. In this case the implementation was done using method A and the @implSpec modified to require method B. The test made some unwarranted assumptions about the behavior of method A (which actually was unspecified) that in fact was not supported by method B; and so the test failed. Otherwise I probably wouldn't have noticed this. In general I don't think we don't do much behavior verification in the JDK. If we do it's pretty ad hoc. In this case the question is, is there a test that verifies that a particular method is implemented by calling method B? I don't think so.

The terms state verification and behavior verification come from here:

https://martinfowler.com/articles/mocksArentStubs.html

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Stuart, thank you for the updates. These look good to me.

As for the testing, you are right that behaviour testing where method A calls B and such isn't usually tested in the JDK. I think that's fine. I was unclear about what testing I was talking about in my previous comment. I was mostly curious whether we had tests to verify that the getFirst()/getLast() EntrySet returned through a SequencedMap.sequencedEntrySet() does indeed prevent a setValue() call.

I had a look at the existing BasicMap.java and it does appear that we already have tests to verify that behaviour (the method checkUnmodifiableEntry()). So I think this change has the necessary test coverage.

public int hashCode() {
return view().hashCode();
}
public void addFirst(K k) { throw new UnsupportedOperationException(); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any utility in adding @Override annotations to the methods to document intent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Override annotation has been inconsistently applied in the collections implementations. In practice since so many methods are overridden, and some test would likely fail if a method weren't overridden properly, the annotation would mostly add clutter. Indeed I'm considering pulling out all uses of @Override in certain areas because they're just clutter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful in situations like this to have the inverse annotation -- "All methods should be overridden, except the ones marked @NoOverride".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. This @NoOverride idea could be useful on a subclass that wants to make sure it overrides everything. (Maybe @NoInherit would be a better name.) Meanwhile for JDK-8357272 (PR #25478) we're implementing similar policy checking in a test, though the exact policy is "no inheritance of default methods."

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Override annotation has been inconsistently applied in the collections implementations. In practice since so many methods are overridden, and some test would likely fail if a method weren't overridden properly, the annotation would mostly add clutter. Indeed I'm considering pulling out all uses of @Override in certain areas because they're just clutter.

The @OverRide annotation was one of Java's best innovations to detect consequences of changes in a base class for derived classes at compile time and it also carries information to any developer looking at a method, so calling it clutter (in Collection implementations) seems like a bit of exaggeration to me. Some IDEs have a feature to automatically add @OverRide annotations on save, and I think the JDK would improve if missing @OverRide annotations would be added to the whole code base.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Override annotation doesn't help with the main maintenance issue facing the collections framework, which occurs when an overriding method is missing. Virtually all of the public methods in the collection implementation classes are overrides. Adding an @Override annotation to every one of these would add a considerable amount of clutter for little benefit.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels Jun 4, 2025
@stuart-marks
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 4, 2025

Going to push as commit ef47635.
Since your change was applied there have been 18 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 4, 2025
@openjdk openjdk bot closed this Jun 4, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 4, 2025
@openjdk
Copy link

openjdk bot commented Jun 4, 2025

@stuart-marks Pushed as commit ef47635.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

7 participants