This is an automated email from the ASF dual-hosted git repository.

alien11689 pushed a commit to branch ARIES-2250-fix-jndi-on-new-java8
in repository https://gitbox.apache.org/repos/asf/aries.git

commit 210ece06a3750cb71d4807e0e1ae9fff64bcd24d
Author: Dominik Przybysz <[email protected]>
AuthorDate: Thu Nov 5 23:25:45 2026 +0100

    ARIES-2250: Fix JNDI on new Java 8 failing on reflection on NamingManager
---
 .../org/apache/aries/jndi/startup/Activator.java   | 43 +++++++++++++++++++---
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git 
a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java 
b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
index 93393a853..59b52ff77 100644
--- a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
+++ b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
@@ -271,19 +271,38 @@ public class Activator implements BundleActivator {
         return "";
     }
 
+    private static class Swapped<T> {
+        boolean swapped;
+        T value;
+
+        Swapped(boolean swapped, T value) {
+            this.swapped = swapped;
+            this.value = value;
+        }
+    }
     /*
      * There are no public API to reset the InitialContextFactoryBuilder or
      * ObjectFactoryBuilder on the NamingManager so try to use reflection.
      */
     private static <T> T swapStaticField(Class<T> expectedType, Object value) 
throws IllegalStateException {
         try {
-            for (Field field : NamingManager.class.getDeclaredFields()) {
-                if (expectedType.equals(field.getType())) {
-                    field.setAccessible(true);
-                    T original = expectedType.cast(field.get(null));
-                    field.set(null, value);
-                    return original;
+            Swapped<T> swappedInNamingManager = 
swapStaticFieldInClass(expectedType, value, NamingManager.class);
+            if (swappedInNamingManager.swapped){
+                return swappedInNamingManager.value;
+            }
+            // NamingManagerHelper is not available on older Java 8 versions - 
available e.g. in Temurin 8u512 after JDK-8290368
+            try {
+                Class<?> helperClass = Class.forName(
+                        "com.sun.naming.internal.NamingManagerHelper",
+                        false,
+                        NamingManager.class.getClassLoader());
+                Swapped<T> swappedInHelper = 
swapStaticFieldInClass(expectedType, value, helperClass);
+                if (swappedInHelper.swapped){
+                    return swappedInHelper.value;
                 }
+            } catch (ClassNotFoundException e) {
+                throw new IllegalStateException(
+                        "Error setting field: no field found for type " + 
expectedType);
             }
         } catch (Throwable t) {
             // Ignore
@@ -293,6 +312,18 @@ public class Activator implements BundleActivator {
         throw new IllegalStateException("Error setting field: no field found 
for type " + expectedType);
     }
 
+    private static <T> Swapped<T> swapStaticFieldInClass(Class<T> 
expectedType, Object value, Class<?> targetClass) throws IllegalAccessException 
{
+        for (Field field : targetClass.getDeclaredFields()) {
+            if (expectedType.equals(field.getType())) {
+                field.setAccessible(true);
+                T original = expectedType.cast(field.get(null));
+                field.set(null, value);
+                return new Swapped<>(true, original);
+            }
+        }
+        return new Swapped<>(false, null);
+    }
+
     private static List<String> 
getInitialContextFactoryInterfaces(ServiceReference<InitialContextFactory> ref) 
{
         String[] interfaces = (String[]) 
ref.getProperty(Constants.OBJECTCLASS);
         List<String> resultList = new ArrayList<>();

Reply via email to