Skip to content

Commit

Permalink
Issue #6052 - make ModuleLocation optional on Android
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 10, 2021
1 parent b899261 commit d7982f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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
23 changes: 15 additions & 8 deletions jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
Expand Up @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -178,7 +179,7 @@ public class TypeUtil
}

private static final MethodHandle[] LOCATION_METHODS;
private static final ModuleLocation MODULE_LOCATION;
private static final Function<Class<?>, URI> MODULE_LOCATION;

static
{
Expand All @@ -190,17 +191,23 @@ public class TypeUtil
try
{
locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type));
ModuleLocation moduleLocation = null;
Function<Class<?>, 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<Class<?>, 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]);
Expand Down Expand Up @@ -723,7 +730,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

0 comments on commit d7982f8

Please sign in to comment.