java.lang.Class.getDeclaredMethods()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(311)

本文整理了Java中java.lang.Class.getDeclaredMethods()方法的一些代码示例,展示了Class.getDeclaredMethods()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getDeclaredMethods()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getDeclaredMethods

Class.getDeclaredMethods介绍

[英]Returns an array containing Method objects for all methods declared in the class represented by this Class. If there are no methods or if this Class represents an array class, a primitive type or void then an empty array is returned.
[中]返回一个数组,该数组包含该类表示的类中声明的所有方法的方法对象。如果没有方法,或者该类表示数组类、基元类型或void,则返回空数组。

代码示例

代码示例来源:origin: spring-projects/spring-framework

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];
}

代码示例来源:origin: spring-projects/spring-framework

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;
  }
});

代码示例来源:origin: apache/incubator-dubbo

/**
 * 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;
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * 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;
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * 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;
}

代码示例来源:origin: libgdx/libgdx

/** 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;
}

代码示例来源:origin: square/retrofit

private void eagerlyValidateMethods(Class<?> service) {
 Platform platform = Platform.get();
 for (Method method : service.getDeclaredMethods()) {
  if (!platform.isDefaultMethod(method) && !Modifier.isStatic(method.getModifiers())) {
   loadServiceMethod(method);
  }
 }
}

代码示例来源:origin: google/guava

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());
   }
  }
 }
}

代码示例来源:origin: google/guava

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);
 }
}

代码示例来源:origin: google/guava

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;
}

代码示例来源:origin: google/guava

/**
 * 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");
}

代码示例来源:origin: google/guava

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;
}

代码示例来源:origin: google/guava

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;
}

代码示例来源:origin: spring-projects/spring-framework

@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]));
    }
  }
}

代码示例来源:origin: google/guava

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);
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

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);
  }
}

代码示例来源:origin: google/guava

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());
  }
 }
}

代码示例来源:origin: google/guava

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());
 }
}

代码示例来源:origin: google/guava

public void testNulls() {
 for (Method method : ParameterTest.class.getDeclaredMethods()) {
  for (Parameter param : Invokable.from(method).getParameters()) {
   new NullPointerTester().testAllPublicInstanceMethods(param);
  }
 }
}

代码示例来源:origin: google/guava

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();
}

相关文章

Class类方法