From 8485eeeda39f4e6e83e372f8e853d92604740ab4 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 19 Apr 2021 10:29:44 +0300 Subject: [PATCH] Add isParentFirst method to QuarkusClassLoader This can be used by extensions to determine if a class would be loaded parent first or not. Relates to: https://github.com/quarkusio/quarkus/issues/16576#issuecomment-822233804 --- .../classloading/QuarkusClassLoader.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java index 416b6c684f1a4..61e3880abdf68 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java @@ -117,6 +117,32 @@ private String sanitizeName(String name) { return name; } + /** + * Returns true if the supplied class is a class that would be loaded parent-first + */ + public boolean isParentFirst(String name) { + if (name.startsWith(JAVA)) { + return true; + } + + //even if the thread is interrupted we still want to be able to load classes + //if the interrupt bit is set then we clear it and restore it at the end + boolean interrupted = Thread.interrupted(); + try { + ClassLoaderState state = getState(); + synchronized (getClassLoadingLock(name)) { + String resourceName = sanitizeName(name).replace(".", "/") + ".class"; + return parentFirst(resourceName, state); + } + + } finally { + if (interrupted) { + //restore interrupt state + Thread.currentThread().interrupt(); + } + } + } + private boolean parentFirst(String name, ClassLoaderState state) { return parentFirst || state.parentFirstResources.contains(name); }