AnnotationVerifier { void verify(TypeElement type) { Validated validatedAnnotation = type.getAnnotation(Validated.class); if (validatedAnnotation == null) { abortWithError("Annotation processor for @" + Validated.class.getSimpleName() + " was invoked with a type that does not have the annotation.", type); return; } if (type.getModifiers().contains(Modifier.PRIVATE)) { abortWithError("Class is private. It must be at least package private", type); } TypeMirror factoryMirror = getTypeMirrorFromAnnotation(validatedAnnotation, type); if (type.getModifiers().contains(Modifier.PROTECTED) || type.getModifiers().contains(Modifier.DEFAULT)) { if (!CompilerUtils.packageNameOf(type).equals( CompilerUtils.packageNameOf(types.asElement(factoryMirror)))) { abortWithError(type.getSimpleName() + " is not visible to " + factoryMirror.toString(), type); } } if (implementsAnnotation(type)) { abortWithError("@" + Validated.class.getSimpleName() + " may not be used to implement an " + "annotation interface.", type); } checkFactoryClass(factoryMirror, type); verifyAnnotationConflicts(type); verifyAnnotations(type); } AnnotationVerifier(Messager messager, Elements elements, Types types);   }
@Test(expected = AbortProcessingException.class) public void verify_whenTypeElementDoesNotHaveAnnotation_shouldAbortWithError() { Messager messager = mock(Messager.class); Elements element = mock(Elements.class); Types types = mock(Types.class); AnnotationVerifier verifier = new AnnotationVerifier(messager, element, types); TypeElement typeElement = mock(TypeElement.class); verifier.verify(typeElement); verify(messager).printMessage(eq(Diagnostic.Kind.ERROR), anyString(), eq(typeElement)); }
@Test(expected = AbortProcessingException.class) public void verify_whenTypeElementIsPackagePrivate_shouldAbortWithError() { Set<Modifier> modSet = new HashSet<>(); modSet.add(Modifier.PROTECTED); Messager messager = mock(Messager.class); Elements element = mock(Elements.class); Types types = mock(Types.class); AnnotationVerifier verifier = new AnnotationVerifier(messager, element, types); PackageElement packageElement = mock(PackageElement.class); Name name = mock(Name.class); when(packageElement.getQualifiedName()).thenReturn(name); when(name.toString()).thenReturn("Nonmatching.package"); TypeElement typeElement = mock(TypeElement.class); when(typeElement.getModifiers()).thenReturn(modSet); when(typeElement.getEnclosingElement()).thenReturn(packageElement); verifier.verify(typeElement); verify(messager).printMessage(eq(Diagnostic.Kind.ERROR), anyString(), eq(typeElement)); }