Search in sources :

Example 1 with TypeVariableName

use of com.squareup.javapoet.TypeVariableName in project tiger by google.

the class NewDependencyCollector method addDependenciesForRequiredKeys.

/**
   * Add dependencies from ctor injected classes needed by requiredKeys recursively to result.
   */
private void addDependenciesForRequiredKeys(SetMultimap<NewBindingKey, NewDependencyInfo> result, Set<NewBindingKey> requiredKeys) {
    // Added all the required dependencies from ctor injected classes.
    while (!requiredKeys.isEmpty()) {
        NewBindingKey key = Iterables.getFirst(requiredKeys, null);
        Preconditions.checkNotNull(key);
        requiredKeys.remove(key);
        if (result.containsKey(key)) {
            continue;
        }
        TypeName typeName = key.getTypeName();
        if (Utils.hasBuiltinBinding(typeName)) {
            key = Utils.getElementKeyForBuiltinBinding(key);
            requiredKeys.add(key);
            continue;
        }
        if (Utils.isMapWithBuiltinValueType(key)) {
            NewBindingKey peeledMapKey = Preconditions.checkNotNull(Utils.peelMapWithBuiltinValue(key));
            requiredKeys.add(peeledMapKey);
            continue;
        }
        ClassName className;
        if (typeName instanceof ClassName) {
            className = (ClassName) typeName;
        } else {
            Preconditions.checkState(typeName instanceof ParameterizedTypeName, "Expect a %s but get %s", ParameterizedTypeName.class, typeName);
            ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
            for (TypeName parameter : parameterizedTypeName.typeArguments) {
                Preconditions.checkState(parameter instanceof ClassName || parameter instanceof ParameterizedTypeName, String.format("Unexpected parameter type %s for type %s.", parameter, typeName));
            }
            NewDependencyInfo dependencyInfo = Utils.getDependencyInfoByGeneric(result, key);
            if (dependencyInfo != null) {
                requiredKeys.addAll(dependencyInfo.getDependencies());
                continue;
            } else {
                className = ((ParameterizedTypeName) typeName).rawType;
            }
        }
        TypeElement classTypeElement = env.getElementUtils().getTypeElement(Utils.getClassCanonicalName(className));
        Preconditions.checkNotNull(classTypeElement, String.format("Class %s not found.", className));
        Collection<NewDependencyInfo> dependencies = collectFromCtorInjectedClass(classTypeElement);
        if (dependencies == null) {
            messager.printMessage(Kind.ERROR, String.format("Binding not found for %s", key));
            continue;
        }
        NewDependencyInfo dependency = Iterables.getOnlyElement(dependencies);
        if (typeName instanceof ParameterizedTypeName) {
            Map<TypeVariableName, TypeName> parameterMap = Utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) typeName, (ParameterizedTypeName) dependency.getDependant().getTypeName());
            requiredKeys.addAll(Utils.specializeIfNeeded(dependency.getDependencies(), parameterMap));
        } else {
            requiredKeys.addAll(dependency.getDependencies());
        }
        checkOneDependency(result, dependency);
        addDependencyInfo(result, dependency);
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 2 with TypeVariableName

use of com.squareup.javapoet.TypeVariableName in project tiger by google.

the class NewDependencyCollector method collectionToMultimap.

/**
   * NOTE: the key of the returned map is of the raw type if the related
   * {@link NewDependencyInfo#getDependant()} is for a generic class.
   */
