public static Method findInterfaceMethod(Class iface) { if (!iface.isInterface()) { throw new IllegalArgumentException(iface + " is not an interface"); } Method[] methods = iface.getDeclaredMethods(); if (methods.length != 1) { throw new IllegalArgumentException("expecting exactly 1 method in " + iface); } return methods[0]; }
public Object run() throws Exception { Method[] methods = Object.class.getDeclaredMethods(); for (Method method : methods) { if ("finalize".equals(method.getName()) || (method.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) > 0) { continue; } OBJECT_METHODS.add(method); } return null; } });
/** * Returns the readResolve method */ protected Method getReadResolve(Class cl) { for (; cl != null; cl = cl.getSuperclass()) { Method[] methods = cl.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals("readResolve") && method.getParameterTypes().length == 0) return method; } } return null; }
/** * Returns the writeReplace method */ protected Method getWriteReplace(Class cl, Class param) { for (; cl != null; cl = cl.getSuperclass()) { for (Method method : cl.getDeclaredMethods()) { if (method.getName().equals("writeReplace") && method.getParameterTypes().length == 1 && param.equals(method.getParameterTypes()[0])) return method; } } return null; }
/** * Returns the writeReplace method */ protected Method getWriteReplace(Class cl, Class param) { for (; cl != null; cl = cl.getSuperclass()) { for (Method method : cl.getDeclaredMethods()) { if (method.getName().equals("writeReplace") && method.getParameterTypes().length == 1 && param.equals(method.getParameterTypes()[0])) return method; } } return null; }
/** Returns an array of {@link Method} containing the methods declared by the class represented by the supplied Class. */ static public Method[] getDeclaredMethods (Class c) { java.lang.reflect.Method[] methods = c.getDeclaredMethods(); Method[] result = new Method[methods.length]; for (int i = 0, j = methods.length; i < j; i++) { result[i] = new Method(methods[i]); } return result; }
private void eagerlyValidateMethods(Class<?> service) { Platform platform = Platform.get(); for (Method method : service.getDeclaredMethods()) { if (!platform.isDefaultMethod(method) && !Modifier.isStatic(method.getModifiers())) { loadServiceMethod(method); } } }
static void verifyConsitentRawType() { for (Method method : RawTypeConsistencyTester.class.getDeclaredMethods()) { assertEquals( method.getReturnType(), TypeToken.of(method.getGenericReturnType()).getRawType()); for (int i = 0; i < method.getParameterTypes().length; i++) { assertEquals( method.getParameterTypes()[i], TypeToken.of(method.getGenericParameterTypes()[i]).getRawType()); } } } }
static ImmutableList<Method> getTestMethods(Class<?> testClass) { List<Method> result = Lists.newArrayList(); for (Method method : testClass.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getReturnType() == void.class && method.getParameterTypes().length == 0 && method.getName().startsWith("test")) { result.add(method); } } return ImmutableList.copyOf(result); } }
private static Set<MethodSignature> getPublicStaticMethods(Class<?> clazz) { Set<MethodSignature> publicStaticMethods = newHashSet(); for (Method method : clazz.getDeclaredMethods()) { int modifiers = method.getModifiers(); if (isPublic(modifiers) && isStatic(modifiers)) { publicStaticMethods.add(new MethodSignature(method)); } } return publicStaticMethods; }
/** * Returns an object responsible for performing sanity tests against the return values of all * public static methods declared by {@code cls}, excluding superclasses. */ public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) { ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder(); for (Method method : cls.getDeclaredMethods()) { Invokable<?, ?> invokable = Invokable.from(method); invokable.setAccessible(true); if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) { builder.add(invokable); } } return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods"); }
public static TestSuite suite() { // we create a test suite containing a test for every AbstractFutureTest test method and we // set it as the name of the test. Then in runTest we can reflectively load and invoke the // corresponding method on AbstractFutureTest in the correct classloader. TestSuite suite = new TestSuite(AbstractFutureFallbackAtomicHelperTest.class.getName()); for (Method method : AbstractFutureTest.class.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("test")) { suite.addTest( TestSuite.createTest(AbstractFutureFallbackAtomicHelperTest.class, method.getName())); } } return suite; }
public static TestSuite suite() { // we create a test suite containing a test for every FuturesTest test method and we // set it as the name of the test. Then in runTest we can reflectively load and invoke the // corresponding method on FuturesTest in the correct classloader. TestSuite suite = new TestSuite(AggregateFutureStateFallbackAtomicHelperTest.class.getName()); for (Method method : FuturesTest.class.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("test")) { suite.addTest( TestSuite.createTest( AggregateFutureStateFallbackAtomicHelperTest.class, method.getName())); } } return suite; }
@Test public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception { Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class; Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class); ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class); List<Class<?>> exceptionTypes = Arrays.asList(annotation.value()); for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) { Class<?>[] paramTypes = method.getParameterTypes(); if (method.getName().startsWith("handle") && (paramTypes.length == 4)) { String name = paramTypes[0].getSimpleName(); assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0])); } } }
public void testAllHashFunctionsHaveKnownHashes() throws Exception { // The following legacy hashing function methods have been covered by unit testing already. List<String> legacyHashingMethodNames = ImmutableList.of("murmur2_64", "fprint96"); for (Method method : Hashing.class.getDeclaredMethods()) { if (method.getReturnType().equals(HashFunction.class) // must return HashFunction && Modifier.isPublic(method.getModifiers()) // only the public methods && method.getParameterTypes().length == 0 // only the seed-less grapes^W hash functions && !legacyHashingMethodNames.contains(method.getName())) { HashFunction hashFunction = (HashFunction) method.invoke(Hashing.class); assertTrue( "There should be at least 3 entries in KNOWN_HASHES for " + hashFunction, KNOWN_HASHES.row(hashFunction).size() >= 3); } } }
private void doTestHierarchyResolution(Class<?> clazz) throws Exception { for (Method method : clazz.getDeclaredMethods()){ Method bridged = BridgeMethodResolver.findBridgedMethod(method); Method expected = clazz.getMethod("test", FooEntity.class); assertEquals(expected, bridged); } }
static void assertSeedlessHashFunctionEquals(Class<?> clazz) throws Exception { for (Method method : clazz.getDeclaredMethods()) { if (method.getReturnType().equals(HashFunction.class) // must return HashFunction && Modifier.isPublic(method.getModifiers()) // only the public methods && method.getParameterTypes().length == 0) { // only the seed-less hash functions HashFunction hashFunction1a = (HashFunction) method.invoke(clazz); HashFunction hashFunction1b = (HashFunction) method.invoke(clazz); new EqualsTester().addEqualityGroup(hashFunction1a, hashFunction1b).testEquals(); // Make sure we're returning not only equal instances, but constants. assertSame(hashFunction1a, hashFunction1b); assertEquals(hashFunction1a.toString(), hashFunction1b.toString()); } } }
public PackageSanityTests() { setDefault(BaseEncoding.class, BaseEncoding.base64()); setDefault(int.class, 32); setDefault(String.class, "abcd"); setDefault(Method.class, AbstractPackageSanityTests.class.getDeclaredMethods()[0]); setDefault(MapMode.class, MapMode.READ_ONLY); setDefault(CharsetEncoder.class, Charsets.UTF_8.newEncoder()); } }
public void testNulls() { for (Method method : ParameterTest.class.getDeclaredMethods()) { for (Parameter param : Invokable.from(method).getParameters()) { new NullPointerTester().testAllPublicInstanceMethods(param); } } }
public void testEquals() { EqualsTester tester = new EqualsTester(); for (Method method : ParameterTest.class.getDeclaredMethods()) { for (Parameter param : Invokable.from(method).getParameters()) { tester.addEqualityGroup(param); } } tester.testEquals(); }