question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Runtime uses jvm methods that are depreciated and may be soon removed

See original GitHub issue

Compiler version

At current main - 1ea177df6d, and also previous versions.

The problem

The problem is caused by usage of Unsafe.objectFieldOffset in runtime. When compiling the compiler on JVM 18, the following errors appear:

sbt:scala3> testCompilation
[error] -- Error: /Users/srodziewicz/Documents/Projects/VirtusLab/dotty/library/src/scala/runtime/LazyVals.scala:103:19 
[error] 103 |    val r = unsafe.objectFieldOffset(clz.getDeclaredField(name))
[error]     |            ^^^^^^^^^^^^^^^^^^^^^^^^
[error]     |method objectFieldOffset in class Unsafe is deprecated since : see corresponding Javadoc for more information.
[error] -- Error: /Users/srodziewicz/Documents/Projects/VirtusLab/dotty/library/src/scala/runtime/LazyVals.scala:110:19 
[error] 110 |    val r = unsafe.objectFieldOffset(field)
[error]     |            ^^^^^^^^^^^^^^^^^^^^^^^^
[error]     |method objectFieldOffset in class Unsafe is deprecated since : see corresponding Javadoc for more information.

It’s not a problem per se, as we don’t use this version of JDK on compiling. But, per the documentation:

The guarantee that a field will always have the same offset and base may not be true in a future release. The ability to provide an offset and object reference to a heap memory accessor will be removed in a future release. Use {@link java.lang.invoke.VarHandle} instead.

And we heavily rely on this feature in the current and the new lazy vals implementation. It’s not a problem now, but soon lazy vals can stop working on new jvms as we don’t have any guarantee on their stability.

Proposal

Use MethodHandles to execute the Unsafe methods if the currently running jvm version == 1.8. In other case, use the VarHandles. It’s a pretty complex approach and there may be another simpler solution.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
smartercommented, Jun 24, 2022

I think it’s important for the compiler to compile on every jdk people might use, so a PR that would silence the deprecation would be welcome.

2reactions
smartercommented, Jun 6, 2022

What we could do is 1) under -release >= 9, use VarHandle properly 2) under -release 8, keep using unsafe but have a (potentially slower) runtime fallback if the method is not available

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deprecated List (Java SE 18 & JDK 18)
This method will remain in place until finalizers have been removed from most existing code. java.lang.Runtime.runFinalization(). Finalization has been ...
Read more >
Time to say goodbye to the finalize method in Java
In Java 9, the finalize() method has been deprecated. The finalization mechanism is inherently problematic and can lead to performance issues, ...
Read more >
When are API methods marked "deprecated" actually going ...
No deprecated API is safe as even APIs that have long been deprecated could be removed quite quickly now: one JDK release sets...
Read more >
MET02-J. Do not use deprecated or obsolete classes ...
Obsolete Methods and Classes ... The Java Virtual Machine Profiler Interface (JVMPI) and JVM Debug Interface (JVMDI) are also deprecated and have been...
Read more >
JEP 277: Enhanced Deprecation
If a deprecated API were to be removed in a JDK release, even after one ... and various disused Runtime and SecurityManager methods...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found