public static SetMultimap<NewBindingKey, NewDependencyInfo> collectionToMultimap(Collection<NewDependencyInfo> dependencies) {
    SetMultimap<NewBindingKey, NewDependencyInfo> result = HashMultimap.create();
    for (NewDependencyInfo info : dependencies) {
        NewBindingKey key = info.getDependant();
        TypeName typeName = key.getTypeName();
        // For generic type with type variable, only keep raw type.
        if (typeName instanceof ParameterizedTypeName) {
            ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
            TypeName anyParameter = Preconditions.checkNotNull(Iterables.getFirst(parameterizedTypeName.typeArguments, null), String.format("ParameterizedTypeName of %s has no parameter.", key));
            if (anyParameter instanceof TypeVariableName) {
                typeName = parameterizedTypeName.rawType;
                key = NewBindingKey.get(typeName, key.getQualifier());
            }
        }
        result.put(key, info);
    }
    return result;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 3 with TypeVariableName

use of com.squareup.javapoet.TypeVariableName in project tiger by google.

the class NewDependencyCollector method addDependencyInfo.

/**
   * Adds the give {@link NewDependencyInfo} to the map, handling generic type with formal
   * parameters. Returns if it changed the given map.
   */
private boolean addDependencyInfo(SetMultimap<NewBindingKey, NewDependencyInfo> existingDependencies, NewDependencyInfo info) {
    NewBindingKey key = info.getDependant();
    TypeName typeName = key.getTypeName();
    // For generic type with type variable, only keep raw type.
    if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
        TypeName anyParameter = Preconditions.checkNotNull(Iterables.getFirst(parameterizedTypeName.typeArguments, null), String.format("ParameterizedTypeName of %s has no parameter.", key));
        if (anyParameter instanceof TypeVariableName) {
            typeName = parameterizedTypeName.rawType;
            key = NewBindingKey.get(typeName, key.getQualifier());
        }
    }
    return existingDependencies.put(key, info);
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 4 with TypeVariableName

use of com.squareup.javapoet.TypeVariableName in project tiger by google.

the class NewInjectorGenerator method generateProvisionMethodFromClass.

/**
   * Handles generic.
   */
private void generateProvisionMethodFromClass(NewBindingKey key, TypeElement referencingClass, String suffix) {
    // System.out.println(
    // "generateProvisionMethodFromClass. key: " + key + " referencingClass: " +
    // referencingClass);
    ClassName packagedInjectorClassName = getInjectorFor(key, referencingClass);
    TypeSpec.Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
    TypeElement cls = getClassFromKey(key);
    ExecutableElement ctor = Utils.findInjectedCtor(cls, processingEnv);
    Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
    List<NewBindingKey> dependencyKeys = Utils.getDependenciesFromExecutableElement(ctor);
    if (key.getTypeName() instanceof ParameterizedTypeName) {
        List<NewBindingKey> specializedKeys = new ArrayList<>();
        Map<TypeVariableName, TypeName> map = Utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(), (ParameterizedTypeName) TypeName.get(cls.asType()));
        for (NewBindingKey k : dependencyKeys) {
            specializedKeys.add(Utils.specializeIfNeeded(k, map));
        }
        dependencyKeys = specializedKeys;
    }
    // System.out.println("generateProvisionMethodFromClass. dependencyKeys: " +
    // dependencyKeys);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
    methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName());
    StringBuilder builder = new StringBuilder("$T result = new $T(");
    if (dependencyKeys.size() > 0) {
        for (NewBindingKey dependencyKey : dependencyKeys) {
            // System.out.println("generateProvisionMethodFromClass. dependencyKey: "
            // + dependencyKey);
            generateProvisionMethodAndAppendAsParameter(dependencyKey, cls, packagedInjectorClassName, builder);
        }
        builder.delete(builder.length() - 2, builder.length());
    }
    builder.append(")");
    methodSpecBuilder.addStatement(builder.toString(), key.getTypeName(), key.getTypeName());
    if (Utils.hasInjectedFieldsOrMethods(cls, processingEnv)) {
        // System.out.println("generateProvisionMethodFromClass. hasInjected");
        generateInjectionMethod(key, referencingClass);
        methodSpecBuilder.addStatement("inject(result)");
    }
    methodSpecBuilder.addStatement("return result");
    injectorSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 5 with TypeVariableName

use of com.squareup.javapoet.TypeVariableName in project tiger by google.

the class Utils method getDependencyInfoByGeneric.

/**
   * Return {@link NewDependencyInfo} for the generalized {@link NewBindingKey} for
   * the give key. Null if not applicable or not exist.
   */
public static NewDependencyInfo getDependencyInfoByGeneric(SetMultimap<NewBindingKey, NewDependencyInfo> dependencies, NewBindingKey key) {
    TypeName typeName = key.getTypeName();
    Preconditions.checkArgument(key.getQualifier() == null, String.format("Binding to %s is supposed to be resolved through generic type of %s" + "but has non-null qualifier.", key, typeName));
    if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
        ClassName rawTypeName = parameterizedTypeName.rawType;
        NewBindingKey rawKey = NewBindingKey.get(rawTypeName);
        if (dependencies.containsKey(rawKey)) {
            NewDependencyInfo dependencyInfo = Iterables.getOnlyElement(dependencies.get(rawKey));
            TypeName formalTypeName = dependencyInfo.getDependant().getTypeName();
            Preconditions.checkState(formalTypeName instanceof ParameterizedTypeName, String.format("Formal type %s is not of type ParameterizedTypeName. Related actual type is %s", formalTypeName, parameterizedTypeName));
            Map<TypeVariableName, TypeName> mapTypeVariableToSpecialized = getMapFromTypeVariableToSpecialized(parameterizedTypeName, (ParameterizedTypeName) formalTypeName);
            Set<NewBindingKey> specializedDependencies = specializeIfNeeded(dependencyInfo.getDependencies(), mapTypeVariableToSpecialized);
            return new NewDependencyInfo(key, specializedDependencies, dependencyInfo.getSourceClassElement(), dependencyInfo.getProvisionMethodElement(), dependencyInfo.getType());
        }
    }
    return null;
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)5 TypeName (com.squareup.javapoet.TypeName)5 TypeVariableName (com.squareup.javapoet.TypeVariableName)5 ClassName (com.squareup.javapoet.ClassName)3 TypeElement (javax.lang.model.element.TypeElement)2 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Builder (com.squareup.javapoet.TypeSpec.Builder)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 ArrayList (java.util.ArrayList)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1