From 5eb3e73400b25d8ba654bf53e0085532206877e2 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 18 Jun 2019 11:03:06 -0500 Subject: [PATCH] Issue #3700 - Fixing TypeUtil and IncludeExcludeSet to work with null location + TypeUtil.getLocationOfClass() respects Class.getClassLoader() of null which means a class belonging to Boot ClassLoader, a Primitive, Void, or a dynamic in-memory class. Using system classloader is incorrect and invalid in Java 9+ + Fixing IncludeExcludeSet.test() to always return TRUE or FALSE never null. Signed-off-by: Joakim Erdfelt --- .../eclipse/jetty/util/IncludeExcludeSet.java | 4 ++-- .../java/org/eclipse/jetty/util/TypeUtil.java | 10 +++++--- .../org/eclipse/jetty/util/TypeUtilTest.java | 24 +++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java index 3c93baeb645..a6e29c5787f 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java @@ -169,7 +169,7 @@ public class IncludeExcludeSet implements Predicate

/** * Test Included and not Excluded * @param item The item to test - * @return Boolean.TRUE if item is included, Boolean.FALSE if item is excluded and null if neither + * @return Boolean.TRUE if item is included, Boolean.FALSE if item is excluded or neither */ public Boolean isIncludedAndNotExcluded(P item) { @@ -178,7 +178,7 @@ public class IncludeExcludeSet implements Predicate

if (_includePredicate.test(item)) return Boolean.TRUE; - return null; + return Boolean.FALSE; } public boolean hasIncludes() diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java index cb675f97cf5..eb63731bd18 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java @@ -562,10 +562,14 @@ public class TypeUtil String resourceName = clazz.getName().replace('.', '/') + ".class"; ClassLoader loader = clazz.getClassLoader(); - URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(resourceName); - if (url != null) + // null means bootstrap classloader, primitive, void, or dynamic in-memory class + if (loader != null) { - return URIUtil.getJarSource(url.toURI()); + URL url = loader.getResource(resourceName); + if (url != null) + { + return URIUtil.getJarSource(url.toURI()); + } } } catch (URISyntaxException e) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java index 0e8a07cc8f3..96c4599c201 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java @@ -18,15 +18,11 @@ package org.eclipse.jetty.util; - import java.nio.file.Path; import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnJre; -import org.junit.jupiter.api.condition.EnabledOnJre; -import org.junit.jupiter.api.condition.JRE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; @@ -149,6 +145,7 @@ public class TypeUtilTest assertThat(TypeUtil.getLocationOfClass(TypeUtil.class).toASCIIString(),containsString("/classes/")); } + /* @Test @DisabledOnJre(JRE.JAVA_8) public void testGetLocation_JvmCore_JPMS() @@ -158,6 +155,15 @@ public class TypeUtilTest assertThat(TypeUtil.getLocationOfClass(String.class).toASCIIString(),containsString(expectedJavaBase)); } + @Test + @DisabledOnJre(JRE.JAVA_8) + public void testGetLocation_JavaLangThreadDeath_JPMS() + { + // Class from JVM core + String expectedJavaBase = "/java.base/"; + assertThat(TypeUtil.getLocationOfClass(java.lang.ThreadDeath.class).toASCIIString(),containsString(expectedJavaBase)); + } + @Test @EnabledOnJre(JRE.JAVA_8) public void testGetLocation_JvmCore_Java8RT() @@ -166,4 +172,14 @@ public class TypeUtilTest String expectedJavaBase = "/rt.jar"; assertThat(TypeUtil.getLocationOfClass(String.class).toASCIIString(),containsString(expectedJavaBase)); } + + @Test + @EnabledOnJre(JRE.JAVA_8) + public void testGetLocation_JavaLangThreadDeath_Java8RT() + { + // Class from JVM core + String expectedJavaBase = "/rt.jar"; + assertThat(TypeUtil.getLocationOfClass(java.lang.ThreadDeath.class).toASCIIString(),containsString(expectedJavaBase)); + } + */ }