Copilot commented on code in PR #2904: URL: https://github.com/apache/groovy/pull/2904#discussion_r3967743484
########## src/main/java/org/codehaus/groovy/vmplugin/JavaFeatureVersion.java: ########## @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.vmplugin; + +/** + * The Java feature version of the running VM, e.g. {@code 17} (GROOVY-12382). + * <p> + * {@code Runtime.version()} is a Java 9 API that some runtimes built on the + * JDK class library do not provide (Android's ART, for one). Where it is + * missing, the {@code java.specification.version} property is parsed instead, + * and when that is unusable the lowest release Groovy supports is assumed. + * <p> + * Deliberately tiny and free of lambdas: it is consulted from static + * initialisers that can run while the system class loader is being + * instantiated ({@code -Djava.system.class.loader=groovy.lang.GroovyClassLoader}), + * where an {@code invokedynamic} bootstrap is not yet permitted, and it must + * not trigger the creation of the VM plugin. + */ +public final class JavaFeatureVersion { + + /** The lowest Java release Groovy runs on. */ + public static final int MINIMUM = 17; + + private JavaFeatureVersion() { + } + + /** + * Returns the feature version of the running VM. + * + * @return the feature version, never below {@link #MINIMUM} + */ + public static int current() { + try { + return Runtime.version().feature(); + } catch (Throwable ignore) { + // NoSuchMethodError on a runtime without Runtime.version() + } + return parse(System.getProperty("java.specification.version"), MINIMUM); + } Review Comment: Catching `Throwable` is overly broad here and can mask serious problems (e.g., `OutOfMemoryError`, `StackOverflowError`). Since the intent is to tolerate runtimes missing `Runtime.version()`, narrow the catch to `NoSuchMethodError` (or possibly `LinkageError` if you want to cover related linkage failures) and let other errors propagate. ########## src/test/groovy/org/codehaus/groovy/vmplugin/VMPluginFactoryTest.groovy: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.vmplugin + +import org.codehaus.groovy.control.CompilerConfiguration +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.assertNotNull +import static org.junit.jupiter.api.Assertions.assertSame + +/** + * GROOVY-12382: the feature version must be available on runtimes without + * {@code Runtime.version()}, such as Android's ART, where the specification + * version property is the only clue and may itself be meaningless ("0.9"). + */ +final class VMPluginFactoryTest { + + @Test + void featureVersionMatchesTheRuntimeOnAJvm() { + assertEquals(Runtime.version().feature(), VMPluginFactory.featureVersion()) + assertEquals(Runtime.version().feature(), JavaFeatureVersion.current()) + assertEquals(17, JavaFeatureVersion.MINIMUM) + assertEquals(Integer.toString(Runtime.version().feature()), CompilerConfiguration.DEFAULT_TARGET_BYTECODE) + } Review Comment: This test hard-depends on `Runtime.version()` and also assumes `CompilerConfiguration.DEFAULT_TARGET_BYTECODE` always equals the runtime feature version. That assumption can break on newer Java releases if Groovy clamps/normalizes to the latest supported bytecode in its internal map. To keep the test aligned with the PR’s goal (tolerating runtimes without `Runtime.version()`), avoid calling `Runtime.version()` in the test and assert properties that are stable: e.g., that `VMPluginFactory.featureVersion()` equals `JavaFeatureVersion.current()`, and that `DEFAULT_TARGET_BYTECODE` parses as an integer >= `JavaFeatureVersion.MINIMUM` (or matches the expected clamped behavior if that is the intended contract). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
