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

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

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

Method.getAnnotationsByType介绍

暂无

代码示例

代码示例来源:origin: shekhargulati/java8-the-missing-tutorial

public static void main(String[] args) throws Exception {
    CreateVm[] createVms = VmManager.class.getMethod("manage").getAnnotationsByType(CreateVm.class);
    Stream.of(createVms).map(CreateVm::name).forEach(System.out::println);
  }
}

代码示例来源:origin: javax.enterprise/cdi-api

@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) {
    T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType);
    return new LinkedHashSet<>(asList(annotationsByType));
  }
}

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

public static List<LongVariableConstraint> parseLongVariableConstraints(Method inputFunction)
{
  return Stream.of(inputFunction.getAnnotationsByType(Constraint.class))
      .map(annotation -> new LongVariableConstraint(annotation.variable(), annotation.expression()))
      .collect(toImmutableList());
}

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

public static Map<String, Class<?>> getDeclaredSpecializedTypeParameters(Method method, Set<TypeParameter> typeParameters)
  {
    Map<String, Class<?>> specializedTypeParameters = new HashMap<>();
    TypeParameterSpecialization[] typeParameterSpecializations = method.getAnnotationsByType(TypeParameterSpecialization.class);
    ImmutableSet<String> typeParameterNames = typeParameters.stream()
        .map(TypeParameter::value)
        .collect(toImmutableSet());
    for (TypeParameterSpecialization specialization : typeParameterSpecializations) {
      checkArgument(typeParameterNames.contains(specialization.name()), "%s does not match any declared type parameters (%s) [%s]", specialization.name(), typeParameters, method);
      Class<?> existingSpecialization = specializedTypeParameters.get(specialization.name());
      checkArgument(existingSpecialization == null || existingSpecialization.equals(specialization.nativeContainerType()),
          "%s has conflicting specializations %s and %s [%s]", specialization.name(), existingSpecialization, specialization.nativeContainerType(), method);
      specializedTypeParameters.put(specialization.name(), specialization.nativeContainerType());
    }
    return specializedTypeParameters;
  }
}

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

/**
 * Returns a List of repeatable annotations by type from a method.
 *
 * @param method          is the method to find
 * @param annotationClass is the type of annotation
 * @param <A>             is the type of annotation
 * @return List of repeatable annotations if it is found
 */
public static <A extends Annotation> List<A> getRepeatableAnnotations(Method method, Class<A> annotationClass) {
  A[] annotations = method.getAnnotationsByType(annotationClass);
  if (annotations == null || annotations.length == 0) {
    for (Annotation metaAnnotation : method.getAnnotations()) {
      annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass);
      if (annotations != null && annotations.length > 0) {
        return Arrays.asList(annotations);
      }
    }
    Method superclassMethod = getOverriddenMethod(method);
    if (superclassMethod != null) {
      return getRepeatableAnnotations(superclassMethod, annotationClass);
    }
  }
  if (annotations == null) {
    return null;
  }
  return Arrays.asList(annotations);
}

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

checkArgument(nullable || !containsLegacyNullable(method.getAnnotations()), "Method [%s] is annotated with @Nullable but not @SqlNullable", method);
typeParameters.addAll(Arrays.asList(method.getAnnotationsByType(TypeParameter.class)));

代码示例来源:origin: line/armeria

