Search in sources :

Example 51 with ParameterizedTypeName

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

the class GeneralInjectorGenerator4 method generateProvisionMethodFromClass.

/**
 * Generic is handled.
 */
protected final void generateProvisionMethodFromClass(BindingKey key, String suffix) {
    // logger.n("key: " + key + " referencingClass: " +
    // referencingClass);
    TypeElement cls = utils.getClassFromKey(key);
    DeclaredType clsType = (DeclaredType) utils.getTypeFromKey(key);
    ExecutableElement ctor = utils.findInjectedCtor(cls);
    Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
    ExecutableType ctorType = (ExecutableType) types.asMemberOf(clsType, ctor);
    List<BindingKey> dependencyKeys = utils.getDependenciesFromMethod(ctorType, ctor);
    // TODO: clean this.
    // if (key.getTypeName() instanceof ParameterizedTypeName) {
    // logger.n("be here :" + key);
    // List<BindingKey> specializedKeys = new ArrayList<>();
    // Map<TypeVariableName, TypeName> map =
    // utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(),
    // (ParameterizedTypeName) TypeName.get(cls.asType()));
    // for (BindingKey k : dependencyKeys) {
    // specializedKeys.add(utils.specializeIfNeeded(k, map));
    // }
    // dependencyKeys = specializedKeys;
    // }
    // logger.n("dependencyKeys: " +
    // dependencyKeys);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
    TypeName returnTypeName = getAccessibleTypeName(key);
    methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(returnTypeName);
    onProvisionMethodStart(methodSpecBuilder, key);
    methodSpecBuilder.addStatement("$T result", returnTypeName);
    addNewStatementToMethodSpec(methodSpecBuilder, Iterables.getOnlyElement(dependencies.get(key)), "result");
    if (shouldInjectAfterCreation() && !utils.isGenericNotSpecialized(cls.asType()) && utils.hasInjectedFieldsOrMethodsRecursively(cls, processingEnv)) {
        // logger.n("hasInjected");
        generateInjectionMethod(utils.getClassFromKey(key), Utils.getInjectionMethodName(key));
        methodSpecBuilder.addStatement("$L(result)", Utils.getInjectionMethodName(key));
    }
    methodSpecBuilder.addStatement("return result");
    onProvisionMethodEnd(methodSpecBuilder, key);
    injectorBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) 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) DeclaredType(javax.lang.model.type.DeclaredType)

Example 52 with ParameterizedTypeName

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

the class PackagedHubInterfaceGenerator method generateAbstractInjectionMethod.

private void generateAbstractInjectionMethod(TypeElement cls) {
    if (!classesWithInjectionMethodGenerated.add(cls)) {
        return;
    }
    // TODO: this can only handle generic where type variables do not involve DI. A more general
    // solution
    // is needed.
    TypeName typeName = TypeName.get(cls.asType());
    if (typeName instanceof ParameterizedTypeName) {
        typeName = ((ParameterizedTypeName) typeName).rawType;
    }
    MethodSpec.Builder builder = MethodSpec.methodBuilder("inject").addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).addParameter(typeName, "v");
    interfaceBuilder.addMethod(builder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 53 with ParameterizedTypeName

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

the class HubInjectorGenerator4 method createStringProvidingKey.

private String createStringProvidingKey(BindingKey key) {
    // logger.n("key: %s", key);
    generateProvisionMethodIfNeeded(key);
    StringBuilder builder = new StringBuilder();
    TypeName typeName = key.getTypeName();
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    DependencyInfo dependencyInfo = dependencyInfos == null ? null : Preconditions.checkNotNull(Iterables.getFirst(dependencyInfos, null), "DI not found for key: " + key);
    if (dependencyInfo != null && dependencyInfo.getDependencySourceType().equals(DependencySourceType.CTOR_INJECTED_CLASS) && typeName instanceof ParameterizedTypeName && !utils.isProviderOrLazy(key) && !utils.isOptional(key)) {
        generateGenericInjectorIfNeeded(dependencyInfo);
        // generic ctor injected class
        builder = new StringBuilder("new ").append(utils.getGenericInjectorName(key)).append("().generate(");
        for (BindingKey k : utils.sortBindingKeys(dependencyInfo.getDependencies())) {
            builder.append(createStringProvidingKey(k)).append(", ");
        }
        utils.trimTrailing(builder, ", ");
        builder.append(")");
    } else {
        // types from hub or this
        builder.append(getProvisionMethodName(key)).append("()");
    }
    // logger.n("result: %s", builder.toString());
    return builder.toString();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 54 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName 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 55 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName 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)

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)135 TypeName (com.squareup.javapoet.TypeName)90 ClassName (com.squareup.javapoet.ClassName)53 MethodSpec (com.squareup.javapoet.MethodSpec)47 TypeSpec (com.squareup.javapoet.TypeSpec)29 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)29 TypeElement (javax.lang.model.element.TypeElement)23 ArrayList (java.util.ArrayList)19 TypeMirror (javax.lang.model.type.TypeMirror)19 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)15 HashSet (java.util.HashSet)14 Map (java.util.Map)13 List (java.util.List)12 Set (java.util.Set)12 FieldSpec (com.squareup.javapoet.FieldSpec)11 ParameterSpec (com.squareup.javapoet.ParameterSpec)11 TypeVariableName (com.squareup.javapoet.TypeVariableName)10 Element (javax.lang.model.element.Element)10 PackageElement (javax.lang.model.element.PackageElement)10 Builder (com.squareup.javapoet.TypeSpec.Builder)9