protected @Nullable X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) { // Attempt to get the trust manager from an OpenJDK socket factory. We attempt this on all // platforms in order to support Robolectric, which mixes classes from both Android and the // Oracle JDK. Note that we don't support HTTP/2 or other nice features on Robolectric. try { Class<?> sslContextClass = Class.forName("sun.security.ssl.SSLContextImpl"); Object context = readFieldOrNull(sslSocketFactory, sslContextClass, "context"); if (context == null) return null; return readFieldOrNull(context, X509TrustManager.class, "trustManager"); } catch (ClassNotFoundException e) { return null; } }
static @Nullable <T> T readFieldOrNull(Object instance, Class<T> fieldType, String fieldName) { for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) { try { Field field = c.getDeclaredField(fieldName); field.setAccessible(true); Object value = field.get(instance); if (!fieldType.isInstance(value)) return null; return fieldType.cast(value); } catch (NoSuchFieldException ignored) { } catch (IllegalAccessException e) { throw new AssertionError(); } } // Didn't find the field we wanted. As a last gasp attempt, try to find the value on a delegate. if (!fieldName.equals("delegate")) { Object delegate = readFieldOrNull(instance, Object.class, "delegate"); if (delegate != null) return readFieldOrNull(delegate, fieldType, fieldName); } return null; }
protected @Nullable X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) { // Attempt to get the trust manager from an OpenJDK socket factory. We attempt this on all // platforms in order to support Robolectric, which mixes classes from both Android and the // Oracle JDK. Note that we don't support HTTP/2 or other nice features on Robolectric. try { Class<?> sslContextClass = Class.forName("sun.security.ssl.SSLContextImpl"); Object context = readFieldOrNull(sslSocketFactory, sslContextClass, "context"); if (context == null) return null; return readFieldOrNull(context, X509TrustManager.class, "trustManager"); } catch (ClassNotFoundException e) { return null; } }
static @Nullable <T> T readFieldOrNull(Object instance, Class<T> fieldType, String fieldName) { for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) { try { Field field = c.getDeclaredField(fieldName); field.setAccessible(true); Object value = field.get(instance); if (!fieldType.isInstance(value)) return null; return fieldType.cast(value); } catch (NoSuchFieldException ignored) { } catch (IllegalAccessException e) { throw new AssertionError(); } } // Didn't find the field we wanted. As a last gasp attempt, try to find the value on a delegate. if (!fieldName.equals("delegate")) { Object delegate = readFieldOrNull(instance, Object.class, "delegate"); if (delegate != null) return readFieldOrNull(delegate, fieldType, fieldName); } return null; }
protected X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) { // Attempt to get the trust manager from an OpenJDK socket factory. We attempt this on all // platforms in order to support Robolectric, which mixes classes from both Android and the // Oracle JDK. Note that we don't support HTTP/2 or other nice features on Robolectric. try { Class<?> sslContextClass = Class.forName("sun.security.ssl.SSLContextImpl"); Object context = readFieldOrNull(sslSocketFactory, sslContextClass, "context"); if (context == null) return null; return readFieldOrNull(context, X509TrustManager.class, "trustManager"); } catch (ClassNotFoundException e) { return null; } }
protected @Nullable X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) { // Attempt to get the trust manager from an OpenJDK socket factory. We attempt this on all // platforms in order to support Robolectric, which mixes classes from both Android and the // Oracle JDK. Note that we don't support HTTP/2 or other nice features on Robolectric. try { Class<?> sslContextClass = Class.forName("sun.security.ssl.SSLContextImpl"); Object context = readFieldOrNull(sslSocketFactory, sslContextClass, "context"); if (context == null) return null; return readFieldOrNull(context, X509TrustManager.class, "trustManager"); } catch (ClassNotFoundException e) { return null; } }
static <T> T readFieldOrNull(Object instance, Class<T> fieldType, String fieldName) { for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) { try { Field field = c.getDeclaredField(fieldName); field.setAccessible(true); Object value = field.get(instance); if (value == null || !fieldType.isInstance(value)) return null; return fieldType.cast(value); } catch (NoSuchFieldException ignored) { } catch (IllegalAccessException e) { throw new AssertionError(); } } // Didn't find the field we wanted. As a last gasp attempt, try to find the value on a delegate. if (!fieldName.equals("delegate")) { Object delegate = readFieldOrNull(instance, Object.class, "delegate"); if (delegate != null) return readFieldOrNull(delegate, fieldType, fieldName); } return null; }
static @Nullable <T> T readFieldOrNull(Object instance, Class<T> fieldType, String fieldName) { for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) { try { Field field = c.getDeclaredField(fieldName); field.setAccessible(true); Object value = field.get(instance); if (value == null || !fieldType.isInstance(value)) return null; return fieldType.cast(value); } catch (NoSuchFieldException ignored) { } catch (IllegalAccessException e) { throw new AssertionError(); } } // Didn't find the field we wanted. As a last gasp attempt, try to find the value on a delegate. if (!fieldName.equals("delegate")) { Object delegate = readFieldOrNull(instance, Object.class, "delegate"); if (delegate != null) return readFieldOrNull(delegate, fieldType, fieldName); } return null; }