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

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

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

Class.getAnnotation介绍

暂无

代码示例

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

@Nullable
protected CacheDefaults getCacheDefaults(Method method, @Nullable Class<?> targetType) {
  CacheDefaults annotation = method.getDeclaringClass().getAnnotation(CacheDefaults.class);
  if (annotation != null) {
    return annotation;
  }
  return (targetType != null ? targetType.getAnnotation(CacheDefaults.class) : null);
}

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

@Override
@Nullable
public BeanWiringInfo resolveWiringInfo(Object beanInstance) {
  Assert.notNull(beanInstance, "Bean instance must not be null");
  Configurable annotation = beanInstance.getClass().getAnnotation(Configurable.class);
  return (annotation != null ? buildWiringInfo(beanInstance, annotation) : null);
}

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

@Override
  public boolean rollbackOn(Throwable ex) {
    ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
    return (ann != null ? ann.rollback() : super.rollbackOn(ex));
  }
}

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

@Test
public void javaxAnnotationTypeViaFindMergedAnnotation() throws Exception {
  assertEquals(ResourceHolder.class.getAnnotation(Resource.class), findMergedAnnotation(ResourceHolder.class, Resource.class));
  assertEquals(SpringAppConfigClass.class.getAnnotation(Resource.class), findMergedAnnotation(SpringAppConfigClass.class, Resource.class));
}

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

