use of javax.lang.model.element.AnnotationValue in project tiger by google.
the class TigerDaggerGeneratorProcessor method getComponentDependencies.
/**
* Returns dependency components of the give component, empty set if none.
*/
private Set<TypeElement> getComponentDependencies(TypeElement component) {
Set<TypeElement> result = new HashSet<>();
AnnotationMirror componentAnnotationMirror = Preconditions.checkNotNull(Utils.getAnnotationMirror(component, Component.class), String.format("@Component not found for %s", component));
AnnotationValue dependenciesAnnotationValue = Utils.getAnnotationValue(componentAnnotationMirror, "dependencies");
if (dependenciesAnnotationValue == null) {
return result;
}
List<? extends AnnotationValue> dependencies = (List<? extends AnnotationValue>) dependenciesAnnotationValue.getValue();
for (AnnotationValue dependency : dependencies) {
result.add((TypeElement) ((DeclaredType) dependency.getValue()).asElement());
}
return result;
}
use of javax.lang.model.element.AnnotationValue in project tiger by google.
the class TigerDaggerGeneratorProcessor method verifyComponents.
/**
* Not all combination of components are allowed. Here are the limitation.
*
* <pre>
* 1. All components must be scoped.
* </pre>
*
* <pre>
* 2. There is at most one dependencies component. This is automatically fulfilled for subcomponents.
* </pre>
*
* <pre>
* 3. Only one root. Forest not supported.
* </pre>
*
* All exceptions are easy to fix, if any. This map to the core injectors very well. Modules
* needed by a core injector but not needed by related component(s) can just be passed in as null
* because it will never be used.
*/
private void verifyComponents(Set<TypeElement> components) {
TypeElement root = null;
for (TypeElement component : components) {
Preconditions.checkNotNull(Utils.getScopeType(component), String.format("Unscoped component supported : %s", component));
AnnotationMirror annotationMirror = Utils.getAnnotationMirror(component, Component.class);
List<AnnotationValue> dependencies = null;
AnnotationValue annotationValue = Utils.getAnnotationValue(annotationMirror, "dependencies");
if (annotationValue != null) {
dependencies = (List<AnnotationValue>) annotationValue.getValue();
}
if (dependencies == null || dependencies.isEmpty()) {
Preconditions.checkState(root == null, String.format("More than one root components found: %s and %s", root, component));
root = component;
} else {
Preconditions.checkState(dependencies.size() == 1, String.format("More than one dependencies found for component: %s", component));
}
}
}
use of javax.lang.model.element.AnnotationValue in project tiger by google.
the class TigerDaggerGeneratorProcessor method getAllModulesOfComponentRecursively.
private Set<TypeElement> getAllModulesOfComponentRecursively(TypeElement component) {
Set<TypeElement> result = new HashSet<>();
AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
for (AnnotationValue annotationValue : (List<AnnotationValue>) Utils.getAnnotationValue(componentAnnotationMirror, "modules").getValue()) {
result.add((TypeElement) ((DeclaredType) annotationValue.getValue()).asElement());
}
result = Utils.findAllModulesRecursively(result);
return result;
}
use of javax.lang.model.element.AnnotationValue in project bazel by bazelbuild.
the class AnnotationUtils method getElementValue.
/**
* Get the attribute with the name {@code name} of the annotation
* {@code anno}. The result is expected to have type {@code expectedType}.
*
* <p>
* <em>Note 1</em>: The method does not work well for attributes of an array
* type (as it would return a list of {@link AnnotationValue}s). Use
* {@code getElementValueArray} instead.
*
* <p>
* <em>Note 2</em>: The method does not work for attributes of an enum type,
* as the AnnotationValue is a VarSymbol and would be cast to the enum type,
* which doesn't work. Use {@code getElementValueEnum} instead.
*
*
* @param anno the annotation to disassemble
* @param name the name of the attribute to access
* @param expectedType the expected type used to cast the return type
* @param useDefaults whether to apply default values to the attribute.
* @return the value of the attribute with the given name
*/
public static <T> T getElementValue(AnnotationMirror anno, CharSequence name, Class<T> expectedType, boolean useDefaults) {
Map<? extends ExecutableElement, ? extends AnnotationValue> valmap;
if (useDefaults) {
valmap = getElementValuesWithDefaults(anno);
} else {
valmap = anno.getElementValues();
}
for (ExecutableElement elem : valmap.keySet()) {
if (elem.getSimpleName().contentEquals(name)) {
AnnotationValue val = valmap.get(elem);
return expectedType.cast(val.getValue());
}
}
ErrorReporter.errorAbort("No element with name \'" + name + "\' in annotation " + anno);
// dead code
return null;
}
use of javax.lang.model.element.AnnotationValue in project glide by bumptech.
the class RootModuleGenerator method getExcludedGlideModuleClassNames.
private static Set<String> getExcludedGlideModuleClassNames(ProcessingEnvironment processingEnv, String rootGlideModuleClassName) {
TypeElement rootGlideModule = processingEnv.getElementUtils().getTypeElement(rootGlideModuleClassName);
String excludedModulesName = Excludes.class.getName();
AnnotationValue excludedModuleAnnotationValue = null;
for (AnnotationMirror annotationMirror : rootGlideModule.getAnnotationMirrors()) {
// instead. This check is necessary because a given class may have multiple Annotations.
if (!excludedModulesName.equals(annotationMirror.getAnnotationType().toString())) {
continue;
}
Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values = annotationMirror.getElementValues().entrySet();
// (usually value).
if (values.size() != 1) {
throw new IllegalArgumentException("Expected single value, but found: " + values);
}
excludedModuleAnnotationValue = values.iterator().next().getValue();
if (excludedModuleAnnotationValue == null) {
throw new NullPointerException("Failed to find Excludes#value");
}
}
// If the RootGlideModule class is not annotated, then there are no classes to exclude.
if (excludedModuleAnnotationValue == null) {
return Collections.emptySet();
}
List values = (List) excludedModuleAnnotationValue.getValue();
Set<String> result = new HashSet<>(values.size());
for (Object value : values) {
Attribute.Class clazz = (Attribute.Class) value;
result.add(clazz.getValue().toString());
}
return result;
}
Aggregations