javascalajava-11java-platform-module-system

sun.misc.Contended is not a member of package sun.misc Scala


I'm updating JDK version from 8 to 11 an get the issue with some concurrent data structure.

error: type Contended is not a member of package sun.misc
[ERROR] @sun.misc.Contended
[ERROR]           ^
[ERROR] one error found

Is there a way to work around this? I'm using Scala 2.12.8?


Solution

  • If you're using this annotation in your user code, you should just remove it, as it's not doing anything.

    Hotspot allows use of this annotation in privileged code. From classFileParser.cpp:

    AnnotationCollector::ID
    AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
                                          const Symbol* name) {
      const vmSymbols::SID sid = vmSymbols::find_sid(name);
      // Privileged code can use all annotations.  Other code silently drops some.
      const bool privileged = loader_data->is_the_null_class_loader_data() ||
                              loader_data->is_platform_class_loader_data() ||
                              loader_data->is_anonymous();
      switch (sid) {
    ...
        case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): {
          if (_location != _in_field && _location != _in_class) {
            break;  // only allow for fields and classes
          }
          if (!EnableContended || (RestrictContended && !privileged)) {
            break;  // honor privileges
          }
          return _jdk_internal_vm_annotation_Contended;
        }
    

    If the annotation is used outside of privileged code it is normally ignored.


    But! it looks like there is a production level flag to enable @Contended specifically for non-privileged classes.

    In that case you can use --add-exports java.base/jdk.internal.vm.annotation=<your module> when compiling, and import the annotation from jdk.internal.vm.annotation instead.

    Then when running the application use -XX:-RestrictContended.

    Of course this is an internal API that should rather not be depended upon. However, for migration to later versions of Java, the above flags can be used as a temporary solution.