Issue #6052 - Removing MethodHandle from TypeUtil static initializer

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2021-03-15 09:05:25 -05:00
parent d7982f869b
commit a3a1478c91
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 17 additions and 36 deletions

View File

@ -19,9 +19,6 @@
package org.eclipse.jetty.util; package org.eclipse.jetty.util;
import java.io.IOException; 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.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; 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.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import static java.lang.invoke.MethodType.methodType;
/** /**
* TYPE Utilities. * TYPE Utilities.
* Provides various static utility methods for manipulating types and their * 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<Function<Class<?>, URI>> LOCATION_METHODS = new ArrayList<>();
private static final Function<Class<?>, URI> MODULE_LOCATION; private static final Function<Class<?>, URI> MODULE_LOCATION;
static static
{ {
List<MethodHandle> locationMethods = new ArrayList<>(); // Lookup order in LOCATION_METHOD is important.
LOCATION_METHODS.add(TypeUtil::getCodeSourceLocation);
MethodHandles.Lookup lookup = MethodHandles.lookup(); Function<Class<?>, URI> moduleFunc = null;
MethodType type = methodType(URI.class, Class.class);
try try
{ {
locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type)); Class<?> clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation");
Function<Class<?>, URI> moduleFunc = null; Object obj = clazzModuleLocation.getConstructor().newInstance();
try if (obj instanceof Function)
{ {
Class<?> clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation"); //noinspection unchecked
Object obj = clazzModuleLocation.getConstructor().newInstance(); moduleFunc = (Function<Class<?>, URI>)obj;
if (obj instanceof Function) LOCATION_METHODS.add(moduleFunc);
{
//noinspection unchecked
moduleFunc = (Function<Class<?>, URI>)obj;
locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type));
}
} }
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) public static URI getLocationOfClass(Class<?> clazz)
{ {
URI location; for (Function<Class<?>, URI> locationFunction : LOCATION_METHODS)
for (MethodHandle locationMethod : LOCATION_METHODS)
{ {
try try
{ {
location = (URI)locationMethod.invoke(clazz); URI location = locationFunction.apply(clazz);
if (location != null) if (location != null)
{ {
return location; return location;