public static void addSuppressedIfPossible(Throwable e, Throwable suppressed) { if (addSuppressedExceptionMethod != null) { try { addSuppressedExceptionMethod.invoke(e, suppressed); } catch (InvocationTargetException | IllegalAccessException ignored) { } } }
boolean warnIfOpen(Object closeGuardInstance) { boolean reported = false; if (closeGuardInstance != null) { try { warnIfOpenMethod.invoke(closeGuardInstance); reported = true; } catch (Exception ignored) { } } return reported; }
Method method = targetClass.getDeclaredMethod(methodName, argClasses); method.setAccessible(true); return method.invoke(targetObject, argObjects);
@Override public void afterHandshake(SSLSocket sslSocket) { try { removeMethod.invoke(null, sslSocket); } catch (IllegalAccessException | InvocationTargetException e) { throw new AssertionError("failed to remove ALPN", e); } }
@Override public @Nullable String getSelectedProtocol(SSLSocket socket) { try { byte[] alpnResult = (byte[]) getAlpnSelectedProtocol.invoke(socket); return alpnResult != null ? new String(alpnResult, UTF_8) : null; } catch (IllegalAccessException | InvocationTargetException e) { throw new AssertionError(e); } }
@Override String typeName(Type type) { try { Method getTypeName = Type.class.getMethod("getTypeName"); return (String) getTypeName.invoke(type); } catch (NoSuchMethodException e) { throw new AssertionError("Type.getTypeName should be available in Java 8"); } catch (InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } } },
private Object invokeGeneratorMethod(Method generator, Object... args) { try { return generator.invoke(this, args); } catch (InvocationTargetException e) { throwIfUnchecked(e.getCause()); throw new RuntimeException(e.getCause()); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } }
private boolean api24IsCleartextTrafficPermitted(String hostname, Class<?> networkPolicyClass, Object networkSecurityPolicy) throws InvocationTargetException, IllegalAccessException { try { Method isCleartextTrafficPermittedMethod = networkPolicyClass .getMethod("isCleartextTrafficPermitted", String.class); return (boolean) isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy, hostname); } catch (NoSuchMethodException e) { return api23IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy); } }
private boolean api23IsCleartextTrafficPermitted(String hostname, Class<?> networkPolicyClass, Object networkSecurityPolicy) throws InvocationTargetException, IllegalAccessException { try { Method isCleartextTrafficPermittedMethod = networkPolicyClass .getMethod("isCleartextTrafficPermitted"); return (boolean) isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy); } catch (NoSuchMethodException e) { return super.isCleartextTrafficPermitted(hostname); } }
@Override final Object invokeInternal(@Nullable Object receiver, Object[] args) throws InvocationTargetException, IllegalAccessException { return method.invoke(receiver, args); }
@GwtIncompatible // java.lang.reflect private static Object invokeAccessibleNonThrowingMethod( Method method, Object receiver, Object... params) { try { return method.invoke(receiver, params); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw propagate(e.getCause()); } }
@Override public boolean isCleartextTrafficPermitted(String hostname) { try { Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy"); Method getInstanceMethod = networkPolicyClass.getMethod("getInstance"); Object networkSecurityPolicy = getInstanceMethod.invoke(null); return api24IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy); } catch (ClassNotFoundException | NoSuchMethodException e) { return super.isCleartextTrafficPermitted(hostname); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new AssertionError("unable to determine cleartext support", e); } }
@Override public void configureTlsExtensions( SSLSocket sslSocket, String hostname, List<Protocol> protocols) { List<String> names = alpnProtocolNames(protocols); try { Object alpnProvider = Proxy.newProxyInstance(Platform.class.getClassLoader(), new Class[] {clientProviderClass, serverProviderClass}, new AlpnProvider(names)); putMethod.invoke(null, sslSocket, alpnProvider); } catch (InvocationTargetException | IllegalAccessException e) { throw new AssertionError("failed to set ALPN", e); } }
private Throwable tryInternalFastPathGetFailure(Future<?> future) throws Exception { Method tryInternalFastPathGetFailureMethod = abstractFutureClass.getDeclaredMethod("tryInternalFastPathGetFailure"); tryInternalFastPathGetFailureMethod.setAccessible(true); return (Throwable) tryInternalFastPathGetFailureMethod.invoke(future); } }
private void runTestMethod(ClassLoader classLoader) throws Exception { Class<?> test = classLoader.loadClass(FuturesTest.class.getName()); Object testInstance = test.newInstance(); test.getMethod("setUp").invoke(testInstance); test.getMethod(getName()).invoke(testInstance); test.getMethod("tearDown").invoke(testInstance); }
@Override public Object invoke(Object proxy, Method method, @Nullable Object[] args) throws Throwable { // If the method is a method from Object then defer to normal invocation. if (method.getDeclaringClass() == Object.class) { return method.invoke(this, args); } if (platform.isDefaultMethod(method)) { return platform.invokeDefaultMethod(method, service, proxy, args); } return loadServiceMethod(method).invoke(args != null ? args : emptyArgs); } });
private static void doTestMocking(RateLimiter mock) throws Exception { for (Method method : RateLimiter.class.getMethods()) { if (!isStatic(method.getModifiers()) && !NOT_WORKING_ON_MOCKS.contains(method.getName()) && !method.getDeclaringClass().equals(Object.class)) { method.invoke(mock, arbitraryParameters(method)); } } }
private Object invokeListMethod(Method method, Object[] args) throws Throwable { try { Object returnValue = method.invoke(delegate, args); mutateDelegate(); return returnValue; } catch (InvocationTargetException e) { throw e.getCause(); } catch (IllegalAccessException e) { throw new AssertionError(e); } }
@AndroidIncompatible // no FpUtils and no Math.nextDown in old versions public void testNextDown() throws Exception { Method jdkNextDown = getJdkNextDown(); for (double d : FINITE_DOUBLE_CANDIDATES) { assertEquals(jdkNextDown.invoke(null, d), DoubleUtils.nextDown(d)); } }