java.lang.reflect.Method类的使用及代码示例

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

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

Method介绍

[英]A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).

A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.
[中]方法提供有关类或接口上单个方法的信息以及对该方法的访问。反射的方法可以是类方法或实例方法(包括抽象方法)。
方法允许在将要调用的实际参数与基础方法的形式参数相匹配时发生加宽转换,但如果发生缩小转换,则会抛出IllegalArgumentException。

代码示例

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

public static void addSuppressedIfPossible(Throwable e, Throwable suppressed) {
 if (addSuppressedExceptionMethod != null) {
  try {
   addSuppressedExceptionMethod.invoke(e, suppressed);
  } catch (InvocationTargetException | IllegalAccessException ignored) {
  }
 }
}

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

public static boolean isBeanPropertyReadMethod(Method method) {
  return method != null
      && Modifier.isPublic(method.getModifiers())
      && !Modifier.isStatic(method.getModifiers())
      && method.getReturnType() != void.class
      && method.getDeclaringClass() != Object.class
      && method.getParameterTypes().length == 0
      && ((method.getName().startsWith("get") && method.getName().length() > 3)
      || (method.getName().startsWith("is") && method.getName().length() > 2));
}

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

private Throwable tryInternalFastPathGetFailure(Future<?> future) throws Exception {
  Method tryInternalFastPathGetFailureMethod =
    abstractFutureClass.getDeclaredMethod("tryInternalFastPathGetFailure");
  tryInternalFastPathGetFailureMethod.setAccessible(true);
  return (Throwable) tryInternalFastPathGetFailureMethod.invoke(future);
 }
}

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

public MethodInvokeTypeProvider(TypeProvider provider, Method method, int index) {
  this.provider = provider;
  this.methodName = method.getName();
  this.declaringClass = method.getDeclaringClass();
  this.index = index;
  this.method = method;
}

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

@Override
public boolean matches(Method method, Class<?> targetClass) {
  return (method.getName().startsWith("set") &&
      method.getParameterCount() == 1 &&
      method.getReturnType() == Void.TYPE);
}

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

public static boolean isSetBeanFactory(Method candidateMethod) {
    return (candidateMethod.getName().equals("setBeanFactory") &&
        candidateMethod.getParameterCount() == 1 &&
        BeanFactory.class == candidateMethod.getParameterTypes()[0] &&
        BeanFactoryAware.class.isAssignableFrom(candidateMethod.getDeclaringClass()));
  }
}

代码示例来源: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: spring-projects/spring-framework

/**
 * Determine whether the given method is an "equals" method.
 * @see java.lang.Object#equals(Object)
 */
public static boolean isEqualsMethod(@Nullable Method method) {
  if (method == null || !method.getName().equals("equals")) {
    return false;
  }
  Class<?>[] paramTypes = method.getParameterTypes();
  return (paramTypes.length == 1 && paramTypes[0] == Object.class);
}

代码示例来源: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: google/guava

@Override
 public int compare(Method a, Method b) {
  return a.getName().compareTo(b.getName());
 }
});

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

@Override
  public boolean matches(Method m, @Nullable Class<?> targetClass) {
    return m.getName().equals("overload") && m.getParameterCount() == 1 &&
        m.getParameterTypes()[0].equals(int.class);
  }
});

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

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

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

MethodSignature(Method method) {
 name = method.getName();
 parameterTypes = Arrays.asList(method.getParameterTypes());
 typeSignature = new TypeSignature(method.getTypeParameters());
}

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

@Test
public void testFindBridgedMethodInHierarchy() throws Exception {
  Method bridgeMethod = DateAdder.class.getMethod("add", Object.class);
  assertTrue(bridgeMethod.isBridge());
  Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(bridgeMethod);
  assertFalse(bridgedMethod.isBridge());
  assertEquals("add", bridgedMethod.getName());
  assertEquals(1, bridgedMethod.getParameterCount());
  assertEquals(Date.class, bridgedMethod.getParameterTypes()[0]);
}

代码示例来源: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: spring-projects/spring-framework

@Override
public Class<?> getPropertyType() {
  if (this.member instanceof Method) {
    return ((Method) this.member).getReturnType();
  }
  else {
    return ((Field) this.member).getType();
  }
}

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

/**
 * Checks whether {@code method} is thread-safe, as indicated by the presence of the {@link
 * AllowConcurrentEvents} annotation.
 */
private static boolean isDeclaredThreadSafe(Method method) {
 return method.getAnnotation(AllowConcurrentEvents.class) != null;
}

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

/** Determines whether the given method takes a Guard as its first parameter. */
private static boolean isGuarded(Method method) {
 Class<?>[] parameterTypes = method.getParameterTypes();
 return parameterTypes.length >= 1 && parameterTypes[0] == Monitor.Guard.class;
}

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

@Nullable
private SendTo getSendTo(Method specificMethod) {
  SendTo ann = AnnotatedElementUtils.findMergedAnnotation(specificMethod, SendTo.class);
  if (ann == null) {
    ann = AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), SendTo.class);
  }
  return ann;
}

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

/**
 * Verifies that {@code method} produces a {@link NullPointerException} or {@link
 * UnsupportedOperationException} when the parameter in position {@code paramIndex} is null. If
 * this parameter is marked nullable, this method does nothing.
 *
 * @param instance the instance to invoke {@code method} on, or null if {@code method} is static
 */
public void testMethodParameter(
  @Nullable final Object instance, final Method method, int paramIndex) {
 method.setAccessible(true);
 testParameter(instance, invokable(instance, method), paramIndex, method.getDeclaringClass());
}

相关文章

微信公众号

最新文章

更多