Merge pull request #6053 from eclipse/jetty-9.4.x-client-hybrid-smaller

Issue #6052 - make ModuleLocation optional on Android
This commit is contained in:
Joakim Erdfelt 2021-03-15 11:58:51 -05:00 committed by GitHub
commit f92394e748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 34 deletions

View File

@ -51,6 +51,7 @@
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>true</shadedArtifactAttached> <shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>hybrid</shadedClassifierName> <shadedClassifierName>hybrid</shadedClassifierName>
<artifactSet> <artifactSet>

View File

@ -23,6 +23,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URI; import java.net.URI;
import java.util.Optional; import java.util.Optional;
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;
@ -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() * 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); 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 try
{ {

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;
@ -38,12 +35,11 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
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
@ -177,38 +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 ModuleLocation MODULE_LOCATION; private static final Function<Class<?>, URI> MODULE_LOCATION;
static static
{ {
List<MethodHandle> locationMethods = new ArrayList<>(); // Lookup order in LOCATION_METHODS 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 = TypeUtil.class.getClassLoader().loadClass(TypeUtil.class.getPackage().getName() + ".ModuleLocation");
ModuleLocation moduleLocation = null; Object obj = clazzModuleLocation.getConstructor().newInstance();
try if (obj instanceof Function)
{ {
moduleLocation = new ModuleLocation(); //noinspection unchecked
locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type)); 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);
} }
/** /**
@ -627,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;
@ -723,7 +711,7 @@ public class TypeUtil
// In Jetty 10, this method can be implemented directly, without reflection // In Jetty 10, this method can be implemented directly, without reflection
if (MODULE_LOCATION != null) if (MODULE_LOCATION != null)
{ {
return MODULE_LOCATION.getModuleLocation(clazz); return MODULE_LOCATION.apply(clazz);
} }
return null; return null;
} }