diff --git a/jetty-client/pom.xml b/jetty-client/pom.xml index 92f6a792766..100dd8e3496 100644 --- a/jetty-client/pom.xml +++ b/jetty-client/pom.xml @@ -51,6 +51,7 @@ shade + true true hybrid diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ModuleLocation.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ModuleLocation.java index 7fc26fc84d1..b5eb4ebd939 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ModuleLocation.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ModuleLocation.java @@ -23,6 +23,7 @@ import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; import java.net.URI; import java.util.Optional; +import java.util.function.Function; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -53,7 +54,7 @@ import static java.lang.invoke.MethodType.methodType; * * In Jetty 10, this entire class can be moved to direct calls to java.lang.Module in TypeUtil.getModuleLocation() */ -class ModuleLocation +class ModuleLocation implements Function, URI> { private static final Logger LOG = Log.getLogger(ModuleLocation.class); @@ -100,7 +101,8 @@ class ModuleLocation } } - public URI getModuleLocation(Class clazz) + @Override + public URI apply(Class clazz) { try { diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java index 6ec3d8ada0b..65121f7aa5a 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java @@ -19,9 +19,6 @@ package org.eclipse.jetty.util; import java.io.IOException; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -38,12 +35,11 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import static java.lang.invoke.MethodType.methodType; - /** * TYPE Utilities. * Provides various static utility methods for manipulating types and their @@ -177,38 +173,32 @@ public class TypeUtil } } - private static final MethodHandle[] LOCATION_METHODS; - private static final ModuleLocation MODULE_LOCATION; + private static final List, URI>> LOCATION_METHODS = new ArrayList<>(); + private static final Function, URI> MODULE_LOCATION; static { - List locationMethods = new ArrayList<>(); - - MethodHandles.Lookup lookup = MethodHandles.lookup(); - MethodType type = methodType(URI.class, Class.class); - + // Lookup order in LOCATION_METHODS is important. + LOCATION_METHODS.add(TypeUtil::getCodeSourceLocation); + Function, URI> moduleFunc = null; try { - locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type)); - ModuleLocation moduleLocation = null; - try + Class clazzModuleLocation = TypeUtil.class.getClassLoader().loadClass(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); + Object obj = clazzModuleLocation.getConstructor().newInstance(); + if (obj instanceof Function) { - moduleLocation = new ModuleLocation(); - locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type)); + //noinspection unchecked + moduleFunc = (Function, URI>)obj; + LOCATION_METHODS.add(moduleFunc); } - catch (UnsupportedOperationException e) - { - LOG.debug("JVM Runtime does not support Modules"); - } - MODULE_LOCATION = moduleLocation; - locationMethods.add(lookup.findStatic(TypeUtil.class, "getClassLoaderLocation", type)); - locationMethods.add(lookup.findStatic(TypeUtil.class, "getSystemClassLoaderLocation", type)); - LOCATION_METHODS = locationMethods.toArray(new MethodHandle[0]); } - catch (Exception e) + catch (Throwable t) { - throw new RuntimeException("Unable to establish Location Lookup Handles", e); + LOG.debug("This JVM Runtime does not support Modules, disabling Jetty internal support"); } + MODULE_LOCATION = moduleFunc; + LOCATION_METHODS.add(TypeUtil::getClassLoaderLocation); + LOCATION_METHODS.add(TypeUtil::getSystemClassLoaderLocation); } /** @@ -627,13 +617,11 @@ public class TypeUtil */ public static URI getLocationOfClass(Class clazz) { - URI location; - - for (MethodHandle locationMethod : LOCATION_METHODS) + for (Function, URI> locationFunction : LOCATION_METHODS) { try { - location = (URI)locationMethod.invoke(clazz); + URI location = locationFunction.apply(clazz); if (location != null) { return location; @@ -723,7 +711,7 @@ public class TypeUtil // In Jetty 10, this method can be implemented directly, without reflection if (MODULE_LOCATION != null) { - return MODULE_LOCATION.getModuleLocation(clazz); + return MODULE_LOCATION.apply(clazz); } return null; }