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

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

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

Class.getDeclaredAnnotationsByType介绍

暂无

代码示例

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

@Test
public void getDeclaredRepeatableAnnotationsDeclaredOnClass() {
  final List<String> expectedValuesJava = asList("A", "B", "C");
  final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1");
  // Java 8
  MyRepeatable[] array = MyRepeatableClass.class.getDeclaredAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  assertThat(values, is(expectedValuesJava));
  // Spring
  Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(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 = getDeclaredRepeatableAnnotations(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 getDeclaredRepeatableAnnotationsDeclaredOnSuperclass() {
  final Class<?> clazz = SubMyRepeatableClass.class;
  // Java 8
  MyRepeatable[] array = clazz.getDeclaredAnnotationsByType(MyRepeatable.class);
  assertNotNull(array);
  assertThat(array.length, is(0));
  // Spring
  Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
  assertNotNull(set);
  assertThat(set.size(), is(0));
  // When container type is omitted and therefore inferred from @Repeatable
  set = getDeclaredRepeatableAnnotations(clazz, MyRepeatable.class);
  assertNotNull(set);
  assertThat(set.size(), is(0));
}

代码示例来源:origin: software.amazon.jsii/jsii-runtime

/**
 * Attempts to find the @Jsii annotation from a type.
 * @param type The type.
 * @param inherited If 'true' will look for the annotation up the class hierarchy.
 * @return The annotation or null.
 */
static Jsii tryGetJsiiAnnotation(final Class<?> type, final boolean inherited) {
  Jsii[] ann;
  if (inherited) {
    ann = (Jsii[]) type.getAnnotationsByType(Jsii.class);
  } else {
    ann = (Jsii[]) type.getDeclaredAnnotationsByType(Jsii.class);
  }
  if (ann.length == 0) {
    return null;
  }
  return ann[0];
}

代码示例来源:origin: com.fitbur.testify/integration

public void real(Set<FieldDescriptor> fieldDescriptors) {
  Class<?> testClass = context.getTestClass();
  of(testClass.getDeclaredAnnotationsByType(Module.class))
      .map(Module::value)
      .distinct()
      .forEachOrdered(locator::addModule);
  locator.reload();
  testReifier.reifyTest(fieldDescriptors);
}

代码示例来源:origin: com.fitbur.testify.level/system

public void real(Set<FieldDescriptor> fieldDescriptors) {
  Class<?> testClass = context.getTestClass();
  of(testClass.getDeclaredAnnotationsByType(Module.class))
      .map(Module::value)
      .distinct()
      .forEachOrdered(locator::addModule);
  locator.reload();
  testReifier.reifyTest(fieldDescriptors);
}

代码示例来源:origin: com.fitbur.testify/system

public void real(Set<FieldDescriptor> fieldDescriptors) {
  Class<?> testClass = context.getTestClass();
  of(testClass.getDeclaredAnnotationsByType(Module.class))
      .map(Module::value)
      .distinct()
      .forEachOrdered(locator::addModule);
  locator.reload();
  testReifier.reifyTest(fieldDescriptors);
}

代码示例来源:origin: com.fitbur.testify.level/integration

public void real(Set<FieldDescriptor> fieldDescriptors) {
  Class<?> testClass = context.getTestClass();
  of(testClass.getDeclaredAnnotationsByType(Module.class))
      .map(Module::value)
      .distinct()
      .forEachOrdered(locator::addModule);
  locator.reload();
  testReifier.reifyTest(fieldDescriptors);
}

代码示例来源:origin: kumuluz/kumuluzee

private <E extends Extension> List<ExtensionWrapper<E>> processSingleEeExtensions(List<E> extensions, List<EeComponentWrapper>
    wrappedComponents) {
  List<ExtensionWrapper<E>> extensionWrappers = new ArrayList<>();
  for (E e : extensions) {
    EeExtensionDef def = e.getClass().getDeclaredAnnotation(EeExtensionDef.class);
    if (def != null) {
      EeComponentDependency[] dependencies = e.getClass().getDeclaredAnnotationsByType(EeComponentDependency.class);
      EeComponentOptional[] optionals = e.getClass().getDeclaredAnnotationsByType(EeComponentOptional.class);
      extensionWrappers.add(new ExtensionWrapper<>(e, def.name(), def.group(), dependencies, optionals));
    }
  }
  log.info("Processing EE single extensions dependencies");
  processEeExtensionDependencies(extensionWrappers, wrappedComponents);
  return extensionWrappers;
}

代码示例来源:origin: org.leapframework/leap-orm

@Override
public void preMappingEntity(MetadataContext context, EntityMappingBuilder emb) throws MetadataException {
  Class<?> sourceClass = emb.getSourceClass();
  if(null != sourceClass){
    mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(Entity.class));
    mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(Table.class));
    mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(AutoCreateTable.class));
    mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(AutoGenerateColumns.class));
    mappingListenerByAnnotations(context, emb, sourceClass.getDeclaredAnnotationsByType(Entity.Listener.class));
    mappingManyToOneByClassAnnotation(context, emb, sourceClass.getDeclaredAnnotationsByType(ManyToOne.class));
    mappingManyToManyByClassAnnotation(context, emb, sourceClass.getDeclaredAnnotationsByType(ManyToMany.class));
  }
}

