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

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

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

Class.getDeclaredAnnotationsByType介绍

暂无

代码示例

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

  1. @Test
  2. public void getDeclaredRepeatableAnnotationsDeclaredOnClass() {
  3. final List<String> expectedValuesJava = asList("A", "B", "C");
  4. final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1");
  5. // Java 8
  6. MyRepeatable[] array = MyRepeatableClass.class.getDeclaredAnnotationsByType(MyRepeatable.class);
  7. assertNotNull(array);
  8. List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
  9. assertThat(values, is(expectedValuesJava));
  10. // Spring
  11. Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
  12. assertNotNull(set);
  13. values = set.stream().map(MyRepeatable::value).collect(toList());
  14. assertThat(values, is(expectedValuesSpring));
  15. // When container type is omitted and therefore inferred from @Repeatable
  16. set = getDeclaredRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class);
  17. assertNotNull(set);
  18. values = set.stream().map(MyRepeatable::value).collect(toList());
  19. assertThat(values, is(expectedValuesSpring));
  20. }

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

  1. @Test
  2. public void getDeclaredRepeatableAnnotationsDeclaredOnSuperclass() {
  3. final Class<?> clazz = SubMyRepeatableClass.class;
  4. // Java 8
  5. MyRepeatable[] array = clazz.getDeclaredAnnotationsByType(MyRepeatable.class);
  6. assertNotNull(array);
  7. assertThat(array.length, is(0));
  8. // Spring
  9. Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
  10. assertNotNull(set);
  11. assertThat(set.size(), is(0));
  12. // When container type is omitted and therefore inferred from @Repeatable
  13. set = getDeclaredRepeatableAnnotations(clazz, MyRepeatable.class);
  14. assertNotNull(set);
  15. assertThat(set.size(), is(0));
  16. }

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

  1. /**
  2. * Attempts to find the @Jsii annotation from a type.
  3. * @param type The type.
  4. * @param inherited If 'true' will look for the annotation up the class hierarchy.
  5. * @return The annotation or null.
  6. */
  7. static Jsii tryGetJsiiAnnotation(final Class<?> type, final boolean inherited) {
  8. Jsii[] ann;
  9. if (inherited) {
  10. ann = (Jsii[]) type.getAnnotationsByType(Jsii.class);
  11. } else {
  12. ann = (Jsii[]) type.getDeclaredAnnotationsByType(Jsii.class);
  13. }
  14. if (ann.length == 0) {
  15. return null;
  16. }
  17. return ann[0];
  18. }

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

  1. public void real(Set<FieldDescriptor> fieldDescriptors) {
  2. Class<?> testClass = context.getTestClass();
  3. of(testClass.getDeclaredAnnotationsByType(Module.class))
  4. .map(Module::value)
  5. .distinct()
  6. .forEachOrdered(locator::addModule);
  7. locator.reload();
  8. testReifier.reifyTest(fieldDescriptors);
  9. }

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

  1. public void real(Set<FieldDescriptor> fieldDescriptors) {
  2. Class<?> testClass = context.getTestClass();
  3. of(testClass.getDeclaredAnnotationsByType(Module.class))
  4. .map(Module::value)
  5. .distinct()
  6. .forEachOrdered(locator::addModule);
  7. locator.reload();
  8. testReifier.reifyTest(fieldDescriptors);
  9. }

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

  1. public void real(Set<FieldDescriptor> fieldDescriptors) {
  2. Class<?> testClass = context.getTestClass();
  3. of(testClass.getDeclaredAnnotationsByType(Module.class))
  4. .map(Module::value)
  5. .distinct()
  6. .forEachOrdered(locator::addModule);
  7. locator.reload();
  8. testReifier.reifyTest(fieldDescriptors);
  9. }

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

  1. public void real(Set<FieldDescriptor> fieldDescriptors) {
  2. Class<?> testClass = context.getTestClass();
  3. of(testClass.getDeclaredAnnotationsByType(Module.class))
  4. .map(Module::value)
  5. .distinct()
  6. .forEachOrdered(locator::addModule);
  7. locator.reload();
  8. testReifier.reifyTest(fieldDescriptors);
  9. }

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

  1. private <E extends Extension> List<ExtensionWrapper<E>> processSingleEeExtensions(List<E> extensions, List<EeComponentWrapper>
  2. wrappedComponents) {
  3. List<ExtensionWrapper<E>> extensionWrappers = new ArrayList<>();
  4. for (E e : extensions) {
  5. EeExtensionDef def = e.getClass().getDeclaredAnnotation(EeExtensionDef.class);
  6. if (def != null) {
  7. EeComponentDependency[] dependencies = e.getClass().getDeclaredAnnotationsByType(EeComponentDependency.class);
  8. EeComponentOptional[] optionals = e.getClass().getDeclaredAnnotationsByType(EeComponentOptional.class);
  9. extensionWrappers.add(new ExtensionWrapper<>(e, def.name(), def.group(), dependencies, optionals));
  10. }
  11. }
  12. log.info("Processing EE single extensions dependencies");
  13. processEeExtensionDependencies(extensionWrappers, wrappedComponents);
  14. return extensionWrappers;
  15. }

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

  1. @Override
  2. public void preMappingEntity(MetadataContext context, EntityMappingBuilder emb) throws MetadataException {
  3. Class<?> sourceClass = emb.getSourceClass();
  4. if(null != sourceClass){
  5. mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(Entity.class));
  6. mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(Table.class));
  7. mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(AutoCreateTable.class));
  8. mappingEntityByAnnotation(context, emb, sourceClass.getAnnotation(AutoGenerateColumns.class));
  9. mappingListenerByAnnotations(context, emb, sourceClass.getDeclaredAnnotationsByType(Entity.Listener.class));
  10. mappingManyToOneByClassAnnotation(context, emb, sourceClass.getDeclaredAnnotationsByType(ManyToOne.class));
  11. mappingManyToManyByClassAnnotation(context, emb, sourceClass.getDeclaredAnnotationsByType(ManyToMany.class));
  12. }
  13. }

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

  1. of(testClass.getDeclaredAnnotationsByType(Module.class))
  2. .map(Module::value)
  3. .distinct()
  4. .forEachOrdered(locator::addModule);
  5. of(testClass.getDeclaredAnnotationsByType(Scan.class))
  6. .map(Scan::value)
  7. .distinct()

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

  1. of(testClass.getDeclaredAnnotationsByType(Module.class))
  2. .map(Module::value)
  3. .distinct()
  4. .forEachOrdered(locator::addModule);
  5. of(testClass.getDeclaredAnnotationsByType(Scan.class))
  6. .map(Scan::value)
  7. .distinct()

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

  1. of(testClass.getDeclaredAnnotationsByType(Module.class))
  2. .map(Module::value)
  3. .distinct()
  4. .forEachOrdered(locator::addModule);
  5. of(testClass.getDeclaredAnnotationsByType(Scan.class))
  6. .map(Scan::value)
  7. .distinct()

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

  1. of(testClass.getDeclaredAnnotationsByType(Module.class))
  2. .map(Module::value)
  3. .distinct()
  4. .forEachOrdered(locator::addModule);
  5. of(testClass.getDeclaredAnnotationsByType(Scan.class))
  6. .map(Scan::value)
  7. .distinct()

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

  1. private synchronized Collection<CompositeIndex> initCompositeIndexFields() {
  2. // init property fields to be able to check existence of properties
  3. propertyFields();
  4. if (cls == null) {
  5. try {
  6. cls = Class.forName(className, false, Thread.currentThread().getContextClassLoader());
  7. } catch (ClassNotFoundException e) {
  8. throw new RuntimeException("Could not get annotation info for class " + className, e);
  9. }
  10. }
  11. CompositeIndex[] annotations = cls.getDeclaredAnnotationsByType(CompositeIndex.class);
  12. ArrayList<CompositeIndex> result = new ArrayList<>(annotations.length);
  13. for (CompositeIndex annotation : annotations) {
  14. String[] properties = annotation.value().length > 0 ? annotation.value() : annotation.properties();
  15. if (properties.length < 1) {
  16. throw new MetadataException("Incorrect CompositeIndex definition on " + className +
  17. ". Provide at least 1 property");
  18. }
  19. for (String property : properties) {
  20. FieldInfo fieldInfo = propertyFields.get(property);
  21. if (fieldInfo == null) {
  22. throw new MetadataException("Incorrect CompositeIndex definition on " + className + ". Property " +
  23. property + " does not exists.");
  24. }
  25. }
  26. result.add(annotation);
  27. }
  28. return result;
  29. }

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

  1. private <E extends Extension> List<ExtensionWrapper<E>> processGroupEeExtensions(List<E> extensions, List<EeComponentWrapper>
  2. wrappedComponents) {
  3. Map<String, ExtensionWrapper<E>> eeExt = new HashMap<>();
  4. // Wrap extensions with their metadata and check for duplicates
  5. for (E e : extensions) {
  6. EeExtensionDef def = e.getClass().getDeclaredAnnotation(EeExtensionDef.class);
  7. if (def != null) {
  8. if (eeExt.containsKey(def.group())) {
  9. String msg = "Found multiple implementations (" + eeExt.get(def.group()).getName() +
  10. ", " + def.name() + ") of the same EE extension group (" + def.group() + "). " +
  11. "Please check to make sure you only include a single implementation of a specific " +
  12. "EE extension group.";
  13. log.severe(msg);
  14. throw new KumuluzServerException(msg);
  15. }
  16. EeComponentDependency[] dependencies = e.getClass().getDeclaredAnnotationsByType
  17. (EeComponentDependency.class);
  18. EeComponentOptional[] optionals = e.getClass().getDeclaredAnnotationsByType(EeComponentOptional.class);
  19. eeExt.put(def.group(), new ExtensionWrapper<>(e, def.name(), def.group(), dependencies, optionals));
  20. }
  21. }
  22. List<ExtensionWrapper<E>> extensionWrappers = new ArrayList<>(eeExt.values());
  23. log.info("Processing EE extension dependencies");
  24. processEeExtensionDependencies(extensionWrappers, wrappedComponents);
  25. return extensionWrappers;
  26. }

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

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

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

  1. /**
  2. * Resolves all {@link MavenLibrary} annotations on the given class.
  3. *
  4. * @param clazz the class to load libraries for.
  5. */
  6. public static void loadAll(Class<?> clazz) {
  7. MavenLibrary[] libs = clazz.getDeclaredAnnotationsByType(MavenLibrary.class);
  8. if (libs == null) {
  9. return;
  10. }
  11. for (MavenLibrary lib : libs) {
  12. load(lib.groupId(), lib.artifactId(), lib.version(), lib.repo().url());
  13. }
  14. }

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

  1. /**
  2. * Resolves all {@link MavenLibrary} annotations on the given class.
  3. *
  4. * @param clazz the class to load libraries for.
  5. */
  6. public static void loadAll(Class<?> clazz) {
  7. MavenLibrary[] libs = clazz.getDeclaredAnnotationsByType(MavenLibrary.class);
  8. if (libs == null) {
  9. return;
  10. }
  11. for (MavenLibrary lib : libs) {
  12. load(lib.groupId(), lib.artifactId(), lib.version(), lib.repo().url());
  13. }
  14. }

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

  1. /**
  2. * Get all the annotations of the given type.
  3. *
  4. * @param <A> annotation type
  5. * @param annotationType the annotation type
  6. * @return optional with annotation, empty optional otherwise
  7. */
  8. default <A extends Annotation> List<A> getAnnotations(Class<A> annotationType) {
  9. T type = getAnnotatedElement();
  10. A[] declaredAnnotations = type.getDeclaredAnnotationsByType(annotationType);
  11. ImmutableList.Builder<A> listBuilder = ImmutableList.builder();
  12. listBuilder.add(declaredAnnotations);
  13. for (Annotation annotation : type.getDeclaredAnnotations()) {
  14. Class<? extends Annotation> declaredAnnotationType = annotation.annotationType();
  15. Bundle bundle = declaredAnnotationType.getDeclaredAnnotation(Bundle.class);
  16. if (bundle != null) {
  17. A[] bundleAnnotations =
  18. declaredAnnotationType.getDeclaredAnnotationsByType(annotationType);
  19. listBuilder.add(bundleAnnotations);
  20. }
  21. }
  22. return listBuilder.build();
  23. }

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

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

相关文章

Class类方法