antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

java runtime: add module-info.java and multi-release build to support…

Open alexberdnik opened this issue 2 years ago • 5 comments

This PR fixes #2946 by adding module-info.java to antlr-runtime module of the project. This is done as "multi-release jar" which allows to specify multiple versions of classes for different java versions. In this case the module is compiled for java 8 with java 9 classes stored in META-INF/versions/9.

Module-info.java exports module named org.antlr.antlr.runtime. This differs from the original automatic-module-name (org.antlr.antlr4.runtime) because JPMS doesn't like trailing digits in names.

Module exports all packages except org.antlr.v4.runtime.tree.pattern and org.antlr.v4.runtime.tree.xpath. Please let me know if these are used outside the module and should be included as well.

alexberdnik avatar Feb 23 '23 22:02 alexberdnik

i think we're stuck at jdk 8 for now...

parrt avatar Mar 05 '23 17:03 parrt

That's ok, the runtime will continue to work under java 8. module-info.java will only be visible on java 9+ - see https://openjdk.org/jeps/238

alexberdnik avatar Mar 05 '23 17:03 alexberdnik

i think we're stuck at jdk 8 for now...

I just noticed that antlr4-maven-plugin requires java 11+, otherwise I'm getting wrong class version error...

I tested locally with these code (sample json parser):

        final JSONLexer lexer = new JSONLexer(CharStreams.fromString(json));
        final JSONParser parser = new JSONParser(new CommonTokenStream(lexer));
        final JSONParser.JsonContext ctx = parser.json();
        System.out.println(ctx.toStringTree());

... and the snapshot version of the runtime (one with module-info.java) The code works perfectly in modular environment (with module path) and also on java 8 jvm. For java 8 build, release option of the compiler is important, otherwise we could face phantom issues like this one.

So to sum-up:

  • runtime works both on java 8 and java 9 +
  • the project itself should be built by java 9+
  • (found during testing) latest antlr4-maven-plugin requires java 11 (tested 4.12.0 and 4.11.1, both are not working on java 8).

alexberdnik avatar Mar 06 '23 19:03 alexberdnik

I think I might have duplicated this one #4383

But in that PR, small difference is that I don't think Java 9 is required to build, so the JDK wouldn't need to be changed anywhere.

bowbahdoe avatar Aug 13 '23 12:08 bowbahdoe

I'm not sure moditect works without java 9 - you need to compile module-info.java somehow and java 8 compiler just can't do it. Anyway, you won't be able to compile the project with java < 11, as I mentioned before. Moditect-based approach looks a bit hacky for me - if the module-info.java can be written directly in the project, why don't just do that? I do understand that this may be the only way if you use a third-party library and don't have access to the source code.

alexberdnik avatar Aug 13 '23 21:08 alexberdnik