use of javax.lang.model.element.AnnotationMirror in project tiger by google.
the class NewDependencyCollector method collectFromModule.
/**
* Collects dependencies from a given {@link dagger.Module}. Type.SET and
* Type.SET_VALUES are put together with Key.get(Set<elementType>, annotation)
* for easier later processing.
*/
private Collection<NewDependencyInfo> collectFromModule(TypeElement module) {
Collection<NewDependencyInfo> result = new HashSet<>();
for (Element e : module.getEnclosedElements()) {
if (!Utils.isProvidesMethod(e, env)) {
continue;
}
ExecutableElement executableElement = (ExecutableElement) e;
TypeMirror returnType = executableElement.getReturnType();
TypeKind returnTypeKind = returnType.getKind();
Preconditions.checkState(returnTypeKind.isPrimitive() || returnTypeKind.equals(TypeKind.DECLARED) || returnTypeKind.equals(TypeKind.ARRAY), String.format("Unexpected type %s from method %s in module %s.", returnTypeKind, executableElement, module));
if (!Utils.isBindableType(returnType)) {
errors.add(String.format("Unbindable type found: %s from module %s by method %s", returnType, module, executableElement));
}
AnnotationMirror annotation = Utils.getQualifier(executableElement);
NewBindingKey key = NewBindingKey.get(returnType, annotation);
List<NewBindingKey> keys = Utils.getDependenciesFromExecutableElement(executableElement);
Provides.Type provideType = Utils.getProvidesType(executableElement);
if (Provides.Type.SET.equals(provideType)) {
key = NewBindingKey.get(ParameterizedTypeName.get(ClassName.get(Set.class), key.getTypeName()), annotation);
} else if (Provides.Type.MAP.equals(provideType)) {
AnnotationMirror mapKeyedMirror = Preconditions.checkNotNull(Utils.getMapKey(executableElement), String.format("Map binding %s missed MapKey.", executableElement));
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirror(mapKeyedMirror.getAnnotationType().asElement(), MapKey.class);
AnnotationValue unwrapValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
if (unwrapValue != null && !((Boolean) unwrapValue.getValue())) {
messager.printMessage(Kind.ERROR, String.format("MapKey with unwrapValue false is not supported, yet. Biding: %s", executableElement));
}
TypeMirror keyTypeMirror = Preconditions.checkNotNull(Utils.getElementTypeMirror(mapKeyedMirror, "value"), String.format("Get key type failed for binding %s", executableElement));
TypeMirror valueTypeMirror = executableElement.getReturnType();
AnnotationMirror qualifier = Utils.getQualifier(executableElement);
key = NewBindingKey.get(ParameterizedTypeName.get(ClassName.get(Map.class), TypeName.get(keyTypeMirror), TypeName.get(valueTypeMirror)), qualifier);
}
NewDependencyInfo newDependencyInfo = new NewDependencyInfo(key, Sets.newHashSet(keys), module, executableElement, provideType);
result.add(newDependencyInfo);
}
// messager.printMessage(Kind.NOTE, String.format("collectFromModule: result: %s", result));
return result;
}
use of javax.lang.model.element.AnnotationMirror in project tiger by google.
the class NewInjectorGenerator method generateMapTypeProvisionMethodForPackage.
// If dependencyInfo is not null, then it is a contributor to map biding.
private void generateMapTypeProvisionMethodForPackage(final NewBindingKey key, Set<NewDependencyInfo> dependencyInfos, String suffix) {
Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
NewDependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
TypeName returnType = key.getTypeName();
ClassName injectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
methodSpecBuilder.addModifiers(Modifier.PUBLIC).returns(returnType);
methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
TypeName mapKeyType = ((ParameterizedTypeName) returnType).typeArguments.get(0);
TypeName mapValueType = ((ParameterizedTypeName) returnType).typeArguments.get(1);
NewBindingKey mapValueKey = NewBindingKey.get(mapValueType);
methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
methodSpecBuilder.addStatement("$T mapValue", mapValueType);
for (NewDependencyInfo di : dependencyInfos) {
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
return;
}
Object mapKey = Utils.getAnnotationValue(mapKeyMirror, "value").getValue();
methodSpecBuilder.addStatement("mapKey = ($T) $S", mapKeyType, mapKey);
if (Utils.isMapWithBuiltinValueType(key)) {
TypeElement scope = scopeCalculator.calculate(key);
methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
} else {
addNewStatementToMethodSpec(mapValueKey, di, injectorClassName, methodSpecBuilder, "mapValue");
}
methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
}
methodSpecBuilder.addStatement("return result");
componentSpecBuilder.addMethod(methodSpecBuilder.build());
// messager.printMessage(Kind.NOTE, String.format(
// "generateSetTypeProvisionMethodFromModule: \n key: %s, \n injector: %s, \n method: %s.",
// key, injectorClassName, methodSpecBuilder.build()));
}
use of javax.lang.model.element.AnnotationMirror 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.AnnotationMirror in project tiger by google.
the class ComponentGeneratorProcessor method getScopeTree.
/**
* Creates a map from child to parent from the give {@link TypeElement}s, which are annotated with
* {@link ScopeDependency}.
*/
private Map<TypeElement, TypeElement> getScopeTree(Set<TypeElement> scopeDependencies) {
Map<TypeElement, TypeElement> result = new HashMap<>();
for (TypeElement element : scopeDependencies) {
AnnotationMirror annotationMirror = Preconditions.checkNotNull(Utils.getAnnotationMirror(element, ScopeDependency.class), String.format("Did not find @ScopeDependency on %s", element));
TypeElement scope = (TypeElement) ((DeclaredType) Utils.getAnnotationValue(annotationMirror, "scope").getValue()).asElement();
TypeElement parent = (TypeElement) ((DeclaredType) Utils.getAnnotationValue(annotationMirror, "parent").getValue()).asElement();
Preconditions.checkState(result.put(scope, parent) == null, String.format("Duplicate ScopeDependencies found for %s. All dependencies are: %s", scope, scopeDependencies));
}
verifyScopeTree(result);
return result;
}
use of javax.lang.model.element.AnnotationMirror in project squidb by yahoo.
the class ImplementsPlugin method parseInterfaces.
private void parseInterfaces() {
TypeElement modelSpecElement = modelSpec.getModelSpecElement();
if (modelSpecElement.getAnnotation(Implements.class) != null) {
List<DeclaredTypeName> typeNames = utils.getTypeNamesFromAnnotationValue(utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceClasses"));
if (!AptUtils.isEmpty(typeNames)) {
interfaces.addAll(typeNames);
}
AnnotationValue value = utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceDefinitions");
List<AnnotationMirror> interfaceSpecs = utils.getValuesFromAnnotationValue(value, AnnotationMirror.class);
for (AnnotationMirror spec : interfaceSpecs) {
AnnotationValue interfaceClassValue = utils.getAnnotationValueFromMirror(spec, "interfaceClass");
List<DeclaredTypeName> interfaceClassList = utils.getTypeNamesFromAnnotationValue(interfaceClassValue);
if (!AptUtils.isEmpty(interfaceClassList)) {
DeclaredTypeName interfaceClass = interfaceClassList.get(0);
AnnotationValue interfaceTypeArgsValue = utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgs");
List<DeclaredTypeName> typeArgs = utils.getTypeNamesFromAnnotationValue(interfaceTypeArgsValue);
if (AptUtils.isEmpty(typeArgs)) {
List<String> typeArgNames = utils.getValuesFromAnnotationValue(utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgNames"), String.class);
for (String typeArgName : typeArgNames) {
typeArgs.add(new DeclaredTypeName(typeArgName));
}
}
interfaceClass.setTypeArgs(typeArgs);
interfaces.add(interfaceClass);
}
}
}
}
Aggregations