Skip to content

Commit

Permalink
Fix Java 21 version detection for JreCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Feb 21, 2025
1 parent 1644abd commit 9e16797
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 11 additions & 4 deletions java/org/apache/tomcat/util/compat/Jre21Compat.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Jre21Compat extends Jre19Compat {
private static final Log log = LogFactory.getLog(Jre21Compat.class);
private static final StringManager sm = StringManager.getManager(Jre21Compat.class);

private static final boolean supported;
private static final Method nameMethod;
private static final Method startMethod;
private static final Method ofVirtualMethod;
Expand All @@ -46,9 +47,14 @@ public class Jre21Compat extends Jre19Compat {
Method m4 = null;

try {
c1 = Class.forName("java.lang.Thread$Builder");
m1 = c1.getMethod("name", String.class, long.class);
m2 = c1.getMethod("start", Runnable.class);
// Note: Virtual threads is the main new feature in Java 21, but it was previously
// present as a preview. As a result, it is more accurate to test for another
// new class
c1 = Class.forName("java.util.SequencedCollection");

Class<?> c2 = Class.forName("java.lang.Thread$Builder");
m1 = c2.getMethod("name", String.class, long.class);
m2 = c2.getMethod("start", Runnable.class);
m3 = Thread.class.getMethod("ofVirtual", (Class<?>[]) null);
m4 = Subject.class.getMethod("callAs", Subject.class, Callable.class);
} catch (ClassNotFoundException e) {
Expand All @@ -58,14 +64,15 @@ public class Jre21Compat extends Jre19Compat {
// Should never happen
log.error(sm.getString("jre21Compat.unexpected"), e);
}
supported = (c1 != null);
nameMethod = m1;
startMethod = m2;
ofVirtualMethod = m3;
callAsMethod = m4;
}

static boolean isSupported() {
return ofVirtualMethod != null;
return supported;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
Improve the checks for exposure to and protection against CVE-2024-56337
so that reflection is not used unless required. (markt)
</fix>
<fix>
Fix a bug in the JRE compatibility detection that incorrectly identified
Java 19 and Java 20 as supporting Java 21 features. (markt)
</fix>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit 9e16797

Please # to comment.