use of javax.lang.model.element.AnnotationMirror in project j2objc by google.
the class ElementUtilTest method testGetAnnotationValue.
public void testGetAnnotationValue() throws IOException {
CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
AbstractTypeDeclaration decl = unit.getTypes().get(0);
TypeElement element = decl.getTypeElement();
AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
Object value = ElementUtil.getAnnotationValue(annotation, "value");
assertEquals("E", value);
}
use of javax.lang.model.element.AnnotationMirror in project error-prone by google.
the class RequiredAnnotationProcessor method validateElement.
private void validateElement(final Element element) {
TypeMirror requiredAnnotationTypeMirror = processingEnv.getElementUtils().getTypeElement(RequiredAnnotation.class.getName()).asType();
for (final AnnotationMirror annotation : processingEnv.getElementUtils().getAllAnnotationMirrors(element)) {
AnnotationMirror requiredAnnotationMirror = getAnnotationMirror(annotation.getAnnotationType().asElement(), requiredAnnotationTypeMirror);
if (requiredAnnotationMirror == null) {
continue;
}
AnnotationValue value = getAnnotationValue(requiredAnnotationMirror, "value");
if (value == null) {
continue;
}
new SimpleAnnotationValueVisitor7<Void, Void>() {
@Override
public Void visitType(TypeMirror t, Void p) {
if (getAnnotationMirror(element, t) == null) {
printError(element, annotation, "Annotation %s on %s also requires %s", annotation, element, t);
}
return null;
}
@Override
public Void visitArray(List<? extends AnnotationValue> vals, Void p) {
for (AnnotationValue val : vals) {
visit(val);
}
return null;
}
}.visit(value);
}
validateElements(element.getEnclosedElements());
}
use of javax.lang.model.element.AnnotationMirror in project tiger by google.
the class TigerDaggerGeneratorProcessor method getMembersInjectorScope.
/**
* Returns the {@link DeclaredType} of the scope class of the
* {@link MembersInjector} specified.
*/
private DeclaredType getMembersInjectorScope(DeclaredType membersInjectorType) {
ExecutableElement scopeElement = null;
TypeElement membersInjectorTypeElement = elementUtils.getTypeElement(MembersInjector.class.getCanonicalName());
for (Element element : membersInjectorTypeElement.getEnclosedElements()) {
if (element.getSimpleName().contentEquals("scope")) {
scopeElement = (ExecutableElement) element;
}
}
Preconditions.checkNotNull(scopeElement);
for (AnnotationMirror annotationMirror : membersInjectorType.asElement().getAnnotationMirrors()) {
if (annotationMirror.getAnnotationType().asElement().equals(elementUtils.getTypeElement(MembersInjector.class.getCanonicalName()))) {
return (DeclaredType) annotationMirror.getElementValues().get(scopeElement).getValue();
}
}
throw new RuntimeException(String.format("Scope not found for MembersInjector: %s", membersInjectorType));
}
use of javax.lang.model.element.AnnotationMirror in project tiger by google.
the class TigerDaggerGeneratorProcessor method getModulesInComponents.
private void getModulesInComponents(Collection<? extends Element> components, SetMultimap<ComponentInfo, TypeElement> scopeModules, Set<TypeElement> unscopedModules) {
Set<TypeElement> modules = new HashSet<>();
for (Element component : components) {
AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
for (AnnotationValue annotationValue : (List<AnnotationValue>) Utils.getAnnotationValue(componentAnnotationMirror, "modules").getValue()) {
modules.add((TypeElement) ((DeclaredType) annotationValue.getValue()).asElement());
}
}
modules = Utils.findAllModulesRecursively(modules);
for (TypeElement module : modules) {
TypeElement scopeType = Utils.getModuleScope((DeclaredType) module.asType());
if (scopeType != null) {
scopeModules.put(new ComponentInfo(scopeType), module);
} else {
unscopedModules.add(module);
}
}
}
use of javax.lang.model.element.AnnotationMirror in project tiger by google.
the class TigerDaggerGeneratorProcessor method getScopeForComponent.
/**
* Returns scope for the give dagger Component, null is unscoped. The result is either
* explicitly specified or implicitly inherited from one of the ancestors.
*/
@Nullable
private TypeElement getScopeForComponent(TypeElement component) {
DeclaredType scope = Utils.getScopeType(component);
if (scope != null) {
return (TypeElement) scope.asElement();
}
AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
List<AnnotationValue> dependencies = (List<AnnotationValue>) Utils.getAnnotationValue(componentAnnotationMirror, "dependencies");
if (dependencies == null) {
return null;
}
Set<TypeElement> parentScopes = new HashSet<>();
for (AnnotationValue dependency : dependencies) {
DeclaredType dependencyClass = (DeclaredType) dependency.getValue();
TypeElement parentScope = getScopeForComponent((TypeElement) dependencyClass.asElement());
if (parentScope != null) {
parentScopes.add(parentScope);
}
}
if (parentScopes.isEmpty()) {
return null;
}
if (parentScopes.size() > 1) {
messager.printMessage(Kind.ERROR, String.format("Component %s depends on more than one scoped components. The scopes are: %s", component, parentScopes));
}
return Iterables.getOnlyElement(parentScopes);
}
Aggregations