JDK-8354556 : Expand value-based class warnings to java.lang.ref API
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 25
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2025-04-14
  • Updated: 2025-05-19
  • Resolved: 2025-05-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 25
25 masterFixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
JEP 390 introduced a 'synchronization' lint category for inappropriate uses of value-based class types in 'synchronized' expressions.

We'd like to make two enhancements to this lint category:
- Rename it 'identity' (as a more general description of what it's for)
- Produce additional warnings for inappropriate uses of 'java.lang.ref' APIs

It may make sense to support the 'identity' and 'synchronization' keys for a few releases as aliases, easing migration for anyone who has opted in to this category explicitly, or who has applied '@SuppressWarnings("synchronization")'.

Specific treatment of APIs is as follows:

1) Define a new internal annotation, @jdk.internal.RequiresIdentity, with target types PARAMETER and TYPE_PARAMETER. The @RequiresIdentity annotation expresses the expectation that an argument to a given method or constructor parameter will be an object with a unique identity, not an instance of a value-based class; or that the type argument to a given type parameter will not be a value-based class type.

2) Apply the annotation to the following:
- The referent parameter of all constructors in classes PhantomReference, SoftReference, and WeakReference
- The obj parameter of Cleaner.register
- The type parameter T of Reference, PhantomReference, SoftReference, WeakReference, and ReferenceQueue
- The key parameter of WeakHashMap.put
- The type parameter K of WeakHashMap

3) Produce a warning whenever an expression of a value-based class type appears as an argument to one of the @RequiresIdentity method/constructor parameters above (warning due to the fact that the given API will throw once the value-based class migrates to be a value class)

4) Produce a warning whenever a value-based class type appears as a type argument for an @RequiresIdentity type parameter (warning due to the fact that the generic API will be impossible to properly use once the value-based class migrates to be a value class)

(Aside: in the future, these warnings will also be enforced for value class types. And additional JDK APIs may decide to adopt the @RequiresIdentity annotation if they also will be specified to throw on value object inputs—but we haven't identified any such APIs to date.)
Comments
Changeset: 637e9d16 Branch: master Author: Vicente Romero <vromero@openjdk.org> Date: 2025-05-19 22:47:13 +0000 URL: https://git.openjdk.org/jdk/commit/637e9d16ddb21003234abcd32f759aefd91f21f9
19-05-2025

Regarding: "It may make sense to support the 'identity' and 'synchronization' keys for a few releases as aliases". There is an existing feature request for adding Lint category aliases: JDK-7004476 "Lint.LintCategory should support aliases" This issue might prod us to address that issue now, so we have a clean & proper mechanism for lint category aliasing before trying to add our first alias. For now this could be a very simple mechanism in which an alias name is immediately mapped to the corresponding canonical name and then "forgotten". This would amount to a few lines of code added to Lint.Category.get().
01-05-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/24746 Date: 2025-04-18 00:06:26 +0000
01-05-2025