Skip to content

[wip] support ScopedValue via reflection#462

Draft
bjlaub wants to merge 2 commits intodevelopfrom
blaub/scoped-value-support
Draft

[wip] support ScopedValue via reflection#462
bjlaub wants to merge 2 commits intodevelopfrom
blaub/scoped-value-support

Conversation

@bjlaub
Copy link

@bjlaub bjlaub commented Aug 14, 2025

This adds support for runtime access to java.lang.ScopedValue without requiring preview features to be enabled, and without requiring a move to JDK25 language level, in a manner similar to virtual thread support in this library.

Note that ScopedValue is a preview feature in JDK21+, which would typically require compiling and running with --enable-preview. However, since we use reflection here, this check appears to be bypassed at runtime, so as long as we are running on JDK21 the classes should be available at runtime (though internal implementations and even the APIs may have changed since JDK21).

The use case for this is to allow consumers who either: (a) are running on Java 21 thru Java 24 to be able to use ScopedValue without explicitly requiring --enable-preview; or (b) are (or will be) running on Java 25 but targeting bytecode versions earlier than that.

I'm not sure that nylon-threads is the best place to house this code, as ScopedValue doesn't directly relate to the other code here, so happy to move this elsewhere if it makes more sense (though it is relevant to virtual threads in general as a more performant way to supply thread-local variables).

TODOs:

  • javadoc
  • more tests

@changelog-app
Copy link

changelog-app bot commented Aug 14, 2025

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

[wip] support ScopedValue via reflection

Check the box to generate changelog(s)

  • Generate changelog entry

import java.util.function.BiConsumer;
import java.util.function.Supplier;

public final class ScopedValues {

Choose a reason for hiding this comment

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants