java.lang.reflect.Method.getAnnotations()方法的使用及代码示例

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

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

Method.getAnnotations介绍

暂无

代码示例

代码示例来源:origin: junit-team/junit4

/**
 * Returns the annotations on this method
 */
public Annotation[] getAnnotations() {
  return method.getAnnotations();
}

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

/**
 * Returns the annotations on this method
 */
@Override
public Annotation[] getAnnotations() {
  return fMethod.getAnnotations();
}

代码示例来源:origin: junit-team/junit4

protected Annotation[] testAnnotations(Method method) {
  return method.getAnnotations();
}

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

Builder(Retrofit retrofit, Method method) {
 this.retrofit = retrofit;
 this.method = method;
 this.methodAnnotations = method.getAnnotations();
 this.parameterTypes = method.getGenericParameterTypes();
 this.parameterAnnotationsArray = method.getParameterAnnotations();
}

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

public DefaultCacheMethodDetails(Method method, A cacheAnnotation, String cacheName) {
  this.method = method;
  this.annotations = Collections.unmodifiableSet(
      new LinkedHashSet<>(Arrays.asList(method.getAnnotations())));
  this.cacheAnnotation = cacheAnnotation;
  this.cacheName = cacheName;
}

代码示例来源:origin: prestodb/presto

public static List<Method> findPublicStaticMethodsWithAnnotation(Class<?> clazz, Class<?> annotationClass)
{
  ImmutableList.Builder<Method> methods = ImmutableList.builder();
  for (Method method : clazz.getMethods()) {
    for (Annotation annotation : method.getAnnotations()) {
      if (annotationClass.isInstance(annotation)) {
        checkArgument(Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers()), "%s annotated with %s must be static and public", method.getName(), annotationClass.getSimpleName());
        methods.add(method);
      }
    }
  }
  return methods.build();
}

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

private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
  Retrofit retrofit, Method method) {
 Type returnType = method.getGenericReturnType();
 Annotation[] annotations = method.getAnnotations();
 try {
  //noinspection unchecked
  return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);
 } catch (RuntimeException e) { // Wide exception range because factories are user code.
  throw methodError(method, e, "Unable to create call adapter for %s", returnType);
 }
}

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

@Override
 public T invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Type returnType = method.getGenericReturnType();
  Annotation[] methodAnnotations = method.getAnnotations();
  CallAdapter<R, T> callAdapter =
    (CallAdapter<R, T>) retrofit.callAdapter(returnType, methodAnnotations);
  return callAdapter.adapt(behaviorCall);
 }
});

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

@Override
public List<Annotation> getAnnotations() {
  Annotation[] annotations = null;
  if (read != null) {
    annotations = read.getAnnotations();
  } else if (field != null) {
    annotations = field.getAnnotations();
  }
  return annotations != null ? Arrays.asList(annotations) : delegate.getAnnotations();
}

代码示例来源:origin: prestodb/presto

@SafeVarargs
public static Set<Method> findPublicMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation>... annotationClasses)
{
  ImmutableSet.Builder<Method> methods = ImmutableSet.builder();
  for (Method method : clazz.getDeclaredMethods()) {
    for (Annotation annotation : method.getAnnotations()) {
      for (Class<?> annotationClass : annotationClasses) {
        if (annotationClass.isInstance(annotation)) {
          checkArgument(Modifier.isPublic(method.getModifiers()), "Method [%s] annotated with @%s must be public", method, annotationClass.getSimpleName());
          methods.add(method);
        }
      }
    }
  }
  return methods.build();
}

代码示例来源:origin: swagger-api/swagger-core

public static String getHttpMethodFromCustomAnnotations(Method method) {
    for (Annotation methodAnnotation : method.getAnnotations()) {
      HttpMethod httpMethod = methodAnnotation.annotationType().getAnnotation(HttpMethod.class);
      if (httpMethod != null) {
        return httpMethod.value().toLowerCase();
      }
    }
    return null;
  }
}

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

private static <ResponseT> Converter<ResponseBody, ResponseT> createResponseConverter(
  Retrofit retrofit, Method method, Type responseType) {
 Annotation[] annotations = method.getAnnotations();
 try {
  return retrofit.responseBodyConverter(responseType, annotations);
 } catch (RuntimeException e) { // Wide exception range because factories are user code.
  throw methodError(method, e, "Unable to create converter for %s", responseType);
 }
}

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

