RFR: 8330005: RandomGeneratorFactory.getDefault() throws exception when the runtime image only has java.base module [v3]

Jaikiran Pai jpai at openjdk.org
Sat Apr 27 11:16:04 UTC 2024


On Fri, 26 Apr 2024 13:45:39 GMT, Alan Bateman <alanb at openjdk.org> wrote:

> How useful is it to deploy with additional RandomGenerator implementations on the class path or module path?

It doesn't look like in its current form it's possible (without an `--add-exports`) to have additional `java.util.random.RandomGenerator` implementations in the class path (or module path). I tried some experiment today to introduce a trivial `java.util.random.RandomGenerator` implementation as follows:


package foo;

import java.util.Random;
import java.util.random.RandomGenerator;

public class DummyRandomGenerator implements RandomGenerator {

    private final Random random = new Random();

    @Override
    public long nextLong() {
        System.out.println("nextLong from " + this.getClass());
        return random.nextLong();
    }
}

Package this `DummyRandomGenerator` into a jar file and in that jar file include a `META-INF/services/java.util.RandomGenerator` which points to the `foo.DummyRandomGenerator` class. Then launch a main application with this jar in the classpath:


import java.util.random.RandomGeneratorFactory;

public class Main {
    public static void main(String[] args) {
        System.out.println("available RandomGenerator(s):");
        RandomGeneratorFactory.all().map(RandomGeneratorFactory::name).forEach(System.out::println);
    }
}

Running this `Main` application with that jar in the classpath doesn't list the `DummyRandomGenerator`. It appears that such application/library specific implementations of `java.util.random.RandomGenerator` are expected to be annotated to an annotation that belongs to the internals of the java.base module https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/random/RandomGeneratorFactory.java#L391.

Changing the `DummyRandomGenerator` to have it annotated with:


@jdk.internal.util.random.RandomSupport.RandomGeneratorProperties(name="Dummy")
public class DummyRandomGenerator implements RandomGenerator {
...

and then compiling that application class with:


javac --add-exports java.base/jdk.internal.util.random=ALL-UNNAMED ...

and then re-packaging that jar with this class and rerunning the Main with that jar in the classpath will this time return the `DummyRandomGenerator` from a call to `RandomGeneratorFactory.all()`.

The API docs of `java.util.random.RandomGeneratorFactory` don't make a mention of the necessity of this internal `jdk.internal.util.random.RandomSupport.RandomGeneratorProperties` annotation.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/18932#issuecomment-2080455552


More information about the core-libs-dev mailing list