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

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

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

Class.getMethods介绍

[英]Returns an array containing Method objects for all public methods for the class C represented by this Class. Methods may be declared in C, the interfaces it implements or in the superclasses of C. The elements in the returned array are in no particular order.

If there are no public methods or if this Class represents a primitive type or void then an empty array is returned.
[中]返回一个数组,该数组包含该类表示的类C的所有公共方法的方法对象。方法可以在C、它实现的接口或C的超类中声明。返回数组中的元素没有特定顺序。
如果没有公共方法,或者如果此类表示基元类型或void,则返回空数组。

代码示例

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

/**
 * Return the set of methods for this type. The default implementation returns the
 * result of {@link Class#getMethods()} for the given {@code type}, but subclasses
 * may override in order to alter the results, e.g. specifying static methods
 * declared elsewhere.
 * @param type the class for which to return the methods
 * @since 3.1.1
 */
protected Method[] getMethods(Class<?> type) {
  return type.getMethods();
}

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

/**
 * Return class methods ordered with non-bridge methods appearing higher.
 */
private Method[] getSortedMethods(Class<?> clazz) {
  return this.sortedMethodsCache.computeIfAbsent(clazz, key -> {
    Method[] methods = key.getMethods();
    Arrays.sort(methods, (o1, o2) -> (o1.isBridge() == o2.isBridge() ? 0 : (o1.isBridge() ? 1 : -1)));
    return methods;
  });
}

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

/** Returns the most concrete public methods from {@code type}. */
private static Method[] getMostConcreteMethods(Class<?> type) {
 Method[] methods = type.getMethods();
 for (int i = 0; i < methods.length; i++) {
  try {
   methods[i] = type.getMethod(methods[i].getName(), methods[i].getParameterTypes());
  } catch (Exception e) {
   throwIfUnchecked(e);
   throw new RuntimeException(e);
  }
 }
 return methods;
}

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

private static Set<Method> findInterruptibleMethods(Class<?> interfaceType) {
 Set<Method> set = Sets.newHashSet();
 for (Method m : interfaceType.getMethods()) {
  if (declaresInterruptedEx(m)) {
   set.add(m);
  }
 }
 return set;
}

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

/**
 * Return whether the given bean class declares or inherits any non-void
 * returning bean property or indexed property setter methods.
 */
private boolean supports(Class<?> beanClass) {
  for (Method method : beanClass.getMethods()) {
    if (ExtendedBeanInfo.isCandidateWriteMethod(method)) {
      return true;
    }
  }
  return false;
}

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

@Nullable
private Method findDestroyMethod(String name) {
  return (this.nonPublicAccessAllowed ?
      BeanUtils.findMethodWithMinimalParameters(this.bean.getClass(), name) :
      BeanUtils.findMethodWithMinimalParameters(this.bean.getClass().getMethods(), name));
}

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

private static Method findMethodWithReturnType(String name, Class<?> returnType, Class<SettingsDaoImpl> targetType) {
  Method[] methods = targetType.getMethods();
  for (Method m : methods) {
    if (m.getName().equals(name) && m.getReturnType().equals(returnType)) {
      return m;
    }
  }
  return null;
}

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

private static Method getMethodForReturnType(Class<?> returnType) {
  return Arrays.stream(TestBean.class.getMethods())
      .filter(method -> method.getReturnType().equals(returnType))
      .findFirst()
      .orElseThrow(() ->
          new IllegalArgumentException("Unique return type not found: " + returnType));
}

代码示例来源:origin: ReactiveX/RxJava

static void scan(Class<?> clazz) {
  for (Method m : clazz.getMethods()) {
    if (m.getDeclaringClass() == clazz) {
      if ((m.getModifiers() & Modifier.STATIC) == 0) {
        if ((m.getModifiers() & (Modifier.PUBLIC | Modifier.FINAL)) == Modifier.PUBLIC) {
          fail("Not final: " + m);
        }
      }
    }
  }
}

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

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

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

@GwtIncompatible // reflection
public void testAsMapBridgeMethods() {
 for (Method m : TreeMultimap.class.getMethods()) {
  if (m.getName().equals("asMap") && m.getReturnType().equals(SortedMap.class)) {
   return;
  }
 }
}

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

public void testReentrantReadWriteLock_implDoesNotExposeShadowedLocks() {
 assertEquals(
   "Unexpected number of public methods in ReentrantReadWriteLock. "
     + "The correctness of CycleDetectingReentrantReadWriteLock depends on "
     + "the fact that the shadowed ReadLock and WriteLock are never used or "
     + "exposed by the superclass implementation. If the implementation has "
     + "changed, the code must be re-inspected to ensure that the "
     + "assumption is still valid.",
   24,
   ReentrantReadWriteLock.class.getMethods().length);
}

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

protected Method getMethod(String name) {
  // Assumes no overloading of test methods...
  Method[] candidates = getClass().getMethods();
  for (Method candidate : candidates) {
    if (candidate.getName().equals(name)) {
      return candidate;
    }
  }
  fail("Bad test specification, no method '" + name + "' found in test class");
  return null;
}

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

@Before
public void setup() throws NoSuchMethodException {
  getAge = TestBean.class.getMethod("getAge");
  // Assumes no overloading
  for (Method method : HasGeneric.class.getMethods()) {
    methodsOnHasGeneric.put(method.getName(), method);
  }
}

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

@GwtIncompatible // reflection
public void testKeySetBridgeMethods() {
 for (Method m : TreeMultimap.class.getMethods()) {
  if (m.getName().equals("keySet") && m.getReturnType().equals(SortedSet.class)) {
   return;
  }
 }
 fail("No bridge method found");
}

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

@GwtIncompatible // reflection
 public void testGetBridgeMethods() {
  for (Method m : TreeMultimap.class.getMethods()) {
   if (m.getName().equals("get") && m.getReturnType().equals(SortedSet.class)) {
    return;
   }
  }
  fail("No bridge method found");
 }
}

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

@GwtIncompatible // reflection
 @AndroidIncompatible // Reflection bug, or actual binary compatibility problem?
 public void testElementSetBridgeMethods() {
  for (Method m : TreeMultiset.class.getMethods()) {
   if (m.getName().equals("elementSet") && m.getReturnType().equals(SortedSet.class)) {
    return;
   }
  }
  fail("No bridge method found");
 }
}

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

@Test
public void testOnAllMethods() throws Exception {
  Method[] methods = StringList.class.getMethods();
  for (Method method : methods) {
    assertNotNull(BridgeMethodResolver.findBridgedMethod(method));
  }
}

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

@Before
public void setUp() throws Exception {
  this.comparator = new AspectJPrecedenceComparator();
  this.anyOldMethod = getClass().getMethods()[0];
  this.anyOldPointcut = new AspectJExpressionPointcut();
  this.anyOldPointcut.setExpression("execution(* *(..))");
}

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

public static TestSuite suite() {
 TestSuite suite = new TestSuite();
 Method[] methods = Monitor.class.getMethods();
 sortMethods(methods);
 for (Method method : methods) {
  if (isAnyEnter(method) || isWaitFor(method)) {
   validateMethod(method);
   addTests(suite, method);
  }
 }
 assertEquals(548, suite.testCount());
 return suite;
}

相关文章

Class类方法