You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/10/06 13:26:30 UTC
[groovy] branch GROOVY-10286 updated (f226ac1 -> bd951f6)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch GROOVY-10286
in repository https://gitbox.apache.org/repos/asf/groovy.git.
discard f226ac1 GROOVY-10286: Set target bytecode by JDK
new bd951f6 GROOVY-10286: Set target bytecode by JDK
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (f226ac1)
\
N -- N -- N refs/heads/GROOVY-10286 (bd951f6)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/codehaus/groovy/vmplugin/VMPlugin.java | 24 +++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
[groovy] 01/01: GROOVY-10286: Set target bytecode by JDK
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-10286
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit bd951f6112d60fa59fdc3236aeb3be3655ddd80f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Oct 6 17:49:42 2021 +0800
GROOVY-10286: Set target bytecode by JDK
---
.../groovy/control/CompilerConfiguration.java | 19 ++++++++++++-
.../org/codehaus/groovy/vmplugin/VMPlugin.java | 32 ++++++++++++++++++++++
.../codehaus/groovy/vmplugin/VMPluginFactory.java | 2 +-
.../org/codehaus/groovy/classgen/RecordTest.groovy | 10 +++----
.../groovy/control/CompilerConfigurationTest.java | 2 +-
5 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
index fa366a5..ee1fe5c 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
@@ -22,6 +22,7 @@ import org.apache.groovy.util.Maps;
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.io.NullWriter;
import org.codehaus.groovy.control.messages.WarningMessage;
+import org.codehaus.groovy.vmplugin.VMPlugin;
import org.objectweb.asm.Opcodes;
import java.io.File;
@@ -129,6 +130,8 @@ public class CompilerConfiguration {
JDK18, Opcodes.V18
);
+ public static final String DEFAULT_TARGET_BYTECODE = defaultTargetBytecode();
+
/**
* The valid targetBytecode values.
*/
@@ -465,7 +468,7 @@ public class CompilerConfiguration {
sourceEncoding = getSystemPropertySafe("groovy.source.encoding",
getSystemPropertySafe("file.encoding", DEFAULT_SOURCE_ENCODING));
setTargetDirectorySafe(getSystemPropertySafe("groovy.target.directory"));
- setTargetBytecodeIfValid(getSystemPropertySafe("groovy.target.bytecode", JDK8));
+ setTargetBytecodeIfValid(getSystemPropertySafe("groovy.target.bytecode", DEFAULT_TARGET_BYTECODE));
defaultScriptExtension = getSystemPropertySafe("groovy.default.scriptExtension", ".groovy");
optimizationOptions = new HashMap<>(4);
@@ -987,6 +990,20 @@ public class CompilerConfiguration {
}
/**
+ * Returns the default target bytecode compatibility level
+ *
+ * @return the default target bytecode compatibility level
+ * @since 4.0.0
+ */
+ private static String defaultTargetBytecode() {
+ final String javaVersion = VMPlugin.getJavaVersion();
+ if (JDK_TO_BYTECODE_VERSION_MAP.containsKey(javaVersion)) {
+ return javaVersion;
+ }
+ return JDK8;
+ }
+
+ /**
* Whether the bytecode version has preview features enabled (JEP 12)
*
* @return preview features
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java b/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
index 10b94b4..2aa654c 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
@@ -20,10 +20,13 @@ package org.codehaus.groovy.vmplugin;
import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;
+import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CompileUnit;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Collections;
@@ -67,6 +70,35 @@ public interface VMPlugin {
int getVersion();
/**
+ * Returns java version, e.g. 1.8, 9, 11, 17
+ *
+ * @return java version
+ * @since 4.0.0
+ */
+ static String getJavaVersion() {
+ try {
+ return System.getProperty("java.specification.version");
+ } catch (SecurityException se) {
+ Class<?> versionClass;
+ try {
+ versionClass = Class.forName("java.lang.Runtime$Version"); // `Version` has been added since Java 9
+ } catch (ClassNotFoundException e) {
+ return "1.8";
+ }
+
+ try {
+ MethodHandles.Lookup lookup = MethodHandles.lookup();
+ MethodHandle versionMethodHandle = lookup.unreflect(Runtime.class.getMethod("version"));
+ Object version = versionMethodHandle.invoke();
+ MethodHandle majorMethodHandle = lookup.unreflect(versionClass.getMethod("major"));
+ return String.valueOf(majorMethodHandle.invoke(version));
+ } catch (Throwable t) {
+ throw new GroovyBugError(t.getMessage()); // should never happen
+ }
+ }
+ }
+
+ /**
* Check whether invoking {@link AccessibleObject#setAccessible(boolean)} on the accessible object will be completed successfully
*
* @param accessibleObject the accessible object to check
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java b/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
index d3ac172..e8d46d4 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
@@ -57,7 +57,7 @@ public class VMPluginFactory {
private static VMPlugin createPlugin() {
return AccessController.doPrivileged((PrivilegedAction<VMPlugin>) () -> {
- final BigDecimal specVer = new BigDecimal(System.getProperty("java.specification.version"));
+ final BigDecimal specVer = new BigDecimal(VMPlugin.getJavaVersion());
ClassLoader loader = VMPluginFactory.class.getClassLoader();
for (Map.Entry<BigDecimal, String> entry : PLUGIN_MAP.entrySet()) {
if (isAtLeast(specVer, entry.getKey())) {
diff --git a/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy b/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
index 2ee7077..2891d20 100644
--- a/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
+++ b/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
@@ -41,16 +41,16 @@ class RecordTest {
@Test
void testRecordOnJDK16plus() {
assumeTrue(isAtLeastJdk('16.0'))
- assertScript(new GroovyShell(new CompilerConfiguration(targetBytecode: CompilerConfiguration.JDK16)), '''
+ assertScript '''
record RecordJDK16plus(String name) {}
assert java.lang.Record == RecordJDK16plus.class.getSuperclass()
- ''')
+ '''
}
@Test
void testRecordOnJDK16plusWhenDisabled() {
assumeTrue(isAtLeastJdk('16.0'))
- def configuration = new CompilerConfiguration(targetBytecode: CompilerConfiguration.JDK16, recordsNative: false)
+ def configuration = new CompilerConfiguration(recordsNative: false)
assertScript(new GroovyShell(configuration), '''
record RecordJDK16plus2(String name) {}
assert java.lang.Record != RecordJDK16plus2.class.getSuperclass()
@@ -70,7 +70,7 @@ class RecordTest {
@Test
void testNativeRecordOnJDK16plus() {
assumeTrue(isAtLeastJdk('16.0'))
- assertScript(new GroovyShell(new CompilerConfiguration(targetBytecode: CompilerConfiguration.JDK16)), '''
+ assertScript '''
import java.lang.annotation.Annotation
import java.lang.annotation.Documented
import java.lang.annotation.ElementType
@@ -101,7 +101,7 @@ class RecordTest {
assert 'java.util.List<java.lang.String>' == rcs[2].genericType.toString()
assert 'titles' == rcs[3].name && String[].class == rcs[3].type
- ''')
+ '''
}
@Test
diff --git a/src/test/org/codehaus/groovy/control/CompilerConfigurationTest.java b/src/test/org/codehaus/groovy/control/CompilerConfigurationTest.java
index c1db59e..6c34821 100644
--- a/src/test/org/codehaus/groovy/control/CompilerConfigurationTest.java
+++ b/src/test/org/codehaus/groovy/control/CompilerConfigurationTest.java
@@ -60,7 +60,7 @@ public final class CompilerConfigurationTest {
assertEquals(10, config.getTolerance());
assertEquals(100, config.getMinimumRecompilationInterval());
assertEquals(System.getProperty("file.encoding", CompilerConfiguration.DEFAULT_SOURCE_ENCODING), config.getSourceEncoding());
- assertEquals(CompilerConfiguration.JDK8, config.getTargetBytecode());
+ assertEquals(CompilerConfiguration.DEFAULT_TARGET_BYTECODE, config.getTargetBytecode());
assertEquals(Boolean.FALSE, config.getRecompileGroovySource());
assertEquals(Collections.emptyList(), config.getClasspath());
assertEquals(".groovy", config.getDefaultScriptExtension());