SLING-7320 guard against NPE happening when initReflectionFields fails
diff --git a/src/main/java/org/apache/sling/commons/threads/impl/ThreadLocalCleaner.java b/src/main/java/org/apache/sling/commons/threads/impl/ThreadLocalCleaner.java
index 98f264f..b755034 100644
--- a/src/main/java/org/apache/sling/commons/threads/impl/ThreadLocalCleaner.java
+++ b/src/main/java/org/apache/sling/commons/threads/impl/ThreadLocalCleaner.java
@@ -31,7 +31,7 @@
/* Reflection fields */
/** this field is in class {@link ThreadLocal} and is of type {@code ThreadLocal.ThreadLocalMap} */
- private static Field threadLocalsField;
+ private static volatile Field threadLocalsField;
/** this field is in class {@link ThreadLocal} and is of type {@code ThreadLocal.ThreadLocalMap} */
private static Field inheritableThreadLocalsField;
private static Class<?> threadLocalMapClass;
@@ -45,11 +45,11 @@
private static Field threadLocalMapSizeField;
/** this field is in the class {@code ThreadLocal.ThreadLocalMap} and next resize threshold */
private static Field threadLocalMapThresholdField;
- private static IllegalStateException reflectionException;
+ private static volatile IllegalStateException reflectionException;
public ThreadLocalCleaner(ThreadLocalChangeListener listener) {
- if (threadLocalsField == null) {
+ if (threadLocalsField == null || reflectionException != null) {
initReflectionFields();
}
this.listener = listener;
@@ -75,7 +75,7 @@
} catch (NoSuchFieldException e) {
reflectionException = new IllegalStateException(
"Could not locate threadLocals field in class Thread. " +
- "Will not be able to clear thread locals: " + e);
+ "Will not be able to clear thread locals: " + e, e);
throw reflectionException;
}
}