Skip to content

Commit

Permalink
Issue #6052 - Removing MethodHandle from TypeUtil static initializer
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Mar 15, 2021
1 parent d7982f8 commit a3a1478
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
Expand Up @@ -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;
Expand All @@ -43,8 +40,6 @@
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
Expand Down Expand Up @@ -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;

static
{
List<MethodHandle> 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<Class<?>, URI> moduleFunc = null;
try
{
locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type));
Function<Class<?>, URI> moduleFunc = null;
try
{
Class<?> clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation");
Object obj = clazzModuleLocation.getConstructor().newInstance();
if (obj instanceof Function)
{
//noinspection unchecked
moduleFunc = (Function<Class<?>, URI>)obj;
locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type));
}
}
catch (Throwable t)
Class<?> clazzModuleLocation = Class.forName(TypeUtil.class.getPackage().getName() + ".ModuleLocation");
Object obj = clazzModuleLocation.getConstructor().newInstance();
if (obj instanceof Function)
{
LOG.debug("This JVM Runtime does not support Modules, disabling Jetty internal support");
//noinspection unchecked
moduleFunc = (Function<Class<?>, URI>)obj;
LOCATION_METHODS.add(moduleFunc);
}
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);
}

/**
Expand Down Expand Up @@ -634,13 +617,11 @@ public static boolean isFalse(Object o)
*/
public static URI getLocationOfClass(Class<?> clazz)
{
URI location;

for (MethodHandle locationMethod : LOCATION_METHODS)
for (Function<Class<?>, URI> locationFunction : LOCATION_METHODS)
{
try
{
location = (URI)locationMethod.invoke(clazz);
URI location = locationFunction.apply(clazz);
if (location != null)
{
return location;
Expand Down

0 comments on commit a3a1478

Please sign in to comment.