Search in sources :

Example 81 with TypeName

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

the class DependencyCollector method completeDependenciesAndRequiredKeys.

private void completeDependenciesAndRequiredKeys(SetMultimap<BindingKey, DependencyInfo> result, Set<BindingKey> requiredKeys, Set<BindingKey> unresolved) {
    Collection<DependencyInfo> dependencies;
    DependencyInfo dependency;
    // Added all the required dependencies from ctor injected classes.
    while (!requiredKeys.isEmpty()) {
        BindingKey key = Iterables.getFirst(requiredKeys, null);
        Preconditions.checkNotNull(key);
        requiredKeys.remove(key);
        TypeName typeName = key.getTypeName();
        DeclaredType type = (DeclaredType) utils.getTypeFromTypeName(typeName);
        TypeElement classTypeElement = (TypeElement) type.asElement();
        Preconditions.checkNotNull(classTypeElement, String.format("Class %s not found.", type));
        // logger.n(TAG + ".addDependenciesForRequiredKeys: typeName " + typeName);
        if (result.containsKey(key)) {
            continue;
        } else if (key.getTypeName().isPrimitive()) {
            BindingKey boxed = BindingKey.get(key.getTypeName().box(), key.getQualifier());
            if (!result.containsKey(boxed)) {
                logger.n(TAG + ".addDependenciesForRequiredKeys: binding not found for key " + key);
            }
            continue;
        } else if (key.getTypeName().isBoxedPrimitive()) {
            BindingKey unboxed = BindingKey.get(key.getTypeName().unbox(), key.getQualifier());
            if (!result.containsKey(unboxed)) {
                logger.n(TAG + ".addDependenciesForRequiredKeys: binding not found for key " + key);
            }
            continue;
        } else if (utils.isOptional(key)) {
            if (utils.getDependencyInfo(result, key) == null) {
                logger.n(TAG + " key " + key + "'s dependencies not found");
            }
            continue;
        } else if (utils.isProviderOrLazy(typeName)) {
            key = utils.getElementKeyForParameterizedBinding(key);
            requiredKeys.add(key);
            continue;
        } else if (utils.isMapWithBuiltinValueType(key)) {
            BindingKey peeledMapKey = Preconditions.checkNotNull(utils.peelMapWithBuiltinValue(key));
            requiredKeys.add(peeledMapKey);
            continue;
        } else if (utils.isMap(key)) {
            if (getMapContributorKeys(result.keySet(), key).isEmpty()) {
                // Not found, must be from parent.
                unresolved.add(key);
                logger.n("Binding not found for : " + key);
            }
            continue;
        } else if (utils.isDaggerMembersInjector(typeName)) {
            BindingKey childKey = utils.getElementKeyForParameterizedBinding(key);
            Set<BindingKey> keys = collectRequiredKeysFromInjectedClass(utils.getTypeElement(childKey));
            DeclaredType childType = (DeclaredType) utils.getTypeFromTypeName(childKey.getTypeName());
            TypeElement childClassTypeElement = (TypeElement) childType.asElement();
            // child, to make generation code happy.
            // dependency =
            // new DependencyInfo(
            // DependencySourceType.DAGGER_MEMBERS_INJECTOR,
            // childKey,
            // Sets.newHashSet(keys),
            // childClassTypeElement,
            // ProvisionType.UNIQUE);
            // addDependencyInfo(result, dependency);
            // itself
            dependency = new DependencyInfo(DependencySourceType.DAGGER_MEMBERS_INJECTOR, key, Sets.newHashSet(keys), childClassTypeElement, ProvisionType.UNIQUE);
        } else // }
        if (utils.isEitherComponentBuilder(classTypeElement)) {
            dependency = new DependencyInfo(DependencySourceType.EITHER_COMPONENT_BUILDER, key, new HashSet<>(), classTypeElement, ProvisionType.UNIQUE);
        } else if (utils.isEitherComponent(classTypeElement)) {
            dependency = new DependencyInfo(DependencySourceType.EITHER_COMPONENT, key, new HashSet<>(), classTypeElement, ProvisionType.UNIQUE);
        } else {
            dependencies = collectFromCtorInjectedClass(classTypeElement, type);
            if (dependencies == null) {
                unresolved.add(key);
                continue;
            }
            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 {
        // logger.n(
        // TAG + ".addDependenciesForRequiredKeys new keys: " + dependency.getDependencies());
        requiredKeys.addAll(dependency.getDependencies());
        // }
        checkOneDependency(result, dependency);
        addDependencyInfo(result, dependency);
    }
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) HashSet(java.util.HashSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) HashSet(java.util.HashSet)

Example 82 with TypeName

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

the class GeneralInjectorGenerator method generateProvisionMethodForDaggerMembersInjector.

protected final void generateProvisionMethodForDaggerMembersInjector(BindingKey key, String suffix) {
    // logger.n("key: " + key + " referencingClass: " +
    // referencingClass);
    BindingKey childKey = utils.getElementKeyForParameterizedBinding(key);
    TypeName childTypeName = childKey.getTypeName();
    String parameterSourceCodeName = Utils.getSourceCodeName(childTypeName);
    MethodSpec.Builder injectMethodSpecBuilder = MethodSpec.methodBuilder("injectMembers").addModifiers(Modifier.PUBLIC).addParameter(childTypeName, parameterSourceCodeName).addAnnotation(Override.class);
    generateInjectionMethod(childKey);
    injectMethodSpecBuilder.addStatement("$L($L)", "inject", parameterSourceCodeName);
    TypeSpec injectorType = TypeSpec.anonymousClassBuilder("").addSuperinterface(key.getTypeName()).addMethod(injectMethodSpecBuilder.build()).build();
    MethodSpec.Builder provisionMethodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName());
    onProvisionMethodStart(provisionMethodSpecBuilder, key);
    provisionMethodSpecBuilder.addStatement("$T result = $L", key.getTypeName(), injectorType);
    provisionMethodSpecBuilder.addStatement("return result");
    onProvisionMethodEnd(provisionMethodSpecBuilder, key);
    // logger.n("" + provisionMethodSpecBuilder.build());
    injectorBuilder.addMethod(provisionMethodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 83 with TypeName

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

the class GeneralInjectorGenerator method generateProvisionMethodForSet.

/**
 * TODO: support set of builtin types.
 */
protected final void generateProvisionMethodForSet(BindingKey key, String suffix) {
    // logger.n("" + key +
    // " PackagedInjector: "
    // + getInjectorFor(key, referencingClass) + " SpecBuilder: " + componentSpecBuilder);
    ParameterizedTypeName type = (ParameterizedTypeName) key.getTypeName();
    Preconditions.checkArgument(type.rawType.equals(ClassName.get(Set.class)));
    TypeName elementType = Iterables.getOnlyElement(type.typeArguments);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(type);
    onProvisionMethodStart(methodSpecBuilder, key);
    methodSpecBuilder.addStatement("$T result = new $T<>()", type, HashSet.class);
    generateSetContributors(key, methodSpecBuilder);
    methodSpecBuilder.addStatement("return result");
    onProvisionMethodEnd(methodSpecBuilder, key);
    injectorBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 84 with TypeName

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

the class GeneralInjectorGenerator method createAnonymousBuiltinType.

/**
 * Generate for either unique a binding or a contributor to a multi-binding. DependencyInfo is
 * null for unique one and non-null or multi-binding one. ReferencingClass is the opposite. For
 * multi-binding, referencing class is the module in dependency. Scope is null for unique binding.
 */
protected final TypeSpec createAnonymousBuiltinType(BindingKey key, @Nullable DependencyInfo dependencyInfo) {
    Preconditions.checkArgument(key.getTypeName() instanceof ParameterizedTypeName);
    boolean isMultiBinding = dependencyInfo != null;
    TypeName rawTypeName = ((ParameterizedTypeName) key.getTypeName()).rawType;
    Preconditions.checkArgument(utils.isProviderOrLazy(key), String.format("Built-in binding expected(Provider or Lazy), but get %s", key));
    boolean isLazy = rawTypeName.equals(ClassName.get(Lazy.class));
    BindingKey elementKey = utils.getElementKeyForParameterizedBinding(key);
    Preconditions.checkNotNull(elementKey);
    if (!isMultiBinding) {
        String provisionMethodName = getProvisionMethodName(elementKey);
        // utils.generateDebugInfoMethod(injectorBuilder,
        // "before" + provisionMethodName + generatedBindings.contains(provisionMethodName));
        generateProvisionMethodIfNeeded(elementKey);
    }
    // TODO: multi-threading.
    MethodSpec.Builder builderForGet = MethodSpec.methodBuilder("get").returns(elementKey.getTypeName()).addAnnotation(Override.class).addModifiers(Modifier.SYNCHRONIZED).addModifiers(Modifier.PUBLIC);
    if (isLazy) {
        builderForGet.beginControlFlow("if (var == null)");
    }
    if (!isMultiBinding) {
        Set<DependencyInfo> dIs = Utils.getDependencyInfosHandlingBox(dependencies, elementKey);
        if (dIs != null) {
            elementKey = Preconditions.checkNotNull(Iterables.getFirst(dIs, null), "key: " + elementKey + " dI: " + dIs).getDependant();
        }
        builderForGet.addStatement("var = $N()", getProvisionMethodName(elementKey));
    } else {
        /**
         * TODO: revisit the logic here, current, for Provide, Lazy and Optional, the key != {@link
         * DependencyInfo#getDependant()}.
         */
        addNewStatementToMethodSpec(builderForGet, dependencyInfo, "var");
    }
    if (isLazy) {
        builderForGet.endControlFlow();
    }
    builderForGet.addStatement("return var");
    return TypeSpec.anonymousClassBuilder("").addSuperinterface(key.getTypeName()).addField(elementKey.getTypeName(), "var", Modifier.PRIVATE).addMethod(builderForGet.build()).build();
}
Also used : Lazy(dagger.Lazy) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 85 with TypeName

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

the class GeneralInjectorGeneratorHubClone method generateSetContributors.

protected void generateSetContributors(BindingKey key, MethodSpec.Builder methodSpecBuilder) {
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    for (DependencyInfo dependencyInfo : dependencyInfos) {
        // logger.n("for %s from"
        // +
        // " %s", key, packageToDependencyInfoMap.get(pkg)));
        ExecutableElement provisionMethodElement = dependencyInfo.getProvisionMethodElement();
        boolean isSetValues = dependencyInfo.getType().equals(SET_VALUES);
        if (utils.isMultibindsMethod(provisionMethodElement)) {
            continue;
        }
        methodSpecBuilder.beginControlFlow("");
        TypeName contributorType = TypeName.get(dependencyInfo.getProvisionMethodElement().getReturnType());
        methodSpecBuilder.addStatement("$T contributor", contributorType);
        addNewStatementToMethodSpec(methodSpecBuilder, dependencyInfo, "contributor");
        if (dependencyInfo.getType().equals(SET)) {
            methodSpecBuilder.addStatement("result.add(contributor)");
        } else {
            Preconditions.checkState(dependencyInfo.getType().equals(SET_VALUES));
            methodSpecBuilder.addStatement("result.addAll(contributor)");
        }
        methodSpecBuilder.endControlFlow();
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

TypeName (com.squareup.javapoet.TypeName)296 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)232 MethodSpec (com.squareup.javapoet.MethodSpec)109 ClassName (com.squareup.javapoet.ClassName)82 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)78 TypeSpec (com.squareup.javapoet.TypeSpec)55 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)50 TypeElement (javax.lang.model.element.TypeElement)46 ArrayList (java.util.ArrayList)45 TypeMirror (javax.lang.model.type.TypeMirror)41 ExecutableElement (javax.lang.model.element.ExecutableElement)36 ParameterSpec (com.squareup.javapoet.ParameterSpec)26 Builder (com.squareup.javapoet.TypeSpec.Builder)22 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)21 CodeBlock (com.squareup.javapoet.CodeBlock)20 VariableElement (javax.lang.model.element.VariableElement)20 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)19 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)18 List (java.util.List)18 Map (java.util.Map)17