private static boolean hasTest(Class<?> testClass, Iterable<String> testNames) { for (String testName : testNames) { try { testClass.getMethod(testName); return true; } catch (NoSuchMethodException e) { continue; } } return false; }
private static Method getAddSuppressed() { try { return Throwable.class.getMethod("addSuppressed", Throwable.class); } catch (Throwable e) { return null; } }
@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 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); } }
public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager trustManager) { try { Class<?> extensionsClass = Class.forName("android.net.http.X509TrustManagerExtensions"); Constructor<?> constructor = extensionsClass.getConstructor(X509TrustManager.class); Object extensions = constructor.newInstance(trustManager); Method checkServerTrusted = extensionsClass.getMethod( "checkServerTrusted", X509Certificate[].class, String.class, String.class); return new AndroidCertificateChainCleaner(extensions, checkServerTrusted); } catch (Exception e) { throw new AssertionError(e); } }
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); } }
@GwtIncompatible // reflection public static Method getMethod(Class<?> clazz, String name) { try { return clazz.getMethod(name); } catch (Exception e) { throw new IllegalArgumentException(e); } } }
@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); } }
ParamOverride(Class<?> clazz, int index, ParamMode mode, String name, Class<?>... arguments) { this.clazz = clazz; this.index = index; this.mode = mode; this.name = name; this.arguments = arguments; try { clazz.getMethod(name, arguments); } catch (Exception ex) { throw new AssertionError(ex); } }
private static Subscriber subscriber( EventBus bus, Object target, String methodName, Class<?> eventType) { try { return Subscriber.create(bus, target, target.getClass().getMethod(methodName, eventType)); } catch (NoSuchMethodException e) { throw new AssertionError(e); } }
public void testMethod_notDeclaredByType() throws NoSuchMethodException { Method sizeMethod = Map.class.getMethod("size"); try { TypeToken.of(List.class).method(sizeMethod); fail(); } catch (IllegalArgumentException expected) { } }
public void testResolveType() throws Exception { Method getFromList = List.class.getMethod("get", int.class); TypeToken<?> returnType = new TypeToken<List<String>>() {}.resolveType(getFromList.getGenericReturnType()); assertEquals(String.class, returnType.getType()); }
public void testMethod_declaredBySuperclass() throws Exception { Method toStringMethod = Object.class.getMethod("toString"); ImmutableList<String> list = ImmutableList.of("foo"); assertEquals(list.toString(), TypeToken.of(List.class).method(toStringMethod).invoke(list)); }
public <T extends Number & List<String>> void testMethod_returnType_resolvedAgainstTypeBound() throws NoSuchMethodException { Method getMethod = List.class.getMethod("get", int.class); Invokable<T, String> invokable = new TypeToken<T>(getClass()) {}.method(getMethod).returning(String.class); assertEquals(TypeToken.of(String.class), invokable.getReturnType()); }
public <T extends List<String>> void testMethod_parameterTypes() throws NoSuchMethodException { Method setMethod = List.class.getMethod("set", int.class, Object.class); Invokable<T, ?> invokable = new TypeToken<T>(getClass()) {}.method(setMethod); ImmutableList<Parameter> params = invokable.getParameters(); assertEquals(2, params.size()); assertEquals(TypeToken.of(int.class), params.get(0).getType()); assertEquals(TypeToken.of(String.class), params.get(1).getType()); }
public void testStaticOneArgMethodsThatShouldFail() throws Exception { for (String methodName : STATIC_ONE_ARG_METHODS_SHOULD_FAIL) { Method method = OneArg.class.getMethod(methodName, String.class); boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); } catch (AssertionFailedError expected) { foundProblem = true; } assertTrue("Should report error in method " + methodName, foundProblem); } }
public void testNonStaticOneArgMethodsThatShouldPass() throws Exception { OneArg foo = new OneArg(); for (String methodName : NONSTATIC_ONE_ARG_METHODS_SHOULD_PASS) { Method method = OneArg.class.getMethod(methodName, String.class); try { new NullPointerTester().testMethodParameter(foo, method, 0); } catch (AssertionFailedError unexpected) { fail("Should not have flagged method " + methodName); } } }
public void testTwoArgNullableNullable() throws Exception { Method method = TwoArg.class.getMethod("nullableNullable", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); verifyBarPass(method, bar); // All args nullable: anything goes! } } }
public void testMessageNoException() throws Exception { Method method = OneArg.class.getMethod("staticOneArgShouldThrowNpeButDoesnt", String.class); boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); } catch (AssertionFailedError expected) { assertThat(expected.getMessage()).contains("index 0"); assertThat(expected.getMessage()).contains("[null]"); foundProblem = true; } assertTrue("Should report error when no exception is thrown", foundProblem); }
@AndroidIncompatible // Android runs ExampleDerivedInterfaceTester directly if it exists public void testBuildTesterRequirements_methodClassConflict() throws Exception { final Method method = ExampleDerivedInterfaceTester.class.getMethod("testRequiringConflictingFeatures"); try { FeatureUtil.buildTesterRequirements(method); fail("Expected ConflictingRequirementsException"); } catch (ConflictingRequirementsException e) { assertThat(e.getConflicts()).contains(ExampleBaseFeature.BASE_FEATURE_1); assertEquals(method, e.getSource()); } }