final Set<Method> methods = getAllMethods(beanFactoryId.type);
for (final Method method : methods) {
  final RequestConverter[] converters = method.getAnnotationsByType(RequestConverter.class);
  try {
    final List<AnnotatedValueResolver> resolvers =

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

typeParameters = Arrays.asList(inputFunction.getAnnotationsByType(TypeParameter.class));

代码示例来源:origin: oracle/opengrok

private static boolean hasConditionalIgnoreAnnotationOnMethod(Description aDescription) {
  if (aDescription.getMethodName() == null) { // if @ClassRule is used
    return false;
  }
  try {
    // this is possible because test methods must not have any argument
    Method testMethod = aDescription.getTestClass().getMethod(aDescription.getMethodName());
    return testMethod.getAnnotationsByType(ConditionalRun.class).length > 0;
  } catch (NoSuchMethodException | SecurityException ex) {
    throw new RuntimeException(ex);
  }
}

代码示例来源:origin: oracle/opengrok

private static RunCondition getIgnoreConditionOnMethod(Description aDescription) {
  try {
    // this is possible because test methods must not have any argument
    ConditionalRun[] annotations = aDescription.getTestClass().getMethod(aDescription.getMethodName()).getAnnotationsByType(ConditionalRun.class);
    return new IgnoreConditionCreator(aDescription.getTestClass(), annotations).create();
  } catch (NoSuchMethodException | SecurityException ex) {
    throw new RuntimeException(ex);
  }
}

代码示例来源:origin: apache/tinkerpop

private static Set<FeatureRequirement> getFeatureRequirementsForTest(final Method testMethod, final LoadGraphWith[] loadGraphWiths) {
  // get feature requirements on the test method and add them to the list of ones to check
  final FeatureRequirement[] featureRequirement = testMethod.getAnnotationsByType(FeatureRequirement.class);
  final List<FeatureRequirement> frs = new ArrayList<>(Arrays.asList(featureRequirement));
  // if the graph is loading data then it will come with it's own requirements
  if (loadGraphWiths.length > 0) frs.addAll(loadGraphWiths[0].value().featuresRequired());
  // if the graph has a set of feature requirements bundled together then add those
  final FeatureRequirementSet[] featureRequirementSets = testMethod.getAnnotationsByType(FeatureRequirementSet.class);
  if (featureRequirementSets.length > 0)
    frs.addAll(Arrays.stream(featureRequirementSets)
        .flatMap(f -> f.value().featuresRequired().stream()).collect(Collectors.toList()));
  // process the unique set of feature requirements
  return new HashSet<>(frs);
}

代码示例来源:origin: mulesoft/mule

private Method resolveInjectableMethod(Object target, Method method) {
 Method candidate = null;
 for (Method serviceImplMethod : getImplementationDeclaredMethods(target)) {
  if (isPublic(serviceImplMethod.getModifiers())
    && serviceImplMethod.getName().equals(method.getName())
    && serviceImplMethod.getAnnotationsByType(Inject.class).length > 0
    && equivalentParams(method.getParameters(), serviceImplMethod.getParameters())) {
   if (candidate != null
     && !(candidate.getName().equals(serviceImplMethod.getName())
       && deepEquals(candidate.getParameterTypes(), serviceImplMethod.getParameterTypes()))) {
    throw new IllegalDependencyInjectionException(format(MANY_CANDIDATES_ERROR_MSG_TEMPLATE, method.getName(),
                               target.toString()));
   }
   candidate = serviceImplMethod;
  }
 }
 return candidate;
}

代码示例来源:origin: mulesoft/mule

private Method resolveInjectableMethod(Method method) {
 Method candidate = null;
 for (Method serviceImplMethod : getImplementationDeclaredMethods()) {
  if (isPublic(serviceImplMethod.getModifiers())
    && serviceImplMethod.getName().equals(method.getName())
    && serviceImplMethod.getAnnotationsByType(Inject.class).length > 0
    && equivalentParams(method.getParameters(), serviceImplMethod.getParameters())) {
   if (candidate != null
     && !(candidate.getName().equals(serviceImplMethod.getName())
       && deepEquals(candidate.getParameterTypes(), serviceImplMethod.getParameterTypes()))) {
    throw new IllegalDependencyInjectionException(format(MANY_CANDIDATES_ERROR_MSG_TEMPLATE, method.getName(),
                               getProxiedObject().getName()));
   }
   candidate = serviceImplMethod;
  }
 }
 return candidate;
}

代码示例来源:origin: apache/tinkerpop

@Before
public void setup() throws Exception {
  final Method testMethod = this.getClass().getMethod(cleanMethodName(name.getMethodName()));
  final LoadGraphWith[] loadGraphWiths = testMethod.getAnnotationsByType(LoadGraphWith.class);
  final LoadGraphWith loadGraphWith = loadGraphWiths.length == 0 ? null : loadGraphWiths[0];
  final LoadGraphWith.GraphData loadGraphWithData = null == loadGraphWith ? null : loadGraphWith.value();
  final Set<FeatureRequirement> featureRequirementSet = getFeatureRequirementsForTest(testMethod, loadGraphWiths);
  graphProvider = GraphManager.getGraphProvider();
  // pre-check if available from graph provider to avoid graph creation
  final Optional<Graph.Features> staticFeatures = graphProvider.getStaticFeatures();
  if (staticFeatures.isPresent()) {
    assumeRequirementsAreMetForTest(featureRequirementSet, staticFeatures.get(), true);
  }
  graphProvider.getTestListener().ifPresent(l -> l.onTestStart(this.getClass(), name.getMethodName()));
  config = graphProvider.standardGraphConfiguration(this.getClass(), name.getMethodName(), loadGraphWithData);
  // this should clear state from a previously unfinished test. since the graph does not yet exist,
  // persisted graphs will likely just have their directories removed
  graphProvider.clear(config);
  graph = graphProvider.openTestGraph(config);
  g = graphProvider.traversal(graph);
  // even if we checked static features earlier it's of little cost to recheck again with the real graph
  // once it is instantiated. the real cost savings is preventing graph creation in the first place so
  // let's double check that all is legit.
  assumeRequirementsAreMetForTest(featureRequirementSet, graph.features(), false);
  beforeLoadGraphWith(graph);
  // load a graph with sample data if the annotation is present on the test
  graphProvider.loadGraphData(graph, loadGraphWith, this.getClass(), name.getMethodName());
  afterLoadGraphWith(graph);
}

代码示例来源:origin: resteasy/Resteasy

Stream[] streams = method.getMethod().getAnnotationsByType(Stream.class);
if (streams.length > 0)

代码示例来源:origin: javax/javaee-web-api

@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) {
    T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType);
    return new LinkedHashSet<>(asList(annotationsByType));
  }
}

