JDK-8256023 : [JEP 390] Add 'lint' warning for synchronization attempts on value-based class instances
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 16
  • Submitted: 2020-11-09
  • Updated: 2020-12-07
  • Resolved: 2020-12-07
Related Reports
CSR :  
Description
Summary
-------

Add a new lint option, `synchronization`, to `javac` to warn about usages of the `synchronized` statement with an operand that is an instance of a value-based class.

Problem
-------

The Valhalla Project is pursuing a significant enhancement to the Java programming model in the form of primitive classes, that offer various performance optimization opportunities. Several classes in the Java Platform that have been informally designated as [_value-based classes_](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/doc-files/ValueBased.html)
in the API specifications are candidates for being migrated to primitive classes in a future release.

Synchronization on instances of value-based classes is discouraged, because the classes' factories do not promise unique ownership of an object identity. After migrating to primitive classes, attempts to synchronize on instances of these classes will produce exceptions, since primitive class instances have no monitors associated with them that can be locked.

Solution
--------

Enhance `javac` to detect `synchronized` statements that have operands that are instances of value-based classes and warn programmers that these synchronization operations are discouraged.

These compile-time checks will be complemented by dynamic checks in the JVM, via JDK-8252182.


Specification
-------------

The new `javac` lint category `synchronization` is turned on by default and produces the following diagnostic message:

         warning: [synchronization] attempt to synchronize on an instance of a value-based class.

 

Comments
Moving to Approved. The category name "synchronization" seems rather broad for what the check does. Please update the CSR if during code review a narrower name is decided upon.
07-12-2020

Q: What is the set of classes that will be considered value-based by this warning? A: Please see JDK-8252181 for a set of candidate classes. In the final scheme of things, it has been decided that the annotation that flags a class as being value-based will be a JDK internal one. OIOW, the new lint warning is restricted to abuses of value-based JDK classes either internally with the platform JDK code or (more intentionally) by client code elsewhere. Please see the subtasks of JDK-8252181 for how this process of choosing and annotating existing JDK classes is taking shape. Q: I assume the warning is limited to synchronized statements and not the declaration or invocation of synchronized methods. A: Correct. A conscious decision in work relating to this JEP is that the new lint diagnostic capability is NOT expected to produce diagnostics for the use of the *author* of the value based class, only the user/clients of such classes. This means that not having any synchronized methods in a value-based class is a responsibility left to the author/reviewer of the class and is not expected to be weeded out by javac. Likewise for any number of other contract violations (e.g: vbc should be final, with only final instance fields ...)
17-11-2020

Moving to Provisional, not Approved. What is the set of classes that will be considered value-based by this warning? I assume the warning is limited to synchronized statements and *not* the declaration or invocation of synchronized methods.
13-11-2020

[~dlsmith] Could you be the reviewer for this please ?
11-11-2020