From d7982f869b42eb2947b28e117ac8ec6dc1438bcf Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 10 Mar 2021 10:45:29 -0600 Subject: [PATCH 1/3] Issue #6052 - make ModuleLocation optional on Android Signed-off-by: Joakim Erdfelt --- jetty-client/pom.xml | 1 + .../eclipse/jetty/util/ModuleLocation.java | 6 +++-- .../java/org/eclipse/jetty/util/TypeUtil.java | 23 ++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) 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..655ad624b9c 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 @@ -38,6 +38,7 @@ 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; @@ -178,7 +179,7 @@ public class TypeUtil } private static final MethodHandle[] LOCATION_METHODS; - private static final ModuleLocation MODULE_LOCATION; + private static final Function, URI> MODULE_LOCATION; static { @@ -190,17 +191,23 @@ public class TypeUtil try { locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type)); - ModuleLocation moduleLocation = null; + Function, URI> moduleFunc = null; try { - moduleLocation = new ModuleLocation(); - locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type)); + Class clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); + Object obj = clazzModuleLocation.getConstructor().newInstance(); + if (obj instanceof Function) + { + //noinspection unchecked + moduleFunc = (Function, URI>)obj; + locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type)); + } } - catch (UnsupportedOperationException e) + catch (Throwable t) { - LOG.debug("JVM Runtime does not support Modules"); + LOG.debug("This JVM Runtime does not support Modules, disabling Jetty internal support"); } - MODULE_LOCATION = moduleLocation; + MODULE_LOCATION = moduleFunc; locationMethods.add(lookup.findStatic(TypeUtil.class, "getClassLoaderLocation", type)); locationMethods.add(lookup.findStatic(TypeUtil.class, "getSystemClassLoaderLocation", type)); LOCATION_METHODS = locationMethods.toArray(new MethodHandle[0]); @@ -723,7 +730,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; } From a3a1478c91438de18003363973e7f374f0d09609 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 15 Mar 2021 09:05:25 -0500 Subject: [PATCH 2/3] Issue #6052 - Removing MethodHandle from TypeUtil static initializer Signed-off-by: Joakim Erdfelt --- .../java/org/eclipse/jetty/util/TypeUtil.java | 53 ++++++------------- 1 file changed, 17 insertions(+), 36 deletions(-) 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 655ad624b9c..450a09dd2ea 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; @@ -43,8 +40,6 @@ 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 @@ -178,44 +173,32 @@ public class TypeUtil } } - private static final MethodHandle[] LOCATION_METHODS; + 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_METHOD is important. + LOCATION_METHODS.add(TypeUtil::getCodeSourceLocation); + Function, URI> moduleFunc = null; try { - locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type)); - Function, URI> moduleFunc = null; - try + Class clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); + Object obj = clazzModuleLocation.getConstructor().newInstance(); + if (obj instanceof Function) { - Class clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); - Object obj = clazzModuleLocation.getConstructor().newInstance(); - if (obj instanceof Function) - { - //noinspection unchecked - moduleFunc = (Function, URI>)obj; - locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type)); - } + //noinspection unchecked + moduleFunc = (Function, URI>)obj; + LOCATION_METHODS.add(moduleFunc); } - catch (Throwable t) - { - LOG.debug("This JVM Runtime does not support Modules, disabling Jetty internal support"); - } - MODULE_LOCATION = moduleFunc; - 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); } /** @@ -634,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; From 9e29832a8a0ec410f55dad1922d465bf4dfa706b Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 15 Mar 2021 09:22:04 -0500 Subject: [PATCH 3/3] Issue #6052 - Applying changes from review Signed-off-by: Joakim Erdfelt --- jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 450a09dd2ea..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 @@ -178,12 +178,12 @@ public class TypeUtil static { - // Lookup order in LOCATION_METHOD is important. + // Lookup order in LOCATION_METHODS is important. LOCATION_METHODS.add(TypeUtil::getCodeSourceLocation); Function, URI> moduleFunc = null; try { - Class clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); + Class clazzModuleLocation = TypeUtil.class.getClassLoader().loadClass(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); Object obj = clazzModuleLocation.getConstructor().newInstance(); if (obj instanceof Function) {