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

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

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

Class.getAnnotationsByType介绍

暂无

代码示例

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

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

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

private List<SystemResourceConsideration> getSystemResourceConsiderations(final ConfigurableComponent component) {
  SystemResourceConsideration[] systemResourceConsiderations = component.getClass().getAnnotationsByType(SystemResourceConsideration.class);
  if (systemResourceConsiderations == null) {
    return Collections.emptyList();
  }
  return Arrays.asList(systemResourceConsiderations);
}

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

public static List<SqlWindowFunction> parseFunctionDefinition(Class<? extends WindowFunction> clazz)
{
  WindowFunctionSignature[] signatures = clazz.getAnnotationsByType(WindowFunctionSignature.class);
  checkArgument(signatures.length > 0, "Class is not annotated with @WindowFunctionSignature: %s", clazz.getName());
  return Stream.of(signatures)
      .map(signature -> parse(clazz, signature))
      .collect(toImmutableList());
}

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

public static <A extends Annotation> A[] getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass) {
  A[] annotations = cls.getAnnotationsByType(annotationClass);
  if (annotations == null || annotations.length == 0) {
    for (Annotation metaAnnotation : cls.getAnnotations()) {
      annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass);
      if (annotations != null && annotations.length > 0) {
        return annotations;
      }
    }
    Class<?> superClass = cls.getSuperclass();
    if (superClass != null && !(superClass.equals(Object.class))) {
      annotations = getRepeatableAnnotationsArray(superClass, annotationClass);
    }
  }
  if (annotations == null || annotations.length == 0) {
    for (Class<?> anInterface : cls.getInterfaces()) {
      for (Annotation metaAnnotation : anInterface.getAnnotations()) {
        annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass);
        if (annotations != null && annotations.length > 0) {
          return annotations;
        }
      }
      annotations = getRepeatableAnnotationsArray(anInterface, annotationClass);
      if (annotations != null) {
        return annotations;
      }
    }
  }
  return annotations;
}

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

private List<Input> fetchDefaultBindings(Class<?> event, BindsConfiguration config) {
  List<Input> defaultInputs = Lists.newArrayList();
  Collection<Input> boundInputs = config.getBoundInputs();
  for (Annotation annotation : event.getAnnotationsByType(DefaultBinding.class)) {
    DefaultBinding defaultBinding = (DefaultBinding) annotation;
    Input defaultInput = defaultBinding.type().getInput(defaultBinding.id());
    if (!boundInputs.contains(defaultInput)) {
      defaultInputs.add(defaultInput);
    } else {
      logger.warn("Input {} is already registered, can not use it as default for event {}", defaultInput, event);
    }
  }
  return defaultInputs;
}

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

private static long getTimeoutInSeconds(Class<?> clazz) {
 Category[] categories = clazz.getAnnotationsByType(Category.class);
 for (Class<?> c : categories[0].value()) {
  if (c == SmallTests.class || c == MediumTests.class || c == LargeTests.class) {
   // All tests have a 10minute timeout.
   return TimeUnit.MINUTES.toSeconds(13);
  }
  if (c == IntegrationTests.class) {
   return TimeUnit.MINUTES.toSeconds(Long.MAX_VALUE);
  }
 }
 throw new IllegalArgumentException(
   clazz.getName() + " does not have SmallTests/MediumTests/LargeTests in @Category");
}

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

private static boolean hasConditionalIgnoreAnnotationOnClass(Description aDescription) {
  return aDescription.getTestClass().getAnnotationsByType(ConditionalRun.class).length > 0;
}

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

objectResolvers, beanFactoryId.type.getAnnotationsByType(RequestConverter.class));

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

@Test
public void getRepeatableAnnotationsDeclaredOnClass() {
  final List<String> expectedValuesJava = asList("A", "B", "C");
  final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1");
  // Java 8
  MyRepeatable[] array = MyRepeatableClass.class.getAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesJava));
  // Spring
  Set<MyRepeatable> set = getRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
  // When container type is omitted and therefore inferred from @Repeatable
  set = getRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
}

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

@Test
public void getRepeatableAnnotationsDeclaredOnSuperclass() {
  final Class<?> clazz = SubMyRepeatableClass.class;
  final List<String> expectedValuesJava = asList("A", "B", "C");
  final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1");
  // Java 8
  MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesJava));
  // Spring
  Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
  // When container type is omitted and therefore inferred from @Repeatable
  set = getRepeatableAnnotations(clazz, MyRepeatable.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
}

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

