Search in sources :

Example 46 with ParameterizedTypeName

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

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

the class CoreInjectorGenerator method generateMapTypeProvisionMethodForPackage.

// it is a contributor to map binding.
/**
 * TODO: revisit the logic to handle scoped multi bindings.
 */
private void generateMapTypeProvisionMethodForPackage(final BindingKey key, Set<DependencyInfo> dependencyInfos, String suffix) {
    messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: key " + key + " di " + dependencyInfos);
    Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
    TypeElement scope = scopeCalculator.calculate(key);
    DependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
    TypeName returnType = key.getTypeName();
    ClassName injectorClassName = getPackagedInjectorNameForDependencyInfo(key, dependencyInfo);
    TypeSpec.Builder packagedInjectorSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(utils.getProvisionMethodName(dependencies, 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);
    BindingKey mapValueKey = BindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        AnnotationMirror mapKeyMirror = utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
            return;
        }
        AnnotationValue mapKey = utils.getAnnotationValue(elements, mapKeyMirror, "value");
        messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: mapKey  " + mapKey);
        methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
        if (utils.isMapWithBuiltinValueType(key)) {
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
        } else {
            addNewStatementToMethodSpec(scope, di, injectorClassName, methodSpecBuilder, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
    methodSpecBuilder.addStatement("return result");
    packagedInjectorSpecBuilder.addMethod(methodSpecBuilder.build());
// messager.printMessage(Kind.NOTE, String.format(
// "generateSetTypeProvisionMethodFromModule: \n key: %s, \n injector: %s, \n method: %s.",
// key, injectorClassName, methodSpecBuilder.build()));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) AnnotationValue(javax.lang.model.element.AnnotationValue) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 48 with ParameterizedTypeName

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

the class GeneralInjectorGenerator4 method generateProvisionMethodForMap.

protected final void generateProvisionMethodForMap(final BindingKey key, String suffix) {
    // logger.n("" + key +
    // " PackagedInjector: "
    // + getInjectorFor(key, referencingClass) + " SpecBuilder: " + componentSpecBuilder);
    ParameterizedTypeName returnType = (ParameterizedTypeName) key.getTypeName();
    Preconditions.checkArgument(returnType.rawType.equals(ClassName.get(Map.class)));
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(returnType);
    onProvisionMethodStart(methodSpecBuilder, key);
    methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
    generateMapContributors(key, returnType, methodSpecBuilder);
    methodSpecBuilder.addStatement("return result");
    onProvisionMethodEnd(methodSpecBuilder, key);
    injectorBuilder.addMethod(methodSpecBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 49 with ParameterizedTypeName

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

the class GeneralInjectorGenerator4 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 50 with ParameterizedTypeName

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

the class GeneralInjectorGenerator4 method generateMapContributors.

protected final void generateMapContributors(BindingKey key, ParameterizedTypeName returnType, MethodSpec.Builder methodSpecBuilder) {
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    // TODO: remove this hack
    if (dependencyInfos == null) {
        dependencyInfos = new HashSet<>();
        logger.w("no dI for key: " + key);
    }
    Preconditions.checkNotNull(dependencyInfos, String.format("dependencyInfo not found for key: %s", key));
    TypeName mapKeyType = returnType.typeArguments.get(0);
    TypeName mapValueType = returnType.typeArguments.get(1);
    BindingKey mapValueKey = BindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            logger.e("unwrapValue = false not supported yet. Consider using set binding.");
            return;
        }
        AnnotationValue mapKey = Utils.getAnnotationValue(elements, mapKeyMirror, "value");
        logger.l(Kind.NOTE, "mapKey: %s", mapKey.toString());
        methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
        if (utils.isMapWithBuiltinValueType(key)) {
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(mapValueKey, di));
        } else {
            addNewStatementToMethodSpec(methodSpecBuilder, di, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) AnnotationValue(javax.lang.model.element.AnnotationValue)

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