Error if an element is annotated with both NotNull and Nullable

This commit is contained in:
Sam Harwell 2014-01-21 14:33:38 -06:00
parent f8dbe1b68f
commit e472cc4e23
1 changed files with 16 additions and 0 deletions

View File

@ -35,9 +35,11 @@ import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.HashSet;
import java.util.Set;
/**
@ -46,6 +48,10 @@ import java.util.Set;
*
* <p>The validation process checks the following items.</p>
*
* <ul>
* <li><strong>Error</strong>: an element is annotated with both {@link NotNull} and {@link Nullable}.</li>
* </ul>
*
* @author Sam Harwell
*/
@SupportedAnnotationTypes({NullUsageProcessor.NotNullClassName, NullUsageProcessor.NullableClassName})
@ -63,6 +69,16 @@ public class NullUsageProcessor extends AbstractProcessor {
return true;
}
Set<? extends Element> notNullElements = roundEnv.getElementsAnnotatedWith(processingEnv.getElementUtils().getTypeElement(NotNullClassName));
Set<? extends Element> nullableElements = roundEnv.getElementsAnnotatedWith(processingEnv.getElementUtils().getTypeElement(NullableClassName));
Set<Element> intersection = new HashSet<Element>(notNullElements);
intersection.retainAll(nullableElements);
for (Element element : intersection) {
String error = String.format("%s cannot be annotated with both NotNull and Nullable", element.getKind().toString().toLowerCase());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, element);
}
return true;
}