Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #6052 - make ModuleLocation optional on Android #6053

Merged
merged 3 commits into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jetty-client/pom.xml
Expand Up @@ -51,6 +51,7 @@
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>hybrid</shadedClassifierName>
<artifactSet>
Expand Down
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -53,7 +54,7 @@
*
* 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<Class<?>, URI>
{
private static final Logger LOG = Log.getLogger(ModuleLocation.class);

Expand Down Expand Up @@ -100,7 +101,8 @@ public ModuleLocation()
}
}

public URI getModuleLocation(Class<?> clazz)
@Override
public URI apply(Class<?> clazz)
{
try
{
Expand Down
52 changes: 20 additions & 32 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 @@ -38,12 +35,11 @@
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
Expand Down Expand Up @@ -177,38 +173,32 @@ public class TypeUtil
}
}

private static final MethodHandle[] LOCATION_METHODS;
private static final ModuleLocation MODULE_LOCATION;
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_METHODS is important.
LOCATION_METHODS.add(TypeUtil::getCodeSourceLocation);
Function<Class<?>, 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<Class<?>, 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);
}

/**
Expand Down Expand Up @@ -627,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 Expand Up @@ -723,7 +711,7 @@ public static URI getModuleLocation(Class<?> clazz)
// 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;
}
Expand Down