use of javax.lang.model.element.TypeElement in project tiger by google.
the class Utils method findAllModulesRecursively.
@SuppressWarnings("unchecked")
public static Set<TypeElement> findAllModulesRecursively(TypeElement inModule) {
Set<TypeElement> result = new HashSet<>();
result.add(inModule);
for (AnnotationMirror annotationMirror : inModule.getAnnotationMirrors()) {
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
ExecutableElement key = entry.getKey();
/** Checks for {@link Module.includes}. */
if (key.getSimpleName().contentEquals("includes")) {
for (AnnotationValue annotationValue : (List<AnnotationValue>) entry.getValue().getValue()) {
TypeElement childModule = (TypeElement) ((DeclaredType) annotationValue.getValue()).asElement();
result.addAll(findAllModulesRecursively(childModule));
}
}
}
}
return result;
}
use of javax.lang.model.element.TypeElement in project tiger by google.
the class Utils method getPackage.
public static PackageElement getPackage(TypeElement typeElement) {
Element result = typeElement.getEnclosingElement();
ElementKind elementKind = result.getKind();
while (!elementKind.equals(ElementKind.PACKAGE)) {
Preconditions.checkState(elementKind.isClass() || elementKind.isInterface(), String.format("Utils.getPackage: unexpected kind: %s for type: %s", elementKind, typeElement));
result = result.getEnclosingElement();
elementKind = result.getKind();
}
return (PackageElement) result;
}
use of javax.lang.model.element.TypeElement in project tiger by google.
the class TigerDaggerGeneratorProcessor method generateComponentBuilder.
private void generateComponentBuilder(ClassName componentClassName, @Nullable TypeElement dependencyComponent, List<TypeElement> sortedPassedModules, TypeSpec.Builder componentBuilder, ComponentInfo coreInjector) {
TypeSpec.Builder builderBuilder = TypeSpec.classBuilder("Builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
// set parent inject methods.
ClassName dependencyClassName = null;
if (dependencyComponent != null) {
dependencyClassName = (ClassName) ClassName.get(dependencyComponent.asType());
Utils.addSetMethod(componentClassName, builderBuilder, dependencyClassName);
}
/**
* Set module methods.
*/
for (TypeElement m : sortedPassedModules) {
Utils.addSetMethod(componentClassName, builderBuilder, (ClassName) ClassName.get(m.asType()));
}
// build() method.
MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).returns(componentClassName);
StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
if (dependencyClassName != null) {
returnCodeBuilder.append(Utils.getSourceCodeName(dependencyComponent));
if (!sortedPassedModules.isEmpty()) {
returnCodeBuilder.append(", ");
}
}
if (!sortedPassedModules.isEmpty()) {
for (TypeElement module : sortedPassedModules) {
String moduleFiledName = Utils.getSourceCodeName(module);
returnCodeBuilder.append(moduleFiledName).append(", ");
}
returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
}
returnCodeBuilder.append(");");
buildMethodBuilder.addCode(returnCodeBuilder.toString(), componentClassName);
builderBuilder.addMethod(buildMethodBuilder.build());
componentBuilder.addType(builderBuilder.build());
}
use of javax.lang.model.element.TypeElement 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.TypeElement 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