@Test
public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception {
  Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class);
  assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class));
  assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class));
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasForMetaAnnotationThatIsNotMetaPresent() throws Exception {
  AliasedComposedContextConfigNotMetaPresent annotation =
      AliasedComposedContextConfigNotMetaPresentClass.class.getAnnotation(AliasedComposedContextConfigNotMetaPresent.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("@AliasFor declaration on attribute 'xmlConfigFile' in annotation"));
  exception.expectMessage(containsString(AliasedComposedContextConfigNotMetaPresent.class.getName()));
  exception.expectMessage(containsString("declares an alias for attribute 'location' in meta-annotation"));
  exception.expectMessage(containsString(ContextConfig.class.getName()));
  exception.expectMessage(containsString("not meta-present"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void getAnnotationAttributesWithNestedAnnotations() {
  ComponentScan componentScan = ComponentScanClass.class.getAnnotation(ComponentScan.class);
  assertNotNull(componentScan);
  AnnotationAttributes attributes = getAnnotationAttributes(ComponentScanClass.class, componentScan);
  assertNotNull(attributes);
  assertEquals(ComponentScan.class, attributes.annotationType());
  Filter[] filters = attributes.getAnnotationArray("excludeFilters", Filter.class);
  assertNotNull(filters);
  List<String> patterns = stream(filters).map(Filter::pattern).collect(toList());
  assertEquals(asList("*Foo", "*Bar"), patterns);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasForAttributeWithDifferentDefaultValue() throws Exception {
  AliasForAttributeWithDifferentDefaultValue annotation =
      AliasForAttributeWithDifferentDefaultValueClass.class.getAnnotation(AliasForAttributeWithDifferentDefaultValue.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("Misconfigured aliases"));
  exception.expectMessage(containsString(AliasForAttributeWithDifferentDefaultValue.class.getName()));
  exception.expectMessage(containsString("attribute 'foo' in annotation"));
  exception.expectMessage(containsString("attribute 'bar' in annotation"));
  exception.expectMessage(containsString("same default value"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasForAttributeOfDifferentType() throws Exception {
  AliasForAttributeOfDifferentType annotation =
      AliasForAttributeOfDifferentTypeClass.class.getAnnotation(AliasForAttributeOfDifferentType.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("Misconfigured aliases"));
  exception.expectMessage(containsString(AliasForAttributeOfDifferentType.class.getName()));
  exception.expectMessage(containsString("attribute 'foo'"));
  exception.expectMessage(containsString("attribute 'bar'"));
  exception.expectMessage(containsString("same return type"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasForWithMissingDefaultValues() throws Exception {
  AliasForWithMissingDefaultValues annotation =
      AliasForWithMissingDefaultValuesClass.class.getAnnotation(AliasForWithMissingDefaultValues.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("Misconfigured aliases"));
  exception.expectMessage(containsString(AliasForWithMissingDefaultValues.class.getName()));
  exception.expectMessage(containsString("attribute 'foo' in annotation"));
  exception.expectMessage(containsString("attribute 'bar' in annotation"));
  exception.expectMessage(containsString("default values"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWhereAliasForIsMissingAttributeDeclaration() throws Exception {
  AliasForWithMissingAttributeDeclaration annotation = AliasForWithMissingAttributeDeclarationClass.class.getAnnotation(AliasForWithMissingAttributeDeclaration.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("@AliasFor declaration on attribute 'foo' in annotation"));
  exception.expectMessage(containsString(AliasForWithMissingAttributeDeclaration.class.getName()));
  exception.expectMessage(containsString("points to itself"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWhereAliasForHasDuplicateAttributeDeclaration() throws Exception {
  AliasForWithDuplicateAttributeDeclaration annotation = AliasForWithDuplicateAttributeDeclarationClass.class.getAnnotation(AliasForWithDuplicateAttributeDeclaration.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("In @AliasFor declared on attribute 'foo' in annotation"));
  exception.expectMessage(containsString(AliasForWithDuplicateAttributeDeclaration.class.getName()));
  exception.expectMessage(containsString("attribute 'attribute' and its alias 'value' are present with values of [baz] and [bar]"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasWithoutMirroredAliasFor() throws Exception {
  AliasForWithoutMirroredAliasFor annotation =
      AliasForWithoutMirroredAliasForClass.class.getAnnotation(AliasForWithoutMirroredAliasFor.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("Attribute 'bar' in"));
  exception.expectMessage(containsString(AliasForWithoutMirroredAliasFor.class.getName()));
  exception.expectMessage(containsString("@AliasFor [foo]"));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasWithMirroredAliasForWrongAttribute() throws Exception {
  AliasForWithMirroredAliasForWrongAttribute annotation =
      AliasForWithMirroredAliasForWrongAttributeClass.class.getAnnotation(AliasForWithMirroredAliasForWrongAttribute.class);
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(startsWith("Attribute 'bar' in"));
  exception.expectMessage(containsString(AliasForWithMirroredAliasForWrongAttribute.class.getName()));
  exception.expectMessage(either(containsString("must be declared as an @AliasFor [foo], not [quux]")).
      or(containsString("is declared as an @AliasFor nonexistent attribute 'quux'")));
  synthesizeAnnotation(annotation);
}

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

@Test
public void synthesizeAnnotationWithAttributeAliasesWithDifferentValues() throws Exception {
  ContextConfig contextConfig = synthesizeAnnotation(ContextConfigMismatch.class.getAnnotation(ContextConfig.class));
  exception.expect(AnnotationConfigurationException.class);
  getValue(contextConfig);
}

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

@Test
public void synthesizeAnnotationWithTransitiveImplicitAliasesForAliasPair() throws Exception {
  Class<?> clazz = TransitiveImplicitAliasesForAliasPairContextConfigClass.class;
  TransitiveImplicitAliasesForAliasPairContextConfig config = clazz.getAnnotation(TransitiveImplicitAliasesForAliasPairContextConfig.class);
  assertNotNull(config);
  TransitiveImplicitAliasesForAliasPairContextConfig synthesizedConfig = synthesizeAnnotation(config);
  assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class));
  assertEquals("xml: ", "test.xml", synthesizedConfig.xml());
  assertEquals("groovy: ", "test.xml", synthesizedConfig.groovy());
}

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

@Test
public void synthesizeAnnotationWithImplicitAliasesForAliasPair() throws Exception {
  Class<?> clazz = ImplicitAliasesForAliasPairContextConfigClass.class;
  ImplicitAliasesForAliasPairContextConfig config = clazz.getAnnotation(ImplicitAliasesForAliasPairContextConfig.class);
  assertNotNull(config);
  ImplicitAliasesForAliasPairContextConfig synthesizedConfig = synthesizeAnnotation(config);
  assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class));
  assertEquals("xmlFile: ", "test.xml", synthesizedConfig.xmlFile());
  assertEquals("groovyScript: ", "test.xml", synthesizedConfig.groovyScript());
}

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

@Test
public void synthesizeAnnotationWithTransitiveImplicitAliases() throws Exception {
  Class<?> clazz = TransitiveImplicitAliasesContextConfigClass.class;
  TransitiveImplicitAliasesContextConfig config = clazz.getAnnotation(TransitiveImplicitAliasesContextConfig.class);
  assertNotNull(config);
  TransitiveImplicitAliasesContextConfig synthesizedConfig = synthesizeAnnotation(config);
  assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class));
  assertEquals("xml: ", "test.xml", synthesizedConfig.xml());
  assertEquals("groovy: ", "test.xml", synthesizedConfig.groovy());
}

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

private void assertAnnotationSynthesisWithImplicitAliasesWithImpliedAliasNamesOmitted(
    Class<?> clazz, String expected) {
  ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig config = clazz.getAnnotation(
      ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig.class);
  assertNotNull(config);
  ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig synthesizedConfig = synthesizeAnnotation(config);
  assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class));
  assertEquals("value: ", expected, synthesizedConfig.value());
  assertEquals("locations: ", expected, synthesizedConfig.location());
  assertEquals("xmlFiles: ", expected, synthesizedConfig.xmlFile());
}

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

@Test
public void synthesizeAnnotationWithoutAttributeAliases() throws Exception {
  Component component = WebController.class.getAnnotation(Component.class);
  assertNotNull(component);
  Component synthesizedComponent = synthesizeAnnotation(component);
  assertNotNull(synthesizedComponent);
  assertSame(component, synthesizedComponent);
  assertEquals("value attribute: ", "webController", synthesizedComponent.value());
}

相关文章

Class类方法