代码示例来源:origin: com.fitbur.testify.level/system

of(testClass.getDeclaredAnnotationsByType(Module.class))
    .map(Module::value)
    .distinct()
    .forEachOrdered(locator::addModule);
of(testClass.getDeclaredAnnotationsByType(Scan.class))
    .map(Scan::value)
    .distinct()

代码示例来源:origin: com.fitbur.testify/system

of(testClass.getDeclaredAnnotationsByType(Module.class))
    .map(Module::value)
    .distinct()
    .forEachOrdered(locator::addModule);
of(testClass.getDeclaredAnnotationsByType(Scan.class))
    .map(Scan::value)
    .distinct()

代码示例来源:origin: com.fitbur.testify.level/integration

of(testClass.getDeclaredAnnotationsByType(Module.class))
    .map(Module::value)
    .distinct()
    .forEachOrdered(locator::addModule);
of(testClass.getDeclaredAnnotationsByType(Scan.class))
    .map(Scan::value)
    .distinct()

代码示例来源:origin: com.fitbur.testify/integration

of(testClass.getDeclaredAnnotationsByType(Module.class))
    .map(Module::value)
    .distinct()
    .forEachOrdered(locator::addModule);
of(testClass.getDeclaredAnnotationsByType(Scan.class))
    .map(Scan::value)
    .distinct()

代码示例来源:origin: neo4j/neo4j-ogm