@Override
public boolean hasAnnotatedMethods(String annotationName) {
  try {
    Method[] methods = getIntrospectedClass().getDeclaredMethods();
    for (Method method : methods) {
      if (!method.isBridge() && method.getAnnotations().length > 0 &&
          AnnotatedElementUtils.isAnnotated(method, annotationName)) {
        return true;
      }
    }
    return false;
  }
  catch (Throwable ex) {
    throw new IllegalStateException("Failed to introspect annotated methods on " + getIntrospectedClass(), ex);
  }
}

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

@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
  try {
    Method[] methods = getIntrospectedClass().getDeclaredMethods();
    Set<MethodMetadata> annotatedMethods = new LinkedHashSet<>(4);
    for (Method method : methods) {
      if (!method.isBridge() && method.getAnnotations().length > 0 &&
          AnnotatedElementUtils.isAnnotated(method, annotationName)) {
        annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap));
      }
    }
    return annotatedMethods;
  }
  catch (Throwable ex) {
    throw new IllegalStateException("Failed to introspect annotated methods on " + getIntrospectedClass(), ex);
  }
}

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

private boolean isAnnotated(Method method) {
  Annotation[] anns = method.getAnnotations();
  if (anns == null) {
    return false;
  }
  for (Annotation ann : anns) {
    String name = ann.annotationType().getName();
    if (name.endsWith("Anno")) {
      return true;
    }
  }
  return false;
}

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

/**
 * Get all {@link Annotation Annotations} that are <em>present</em> on the
 * supplied {@link Method}.
 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
 * <p>Meta-annotations will <em>not</em> be searched.
 * @param method the Method to retrieve annotations from
 * @return the annotations found, an empty array, or {@code null} if not
 * resolvable (e.g. because nested Class values in annotation attributes
 * failed to resolve at runtime)
 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
 * @see AnnotatedElement#getAnnotations()
 */
@Nullable
public static Annotation[] getAnnotations(Method method) {
  try {
    return synthesizeAnnotationArray(BridgeMethodResolver.findBridgedMethod(method).getAnnotations(), method);
  }
  catch (Throwable ex) {
    handleIntrospectionFailure(method, ex);
    return null;
  }
}

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

/**
 * Filter on methods not annotated with the given annotation type.
 */
@SafeVarargs
public final Builder<T> annotNotPresent(Class<? extends Annotation>... annotationTypes) {
  String message = "annotationNotPresent=" + Arrays.toString(annotationTypes);
  addFilter(message, method -> {
    if (annotationTypes.length != 0) {
      return Arrays.stream(annotationTypes).noneMatch(annotType ->
          AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null);
    }
    else {
      return method.getAnnotations().length == 0;
    }
  });
  return this;
}

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

/**
 * Filter on methods not annotated with the given annotation type.
 */
@SafeVarargs
public final Builder<T> annotNotPresent(Class<? extends Annotation>... annotationTypes) {
  String message = "annotationNotPresent=" + Arrays.toString(annotationTypes);
  addFilter(message, method -> {
    if (annotationTypes.length != 0) {
      return Arrays.stream(annotationTypes).noneMatch(annotType ->
          AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null);
    }
    else {
      return method.getAnnotations().length == 0;
    }
  });
  return this;
}

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

@Test public void testGetElementKey_WithQualifier() throws NoSuchMethodException {
 Method method = KeysTest.class.getDeclaredMethod("qualifiedElementProvides", new Class<?>[]{});
 assertThat(Keys.getSetKey(method.getGenericReturnType(), method.getAnnotations(), method))
   .isEqualTo("@javax.inject.Named(value=foo)/java.util.Set<java.lang.String>");
}

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

@Test public void testGetElementKey_NoQualifier() throws NoSuchMethodException {
 Method method = KeysTest.class.getDeclaredMethod("elementProvides", new Class<?>[]{});
 assertThat(Keys.getSetKey(method.getGenericReturnType(), method.getAnnotations(), method))
   .isEqualTo("java.util.Set<java.lang.String>");
}

相关文章

微信公众号

最新文章

更多