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

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

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

Class.getDeclaredMethod介绍

[英]Returns a Method object which represents the method matching the given name and parameter types that is declared by the class represented by this Class. (Class[]) null is equivalent to the empty array.

See #getMethod if you want to search superclasses.
[中]返回一个Method对象,该对象表示与该类所表示的类声明的给定名称和参数类型相匹配的方法。(类[])null相当于空数组。
如果要搜索超类,请参阅#getMethod。

代码示例

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

private static boolean isEqualsDefined(Class<?> cls) {
 try {
  return !cls.getDeclaredMethod("equals", Object.class).isSynthetic();
 } catch (NoSuchMethodException e) {
  return false;
 }
}

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

static WildcardType getWildcardType(String methodName) throws Exception {
  ParameterizedType parameterType =
    (ParameterizedType)
      WithWildcardType.class.getDeclaredMethod(methodName, List.class)
        .getGenericParameterTypes()[0];
  return (WildcardType) parameterType.getActualTypeArguments()[0];
 }
}

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

static Throwable[] getSuppressed(Throwable throwable) {
 try {
  Method getSuppressed = Throwable.class.getDeclaredMethod("getSuppressed");
  return (Throwable[]) getSuppressed.invoke(throwable);
 } catch (Exception e) {
  throw new AssertionError(e); // only called if running on JDK7
 }
}

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

private Method getTestSubscriberMethod(String name) {
 try {
  return getClass().getDeclaredMethod(name, Object.class);
 } catch (NoSuchMethodException e) {
  throw new AssertionError();
 }
}

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

static TypeVariable<?> getTypeVariable(String methodName) throws Exception {
  ParameterizedType parameterType =
    (ParameterizedType)
      WithTypeVariable.class.getDeclaredMethod(methodName, List.class)
        .getGenericParameterTypes()[0];
  return (TypeVariable<?>) parameterType.getActualTypeArguments()[0];
 }
}

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

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

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

static Invokable<Prepender, Object> method(String name, Class<?>... parameterTypes) {
 try {
  Method method = Prepender.class.getDeclaredMethod(name, parameterTypes);
  @SuppressWarnings("unchecked") // The method is from Prepender.
  Invokable<Prepender, Object> invokable =
    (Invokable<Prepender, Object>) Invokable.from(method);
  return invokable;
 } catch (NoSuchMethodException e) {
  throw new IllegalArgumentException(e);
 }
}

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

Type getTargetType(String methodName) throws Exception {
  ParameterizedType parameterType =
    (ParameterizedType)
      WithGenericBound.class.getDeclaredMethod(methodName, List.class)
        .getGenericParameterTypes()[0];
  parameterType =
    (ParameterizedType) TypeToken.of(this.getClass()).resolveType(parameterType).getType();
  return parameterType.getActualTypeArguments()[0];
 }
}

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

public void testToGenericType_staticMemberClass() throws Exception {
 Method getStaticAnonymousClassMethod =
   TypeTokenTest.class.getDeclaredMethod("getStaticAnonymousClass", Object.class);
 ParameterizedType javacReturnType =
   (ParameterizedType) getStaticAnonymousClassMethod.getGenericReturnType();
 ParameterizedType parameterizedType =
   (ParameterizedType) TypeToken.toGenericType(GenericClass.class).getType();
 assertThat(parameterizedType.getOwnerType()).isEqualTo(javacReturnType.getOwnerType());
}

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

public <T> void testCaptureTypeParameter() throws Exception {
 TypeVariable<?> variable = new TypeParameter<T>() {}.typeVariable;
 TypeVariable<?> expected =
   TypeParameterTest.class.getDeclaredMethod("testCaptureTypeParameter")
     .getTypeParameters()[0];
 assertEquals(expected, variable);
}

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

public void testVisibility_protected() throws Exception {
 assertFalse(
   Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod")));
 assertFalse(
   Visibility.PROTECTED.isVisible(
     VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod")));
 assertTrue(
   Visibility.PROTECTED.isVisible(
     VisibilityMethods.class.getDeclaredMethod("protectedMethod")));
 assertTrue(
   Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod")));
}

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

static Element method(String name, Class<?>... parameterTypes) throws Exception {
 Element element = new Element(A.class.getDeclaredMethod(name, parameterTypes));
 assertEquals(name, element.getName());
 assertEquals(A.class, element.getDeclaringClass());
 return element;
}

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

public void testVisibility_public() throws Exception {
 assertFalse(
   Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod")));
 assertFalse(
   Visibility.PUBLIC.isVisible(
     VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod")));
 assertFalse(
   Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("protectedMethod")));
 assertTrue(
   Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod")));
}

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

public void testVisibility_package() throws Exception {
 assertFalse(
   Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod")));
 assertTrue(
   Visibility.PACKAGE.isVisible(
     VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod")));
 assertTrue(
   Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("protectedMethod")));
 assertTrue(
   Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod")));
}

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

public void testNulls() throws Exception {
 new ClassSanityTester()
   .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls"))
   .testNulls(FreshValueGenerator.class);
}

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

public void testWildcardCaptured_wildcardWithImplicitBound() throws Exception {
 TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {};
 ImmutableList<Parameter> parameters =
   type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters();
 assertThat(parameters).hasSize(1);
 TypeToken<?> parameterType = parameters.get(0).getType();
 Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments();
 assertThat(typeArgs).asList().hasSize(1);
 TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0];
 assertThat(captured.getBounds()).asList().containsExactly(Object.class);
 assertThat(new TypeToken<List<?>>() {}.isSupertypeOf(parameterType)).isTrue();
}

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

public void testWildcardCaptured_wildcardWithExplicitBound() throws Exception {
 TypeToken<Holder<? extends Number>> type = new TypeToken<Holder<? extends Number>>() {};
 ImmutableList<Parameter> parameters =
   type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters();
 assertThat(parameters).hasSize(1);
 TypeToken<?> parameterType = parameters.get(0).getType();
 Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments();
 assertThat(typeArgs).asList().hasSize(1);
 TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0];
 assertThat(captured.getBounds()).asList().containsExactly(Number.class);
 assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(parameterType)).isTrue();
}

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

public void testWildcardCaptured_methodParameter_upperBound() throws Exception {
 TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {};
 ImmutableList<Parameter> parameters =
   type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters();
 assertThat(parameters).hasSize(1);
 TypeToken<?> parameterType = parameters.get(0).getType();
 assertEquals(List.class, parameterType.getRawType());
 assertFalse(
   parameterType.getType().toString(),
   parameterType.isSupertypeOf(new TypeToken<List<Integer>>() {}));
}

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

public <A, B> void testEquals() throws Exception {
 Method method = TypeParameterTest.class.getDeclaredMethod("testEquals");
 new EqualsTester()
   .addEqualityGroup(new TypeParameter<A>() {}, new TypeParameter<A>() {})
   .addEqualityGroup(new TypeParameter<B>() {})
   .testEquals();
}

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

public void testNonFinalMethodInFinalClass_isOverridable() throws Exception {
 Invokable<?, ?> delegate = Invokable.from(FinalClass.class.getDeclaredMethod("notFinalMethod"));
 assertFalse(delegate.isOverridable());
 assertFalse(delegate.isVarArgs());
}

相关文章

Class类方法