Skip to content

Commit

Permalink
make use of Runtime.version().feature() (#2019)
Browse files Browse the repository at this point in the history
* make use of Runtime.version().feature()

* try avoiding classloading failure for class version unsupported by asm

* rename failOnIllegalArguentException to failOnUnexpectedParseClassException
  • Loading branch information
blackdrag authored Jan 9, 2024
1 parent f9327a6 commit 1034c3a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/codehaus/groovy/control/ClassNodeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,16 @@ public LookupResult findClassNode(final String name, final CompilationUnit compi
private LookupResult tryAsLoaderClassOrScript(final String name, final CompilationUnit compilationUnit) {
GroovyClassLoader loader = compilationUnit.getClassLoader();
Map<String, Boolean> options = compilationUnit.configuration.getOptimizationOptions();
boolean noClassLoaderResolving = Boolean.FALSE.equals(options.get("classLoaderResolving"));

if (!Boolean.FALSE.equals(options.get("asmResolving"))) {
LookupResult result = findDecompiled(name, compilationUnit, loader);
LookupResult result = findDecompiled(name, compilationUnit, loader, noClassLoaderResolving);
if (result != null) {
return result;
}
}

if (!Boolean.FALSE.equals(options.get("classLoaderResolving"))) {
if (!noClassLoaderResolving) {
return findByClassLoading(name, compilationUnit, loader);
}

Expand Down Expand Up @@ -241,7 +242,7 @@ private static LookupResult findByClassLoading(final String name, final Compilat
/**
* Search for classes using ASM decompiler
*/
private LookupResult findDecompiled(final String name, final CompilationUnit compilationUnit, final GroovyClassLoader loader) {
private LookupResult findDecompiled(final String name, final CompilationUnit compilationUnit, final GroovyClassLoader loader, boolean failOnUnexpectedParseClassException) {
ClassNode node = ClassHelper.make(name);
if (node.isResolved()) {
return new LookupResult(null, node);
Expand All @@ -259,6 +260,12 @@ private LookupResult findDecompiled(final String name, final CompilationUnit com
}
} catch (IOException e) {
// fall through and attempt other search strategies
} catch (IllegalArgumentException e) {
// very likely resolving failed here due to a class format error or similar
// if we do not try other means we should report this error to the user
if (failOnUnexpectedParseClassException) {
throw e;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -1115,7 +1114,7 @@ public final int getBytecodeVersion() {
* @since 4.0.0
*/
private static String defaultTargetBytecode() {
final String javaVersion = VMPlugin.getJavaVersion();
String javaVersion = Integer.toString(Runtime.version().feature());
if (JDK_TO_BYTECODE_VERSION_MAP.containsKey(javaVersion)) {
return javaVersion;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public interface VMPlugin {
*
* @return java version
* @since 4.0.0
* @deprecated
*/
@Deprecated
static String getJavaVersion() {
try {
return System.getProperty("java.specification.version");
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.groovy.util.Maps;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;

import java.math.BigDecimal;
import java.util.Map;

/**
Expand All @@ -31,22 +30,20 @@
*/
public class VMPluginFactory {

private static final Map<BigDecimal,String> PLUGIN_MAP = Maps.of(
private static final Map<Integer,String> PLUGIN_MAP = Maps.of(
// NOTE: Declare the vm plugin entries in *descending* order!
new BigDecimal( "16"), "org.codehaus.groovy.vmplugin.v16.Java16",
new BigDecimal( "10"), "org.codehaus.groovy.vmplugin.v10.Java10",
new BigDecimal( "9"), "org.codehaus.groovy.vmplugin.v9.Java9",
new BigDecimal("1.8"), "org.codehaus.groovy.vmplugin.v8.Java8"
16, "org.codehaus.groovy.vmplugin.v16.Java16",
10, "org.codehaus.groovy.vmplugin.v10.Java10"
);

private static final VMPlugin PLUGIN = createPlugin();

private static VMPlugin createPlugin() {
return doPrivileged(() -> {
ClassLoader loader = VMPluginFactory.class.getClassLoader();
BigDecimal specVer = new BigDecimal(VMPlugin.getJavaVersion());
for (Map.Entry<BigDecimal,String> entry : PLUGIN_MAP.entrySet()) {
if (DefaultGroovyMethods.isAtLeast(specVer, entry.getKey())) {
int specVer = Runtime.version().feature();
for (Map.Entry<Integer,String> entry : PLUGIN_MAP.entrySet()) {
if (specVer >= entry.getKey()) {
String fullName = entry.getValue();
try {
return (VMPlugin) loader.loadClass(fullName).getDeclaredConstructor().newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@
*/
package org.apache.groovy.groovysh.util

import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.vmplugin.VMPlugin

class SecurityManagerUtil {
private final SecurityManager saved

SecurityManagerUtil() {
if (explicitlyEnabled() || autoEnabledUntilJDK17()) {
saved = System.getSecurityManager()
System.setSecurityManager(new NoExitSecurityManager())
} else {
saved = null;
}
}

private boolean autoEnabledUntilJDK17() {
!CompilerConfiguration.isPostJDK18(VMPlugin.getJavaVersion())
private static boolean autoEnabledUntilJDK17() {
return Runtime.version().feature() < 18;
}

private boolean explicitlyEnabled() {
private static boolean explicitlyEnabled() {
System.getProperty('java.security.manager', 'disallow') == 'allow'
}

Expand Down

0 comments on commit 1034c3a

Please sign in to comment.