private synchronized Collection<CompositeIndex> initCompositeIndexFields() {
  // init property fields to be able to check existence of properties
  propertyFields();
  if (cls == null) {
    try {
      cls = Class.forName(className, false, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException e) {
      throw new RuntimeException("Could not get annotation info for class " + className, e);
    }
  }
  CompositeIndex[] annotations = cls.getDeclaredAnnotationsByType(CompositeIndex.class);
  ArrayList<CompositeIndex> result = new ArrayList<>(annotations.length);
  for (CompositeIndex annotation : annotations) {
    String[] properties = annotation.value().length > 0 ? annotation.value() : annotation.properties();
    if (properties.length < 1) {
      throw new MetadataException("Incorrect CompositeIndex definition on " + className +
        ". Provide at least 1 property");
    }
    for (String property : properties) {
      FieldInfo fieldInfo = propertyFields.get(property);
      if (fieldInfo == null) {
        throw new MetadataException("Incorrect CompositeIndex definition on " + className + ". Property " +
          property + " does not exists.");
      }
    }
    result.add(annotation);
  }
  return result;
}

代码示例来源:origin: kumuluz/kumuluzee

private <E extends Extension> List<ExtensionWrapper<E>> processGroupEeExtensions(List<E> extensions, List<EeComponentWrapper>
    wrappedComponents) {
  Map<String, ExtensionWrapper<E>> eeExt = new HashMap<>();
  // Wrap extensions with their metadata and check for duplicates
  for (E e : extensions) {
    EeExtensionDef def = e.getClass().getDeclaredAnnotation(EeExtensionDef.class);
    if (def != null) {
      if (eeExt.containsKey(def.group())) {
        String msg = "Found multiple implementations (" + eeExt.get(def.group()).getName() +
            ", " + def.name() + ") of the same EE extension group (" + def.group() + "). " +
            "Please check to make sure you only include a single implementation of a specific " +
            "EE extension group.";
        log.severe(msg);
        throw new KumuluzServerException(msg);
      }
      EeComponentDependency[] dependencies = e.getClass().getDeclaredAnnotationsByType
          (EeComponentDependency.class);
      EeComponentOptional[] optionals = e.getClass().getDeclaredAnnotationsByType(EeComponentOptional.class);
      eeExt.put(def.group(), new ExtensionWrapper<>(e, def.name(), def.group(), dependencies, optionals));
    }
  }
  List<ExtensionWrapper<E>> extensionWrappers = new ArrayList<>(eeExt.values());
  log.info("Processing EE extension dependencies");
  processEeExtensionDependencies(extensionWrappers, wrappedComponents);
  return extensionWrappers;
}

代码示例来源:origin: software.amazon.jsii/jsii-runtime

&& klass.getDeclaredAnnotationsByType(Jsii.class).length == 0
&& klass != Object.class) {

代码示例来源:origin: lucko/helper

/**
 * Resolves all {@link MavenLibrary} annotations on the given class.
 *
 * @param clazz the class to load libraries for.
 */
public static void loadAll(Class<?> clazz) {
  MavenLibrary[] libs = clazz.getDeclaredAnnotationsByType(MavenLibrary.class);
  if (libs == null) {
    return;
  }
  for (MavenLibrary lib : libs) {
    load(lib.groupId(), lib.artifactId(), lib.version(), lib.repo().url());
  }
}

代码示例来源:origin: me.lucko/helper

/**
 * Resolves all {@link MavenLibrary} annotations on the given class.
 *
 * @param clazz the class to load libraries for.
 */
public static void loadAll(Class<?> clazz) {
  MavenLibrary[] libs = clazz.getDeclaredAnnotationsByType(MavenLibrary.class);
  if (libs == null) {
    return;
  }
  for (MavenLibrary lib : libs) {
    load(lib.groupId(), lib.artifactId(), lib.version(), lib.repo().url());
  }
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get all the annotations of the given type.
 *
 * @param <A> annotation type
 * @param annotationType the annotation type
 * @return optional with annotation, empty optional otherwise
 */
default <A extends Annotation> List<A> getAnnotations(Class<A> annotationType) {
  T type = getAnnotatedElement();
  A[] declaredAnnotations = type.getDeclaredAnnotationsByType(annotationType);
  ImmutableList.Builder<A> listBuilder = ImmutableList.builder();
  listBuilder.add(declaredAnnotations);
  for (Annotation annotation : type.getDeclaredAnnotations()) {
    Class<? extends Annotation> declaredAnnotationType = annotation.annotationType();
    Bundle bundle = declaredAnnotationType.getDeclaredAnnotation(Bundle.class);
    if (bundle != null) {
      A[] bundleAnnotations =
          declaredAnnotationType.getDeclaredAnnotationsByType(annotationType);
      listBuilder.add(bundleAnnotations);
    }
  }
  return listBuilder.build();
}

代码示例来源:origin: kumuluz/kumuluzee

EeComponentDependency[] dependencies = c.getClass().getDeclaredAnnotationsByType
    (EeComponentDependency.class);
EeComponentOptional[] optionals = c.getClass().getDeclaredAnnotationsByType(EeComponentOptional.class);

相关文章

Class类方法