下面列出了javax.annotation.ParametersAreNonnullByDefault#java.lang.annotation.Documented 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
assertThat(metadata.getClassName(), is(Component.class.getName()));
assertThat(metadata.isInterface(), is(true));
assertThat(metadata.isAnnotation(), is(true));
assertThat(metadata.isAbstract(), is(true));
assertThat(metadata.isConcrete(), is(false));
assertThat(metadata.hasSuperClass(), is(false));
assertThat(metadata.getSuperClassName(), nullValue());
assertThat(metadata.getInterfaceNames().length, is(1));
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(false));
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false));
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false));
assertThat(metadata.getAnnotationTypes().size(), is(1));
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
assertThat(metadata.getClassName(), is(Component.class.getName()));
assertThat(metadata.isInterface(), is(true));
assertThat(metadata.isAnnotation(), is(true));
assertThat(metadata.isAbstract(), is(true));
assertThat(metadata.isConcrete(), is(false));
assertThat(metadata.hasSuperClass(), is(false));
assertThat(metadata.getSuperClassName(), nullValue());
assertThat(metadata.getInterfaceNames().length, is(1));
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false));
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false));
assertThat(metadata.getAnnotationTypes().size(), is(4));
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
assertThat(metadata.getClassName(), is(Component.class.getName()));
assertThat(metadata.isInterface(), is(true));
assertThat(metadata.isAnnotation(), is(true));
assertThat(metadata.isAbstract(), is(true));
assertThat(metadata.isConcrete(), is(false));
assertThat(metadata.hasSuperClass(), is(false));
assertThat(metadata.getSuperClassName(), nullValue());
assertThat(metadata.getInterfaceNames().length, is(1));
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false));
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false));
assertThat(metadata.getAnnotationTypes().size(), is(3));
}
private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
Set<String> acceptable) {
for (AnnotationMirror mirror : annotations) {
Element annotationElement = mirror.getAnnotationType().asElement();
if (annotationElement.getAnnotation(Documented.class) == null) {
note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
}
verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
for (AnnotationValue value : mirror.getElementValues().values()) {
verifyAnnotationValue(value, acceptable);
}
}
}
@Test
public void testScanOtherPackage() {
//There can't be a class that is annotated with Inject
final DefaultClasspathScanner scanner = new DefaultClasspathScanner(CLASSPATH_SCAN);
Set<Class<?>> classes = scanner.getTypesAnnotatedWith(Resources.class);
assertNotNull(classes);
assertEquals(classes.size(), 0);
classes = scanner.getTypesAnnotatedWith(AnnotationForClasspathScanTest.class);
assertNotNull(classes);
assertEquals(classes.size(), 0);
classes = scanner.getTypesAnnotatedWith(Documented.class);
assertNotNull(classes);
assertEquals(classes.size(), 1);
assertTrue(classes.contains(DocumentAnnotatedClass.class));
}
@Test
public void getAnnotationMirror() {
TypeElement element =
compilation.getElements().getTypeElement(AnnotatedAnnotation.class.getCanonicalName());
Optional<AnnotationMirror> documented =
MoreElements.getAnnotationMirror(element, Documented.class);
Optional<AnnotationMirror> innerAnnotation =
MoreElements.getAnnotationMirror(element, InnerAnnotation.class);
Optional<AnnotationMirror> suppressWarnings =
MoreElements.getAnnotationMirror(element, SuppressWarnings.class);
expect.that(documented).isPresent();
expect.that(innerAnnotation).isPresent();
expect.that(suppressWarnings).isAbsent();
Element annotationElement = documented.get().getAnnotationType().asElement();
expect.that(MoreElements.isType(annotationElement)).isTrue();
expect.that(MoreElements.asType(annotationElement).getQualifiedName().toString())
.isEqualTo(Documented.class.getCanonicalName());
annotationElement = innerAnnotation.get().getAnnotationType().asElement();
expect.that(MoreElements.isType(annotationElement)).isTrue();
expect.that(MoreElements.asType(annotationElement).getQualifiedName().toString())
.isEqualTo(InnerAnnotation.class.getCanonicalName());
}
@Test
public void isAnnotationMetaPresentForPlainType() {
assertTrue(isAnnotationMetaPresent(Order.class, Documented.class));
assertTrue(isAnnotationMetaPresent(NonNullApi.class, Documented.class));
assertTrue(isAnnotationMetaPresent(NonNullApi.class, Nonnull.class));
assertTrue(isAnnotationMetaPresent(ParametersAreNonnullByDefault.class, Nonnull.class));
}
@Test
public void isAnnotatedForPlainTypes() {
assertTrue(isAnnotated(Order.class, Documented.class));
assertTrue(isAnnotated(NonNullApi.class, Documented.class));
assertTrue(isAnnotated(NonNullApi.class, Nonnull.class));
assertTrue(isAnnotated(ParametersAreNonnullByDefault.class, Nonnull.class));
}
@Test
public void hasAnnotationForPlainTypes() {
assertTrue(hasAnnotation(Order.class, Documented.class));
assertTrue(hasAnnotation(NonNullApi.class, Documented.class));
assertTrue(hasAnnotation(NonNullApi.class, Nonnull.class));
assertTrue(hasAnnotation(ParametersAreNonnullByDefault.class, Nonnull.class));
}
/**
* Given an annotation, return true if it should be documented and false
* otherwise.
*
* @param annotation the annotation to check.
*
* @return true return true if it should be documented and false otherwise.
*/
public boolean isDocumentedAnnotation(TypeElement annotation) {
for (AnnotationMirror anno : annotation.getAnnotationMirrors()) {
if (getFullyQualifiedName(anno.getAnnotationType().asElement()).equals(
Documented.class.getName())) {
return true;
}
}
return false;
}
/**
* Given an annotation, return true if it should be documented and false
* otherwise.
*
* @param annotationDoc the annotation to check.
*
* @return true return true if it should be documented and false otherwise.
*/
public boolean isDocumentedAnnotation(AnnotationTypeDoc annotationDoc) {
for (AnnotationDesc anno : annotationDoc.annotations()) {
if (anno.annotationType().qualifiedName().equals(
Documented.class.getName())) {
return true;
}
}
return false;
}
public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass) {
A res = cls.getAnnotation(annotationClass);
if (res == null) {
for (Annotation annotation : cls.getAnnotations()) {
if (annotation instanceof Documented) {
break;
}
res = getAnnotation(annotation.annotationType(), annotationClass);
if (res != null)
break;
}
}
return res;
}
private void assertForAnnotation(final DefaultClasspathScanner scanner) {
final Set<Class<?>> resourceClasses = scanner.getTypesAnnotatedWith(Resource.class);
assertNotNull(resourceClasses);
assertEquals(resourceClasses.size(), 1);
final Set<Class<?>> documentClasses = scanner.getTypesAnnotatedWith(Documented.class);
assertNotNull(documentClasses);
assertEquals(documentClasses.size(), 1);
}
@Test
public void isAnnotationPresent() {
TypeElement annotatedAnnotationElement =
compilation.getElements().getTypeElement(AnnotatedAnnotation.class.getCanonicalName());
assertThat(MoreElements.isAnnotationPresent(annotatedAnnotationElement, Documented.class))
.isTrue();
assertThat(MoreElements.isAnnotationPresent(annotatedAnnotationElement, InnerAnnotation.class))
.isTrue();
assertThat(MoreElements.isAnnotationPresent(annotatedAnnotationElement, SuppressWarnings.class))
.isFalse();
}
@Test
public void perRequest() {
// not a scope so will be considered as a not singleton bean
when(bean.getScope()).thenReturn(Class.class.cast(Documented.class));
assertNotSingleton();
}