@Test
public void getRepeatableAnnotationsDeclaredOnMultipleSuperclasses() {
  final Class<?> clazz = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
  final List<String> expectedValuesJava = asList("X", "Y", "Z");
  final List<String> expectedValuesSpring = asList("X", "Y", "Z", "meta2");
  // Java 8
  MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesJava));
  // Spring
  Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
  // When container type is omitted and therefore inferred from @Repeatable
  set = getRepeatableAnnotations(clazz, MyRepeatable.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
}

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

@Test
public void getRepeatableAnnotationsDeclaredOnClassAndSuperclass() {
  final Class<?> clazz = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
  final List<String> expectedValuesJava = asList("X", "Y", "Z");
  final List<String> expectedValuesSpring = asList("X", "Y", "Z", "meta2");
  // Java 8
  MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesJava));
  // Spring
  Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
  // When container type is omitted and therefore inferred from @Repeatable
  set = getRepeatableAnnotations(clazz, MyRepeatable.class);
  assertNotNull(set);
  values = set.stream().map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesSpring));
}

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

final Repeatable[] repeatableAnnotations = annotationType.getAnnotationsByType(Repeatable.class);
final Class<? extends Annotation> containerType =
    repeatableAnnotations.length > 0 ? repeatableAnnotations[0].value() : null;

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

private Plugin instantiate(List<Plugin> scannedPlugins, Class<Plugin> clazz) throws PluginInstantiationException
  PluginDependency[] pluginDependencies = clazz.getAnnotationsByType(PluginDependency.class);
  List<Plugin> deps = new ArrayList<>();
  for (PluginDependency pluginDependency : pluginDependencies)

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

/**
 * Writes all the system resource considerations for this component
 *
 * @param configurableComponent the component to describe
 * @param xmlStreamWriter the xml stream writer to use
 * @throws XMLStreamException thrown if there was a problem writing the XML
 */
private void writeSystemResourceConsiderationInfo(ConfigurableComponent configurableComponent, XMLStreamWriter xmlStreamWriter)
    throws XMLStreamException {
  SystemResourceConsideration[] systemResourceConsiderations = configurableComponent.getClass().getAnnotationsByType(SystemResourceConsideration.class);
  writeSimpleElement(xmlStreamWriter, "h3", "System Resource Considerations:");
  if (systemResourceConsiderations.length > 0) {
    xmlStreamWriter.writeStartElement("table");
    xmlStreamWriter.writeAttribute("id", "system-resource-considerations");
    xmlStreamWriter.writeStartElement("tr");
    writeSimpleElement(xmlStreamWriter, "th", "Resource");
    writeSimpleElement(xmlStreamWriter, "th", "Description");
    xmlStreamWriter.writeEndElement();
    for (SystemResourceConsideration systemResourceConsideration : systemResourceConsiderations) {
      xmlStreamWriter.writeStartElement("tr");
      writeSimpleElement(xmlStreamWriter, "td", systemResourceConsideration.resource().name());
      writeSimpleElement(xmlStreamWriter, "td", systemResourceConsideration.description().trim().isEmpty()
          ? "Not Specified" : systemResourceConsideration.description());
      xmlStreamWriter.writeEndElement();
    }
    xmlStreamWriter.writeEndElement();
  } else {
    xmlStreamWriter.writeCharacters("None specified.");
  }
}

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

PluginDependency[] pluginDependencies = pluginClazz.getAnnotationsByType(PluginDependency.class);

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

@Override
 public void testStarted(Description description) throws Exception {
  Category[] categories = description.getTestClass().getAnnotationsByType(Category.class);
  for (Class<?> c : categories[0].value()) {
   if (c == IntegrationTests.class) {
    return;
   }
  }
  for (Field field : description.getTestClass().getFields()) {
   if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class &&
    field.isAnnotationPresent(ClassRule.class)) {
    HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null);
    assertEquals(
     "The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() +
      " is for " + timeout.getClazz().getName(),
     description.getTestClass(), timeout.getClazz());
    return;
   }
  }
  fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName());
 }
}

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

private static RunCondition getIgnoreConditionOnClass(Description aDescription) {
  ConditionalRun[] annotations = aDescription.getTestClass().getAnnotationsByType(ConditionalRun.class);
  return new IgnoreConditionCreator(aDescription.getTestClass(), annotations).create();
}

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

private static List<Graph.OptOut> getAllOptOuts(final Class<?> clazz) {
  // we typically get a null class if this is called recursively and the original clazz was an interface
  if (clazz == Object.class || null == clazz)
    return Collections.emptyList();
  return Stream.concat(getAllOptOuts(clazz.getSuperclass()).stream(),
      Stream.of(clazz.getAnnotationsByType(Graph.OptOut.class))).distinct().collect(Collectors.toList());
}

相关文章

Class类方法