代码示例来源:origin: weld/core

@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) {
    T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType);
    return new LinkedHashSet<>(asList(annotationsByType));
  }
}

代码示例来源:origin: weld/core

@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) {
    T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType);
    return new LinkedHashSet<>(asList(annotationsByType));
  }
}

代码示例来源:origin: lvhao/schedule-job

@Around(value = "anyPublicServiceMethod() && annotationOnClass(targetDataSource))", argNames = "proceedingJoinPoint,targetDataSource")
public Object annotationOnClassInvoke(ProceedingJoinPoint proceedingJoinPoint, TargetDataSource targetDataSource) throws Throwable {
  MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
  Method method = ms.getMethod();
  // 方法是否标注了注解 TargetDataSource
  if(method.isAnnotationPresent(TargetDataSource.class)){
    targetDataSource = method.getAnnotationsByType(TargetDataSource.class)[0];
  }
  return doInvoke(proceedingJoinPoint, targetDataSource);
}

代码示例来源:origin: apache/cxf

private static void checkForInvalidClientHeaderParams(Class<?> userType, Method[] methods) {
  ClientHeaderParam[] interfaceAnnotations = userType.getAnnotationsByType(ClientHeaderParam.class);
  checkClientHeaderParamAnnotation(interfaceAnnotations, userType, methods);
  for (Method method : methods) {
    ClientHeaderParam[] methodAnnotations = method.getAnnotationsByType(ClientHeaderParam.class);
    checkClientHeaderParamAnnotation(methodAnnotations, userType, methods);
  }
}

相关文章

微信公众号

最新文章

更多