use of javax.lang.model.element.AnnotationMirror in project dagger by square.
the class ValidationProcessor method validateScoping.
private void validateScoping(Element element) {
boolean suppressWarnings = element.getAnnotation(SuppressWarnings.class) != null && Arrays.asList(element.getAnnotation(SuppressWarnings.class).value()).contains("scoping");
int numberOfScopingAnnotationsOnElement = 0;
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (annotation.getAnnotationType().asElement().getAnnotation(Scope.class) == null) {
continue;
}
switch(element.getKind()) {
case METHOD:
numberOfScopingAnnotationsOnElement++;
if (!isProvidesMethod(element) && !suppressWarnings) {
warning("Dagger will ignore scoping annotations on methods that are not " + "@Provides methods: " + elementToString(element), element);
}
break;
case CLASS:
if (!element.getModifiers().contains(ABSTRACT)) {
numberOfScopingAnnotationsOnElement++;
break;
}
// fall through if abstract
default:
error("Scoping annotations are only allowed on concrete types and @Provides methods: " + elementToString(element), element);
}
}
if (numberOfScopingAnnotationsOnElement > 1) {
error("Only one scoping annotation is allowed per element: " + elementToString(element), element);
}
}
use of javax.lang.model.element.AnnotationMirror in project dagger by square.
the class ValidationProcessor method validateQualifiers.
private void validateQualifiers(Element element, Map<Element, Element> parametersToTheirMethods) {
boolean suppressWarnings = element.getAnnotation(SuppressWarnings.class) != null && Arrays.asList(element.getAnnotation(SuppressWarnings.class).value()).contains("qualifiers");
int numberOfQualifiersOnElement = 0;
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (annotation.getAnnotationType().asElement().getAnnotation(Qualifier.class) == null) {
continue;
}
switch(element.getKind()) {
case FIELD:
numberOfQualifiersOnElement++;
if (element.getAnnotation(Inject.class) == null && !suppressWarnings) {
warning("Dagger will ignore qualifier annotations on fields that are not " + "annotated with @Inject: " + elementToString(element), element);
}
break;
case METHOD:
numberOfQualifiersOnElement++;
if (!isProvidesMethod(element) && !suppressWarnings) {
warning("Dagger will ignore qualifier annotations on methods that are not " + "@Provides methods: " + elementToString(element), element);
}
break;
case PARAMETER:
numberOfQualifiersOnElement++;
if (!isInjectableConstructorParameter(element, parametersToTheirMethods) && !isProvidesMethodParameter(element, parametersToTheirMethods) && !suppressWarnings) {
warning("Dagger will ignore qualifier annotations on parameters that are not " + "@Inject constructor parameters or @Provides method parameters: " + elementToString(element), element);
}
break;
default:
error("Qualifier annotations are only allowed on fields, methods, and parameters: " + elementToString(element), element);
}
}
if (numberOfQualifiersOnElement > 1) {
error("Only one qualifier annotation is allowed per element: " + elementToString(element), element);
}
}
use of javax.lang.model.element.AnnotationMirror in project commons by twitter.
the class CmdLineProcessor method processVerifiers.
private void processVerifiers(Set<? extends Element> elements) {
TypeElement verifierType = typeElement(Verifier.class);
TypeElement verifierForType = typeElement(VerifierFor.class);
for (Element element : elements) {
if (element.getKind() != ElementKind.CLASS) {
error("Found a @VerifierFor annotation on a non-class %s", element);
} else {
TypeElement verifier = (TypeElement) element;
if (!isAssignable(verifier, Verifier.class)) {
error("Found a @Verifier annotation on a non-Verifier %s", element);
return;
}
String verifierClassName = getBinaryName(verifier);
@Nullable AnnotationMirror verifierFor = getAnnotationMirror(verifier, verifierForType);
if (verifierFor != null) {
@Nullable TypeElement verifyAnnotationType = getClassType(verifierFor, "value", null);
if (verifyAnnotationType != null) {
@Nullable String verifiedType = getTypeArgument(verifier, verifierType);
if (verifiedType != null) {
String verifyAnnotationClassName = elementUtils.getBinaryName(verifyAnnotationType).toString();
getBuilder(verifierClassName).addVerifier(verifiedType, verifyAnnotationClassName, verifierClassName);
}
}
}
}
}
}
use of javax.lang.model.element.AnnotationMirror in project classindex by atteo.
the class ClassIndexProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
try {
for (Element element : roundEnv.getRootElements()) {
if (!(element instanceof TypeElement)) {
continue;
}
final PackageElement packageElement = getPackage(element);
element.accept(new ElementScanner6<Void, Void>() {
@Override
public Void visitType(TypeElement typeElement, Void o) {
try {
for (AnnotationMirror mirror : typeElement.getAnnotationMirrors()) {
final TypeElement annotationElement = (TypeElement) mirror.getAnnotationType().asElement();
storeAnnotation(annotationElement, typeElement);
}
indexSupertypes(typeElement, typeElement);
if (packageElement != null) {
storeClassFromPackage(packageElement, typeElement);
}
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] " + e.getMessage());
}
return super.visitType(typeElement, o);
}
}, null);
}
if (!roundEnv.processingOver()) {
return false;
}
writeIndexFiles(ClassIndex.SUBCLASS_INDEX_PREFIX, subclassMap);
writeIndexFiles(ClassIndex.ANNOTATED_INDEX_PREFIX, annotatedMap);
for (Map.Entry<String, Set<String>> entry : packageMap.entrySet()) {
writeSimpleNameIndexFile(entry.getValue(), entry.getKey().replace(".", "/") + "/" + ClassIndex.PACKAGE_INDEX_NAME);
}
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Can't write index file: " + e.getMessage());
} catch (Throwable e) {
e.printStackTrace();
messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Internal error: " + e.getMessage());
}
return false;
}
use of javax.lang.model.element.AnnotationMirror in project bazel by bazelbuild.
the class PurityUtils method getPurityKinds.
/**
* @return The types of purity of the method {@code methodElement}.
*/
public static List<Pure.Kind> getPurityKinds(AnnotationProvider provider, Element methodElement) {
AnnotationMirror pureAnnotation = provider.getDeclAnnotation(methodElement, Pure.class);
AnnotationMirror sefAnnotation = provider.getDeclAnnotation(methodElement, SideEffectFree.class);
AnnotationMirror detAnnotation = provider.getDeclAnnotation(methodElement, Deterministic.class);
List<Pure.Kind> kinds = new ArrayList<>();
if (pureAnnotation != null) {
kinds.add(Kind.DETERMINISTIC);
kinds.add(Kind.SIDE_EFFECT_FREE);
}
if (sefAnnotation != null) {
kinds.add(Kind.SIDE_EFFECT_FREE);
}
if (detAnnotation != null) {
kinds.add(Kind.DETERMINISTIC);
}
return kinds;
}
Aggregations