[ https://issues.apache.org/jira/browse/GROOVY-9073?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16823511#comment-16823511 ]
Keegan Witt commented on GROOVY-9073: ------------------------------------- {quote}Using the preview switch and also using -Dgroovy.target.bytecode=12 when calling groovyc seems to then produce class files with the required bytecode versions. The Java compiler seems to prohibit setting the preview flag for older bytecode versions, e.g. JDK11 and earlier. Currently we allow this - even though the produced class files can't be run, we might want to fail early with some kind of compiler error message in this case (or some of the cases). {quote} That is correct. I was following the pattern we did with the `--parameters` option (GROOVY-7423), which doesn't work with Java previous to 8 ([JEP 118|https://openjdk.java.net/jeps/118]), but we don't check the Java version. Lemme know if you think we should add a check in this case. {quote}I'd suggest using --enable-preview as the long switch (we can leave the short switch as is perhaps or just omit it). This will better match error message from the JDK: Caught: java.lang.UnsupportedClassVersionError: Preview features are not enabled for JEP325G (class file version 56.65535). Try running with '--enable-preview'. {quote} {quote}The other comment wrt doco is to do with whether this flag would ever be used with Groovy preview features. We could piggy back on the JDK concept but we currently don't and should mention that at least in the release notes so that folks understand this is just for JDK features at present. {quote} I've updated the PR for these. Lemme know if the doc can be any clearer that it's specifically for Java features. I'd assume if we were to ever add a similar flag to Groovy, we'd have a separate flag to control that. {quote}The class files produced from above can be successfully run from java with the appropriate switch and classspath set. Such classes can be run from Groovy without the preview flag set (so long as they don't refer to any preview Java class files) but can't be run with the preview flag set! And indeed just running any script, e.g. groovy --preview -e "println new Date()" will give the UnsupportedClassVersionError message as above. I suspect there is both a runtime and compile-time notion of version/bytecode version and we haven't married those up properly yet. {quote} Hmm – I'll do some testing around this. I hadn't really tested this out yet. > Support preview features in compiled classes > -------------------------------------------- > > Key: GROOVY-9073 > URL: https://issues.apache.org/jira/browse/GROOVY-9073 > Project: Groovy > Issue Type: Improvement > Components: Compiler > Reporter: Keegan Witt > Assignee: Keegan Witt > Priority: Major > Fix For: 3.0.0-beta-1, 2.5.7 > > Attachments: groovy9073_tweaks.patch > > Time Spent: 1.5h > Remaining Estimate: 0h > > In order to support the preview features ([JEP > 12|https://openjdk.java.net/jeps/12]), we need a new compiler flag to set the > [V_PREVIEW|https://asm.ow2.io/javadoc/org/objectweb/asm/Opcodes.html#V_PREVIEW] > opcode (to achieve the equivalent of {{--enable-preview}} mentioned in > [JDK-8195734|https://bugs.openjdk.java.net/browse/JDK-8195734]). -- This message was sent by Atlassian JIRA (v7.6.3#76005)