Search in sources :

Example 71 with ParameterizedTypeName

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

the class Utils method peelMapWithBuiltinValue.

/**
   * If the key comes with value of type that has dagger builtin binding, return one
   * with the type replaced by the element of the original value type, null otherwise.
   * Nested built-in binding like Lazy<Lazy<Foo>>, Provider<Lazy<Foo>>, etc, are not 
   * supported.
   */
@Nullable
public static NewBindingKey peelMapWithBuiltinValue(NewBindingKey key) {
    Preconditions.checkState(isMap(key), String.format("Expect a map but got %s", key));
    ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) key.getTypeName();
    TypeName valueType = parameterizedTypeName.typeArguments.get(1);
    if (hasBuiltinBinding(valueType)) {
        TypeName mapKeyType = parameterizedTypeName.typeArguments.get(0);
        TypeName elementType = Iterables.getOnlyElement(((ParameterizedTypeName) valueType).typeArguments);
        TypeName newType = ParameterizedTypeName.get(ClassName.get(Map.class), mapKeyType, elementType);
        return NewBindingKey.get(newType, key.getQualifier());
    }
    return null;
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) Map(java.util.Map) HashMap(java.util.HashMap) IntoMap(dagger.multibindings.IntoMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Nullable(javax.annotation.Nullable)

Example 72 with ParameterizedTypeName

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

Example 73 with ParameterizedTypeName

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

the class Utils method isMap.

/**
   * Returns if key is a map with type variables.
   */
public static boolean isMap(NewBindingKey key) {
    TypeName typeName = key.getTypeName();
    if (!(typeName instanceof ParameterizedTypeName)) {
        return false;
    }
    ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
    return parameterizedTypeName.rawType.equals(ClassName.get(Map.class));
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) Map(java.util.Map) HashMap(java.util.HashMap) IntoMap(dagger.multibindings.IntoMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 74 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project butterknife by JakeWharton.

the class FieldCollectionViewBinding method render.

CodeBlock render() {
    CodeBlock.Builder builder = CodeBlock.builder().add("target.$L = $T.$L(", name, UTILS, kind.factoryName);
    for (int i = 0; i < ids.size(); i++) {
        if (i > 0) {
            builder.add(", ");
        }
        builder.add("\n");
        boolean requiresCast = requiresCast(type);
        if (!requiresCast && !required) {
            builder.add("source.findViewById($L)", ids.get(i).code);
        } else {
            builder.add("$T.find", UTILS);
            builder.add(required ? "RequiredView" : "OptionalView");
            if (requiresCast) {
                builder.add("AsType");
            }
            builder.add("(source, $L, \"field '$L'\"", ids.get(i).code, name);
            if (requiresCast) {
                TypeName rawType = type;
                if (rawType instanceof ParameterizedTypeName) {
                    rawType = ((ParameterizedTypeName) rawType).rawType;
                }
                builder.add(", $T.class", rawType);
            }
            builder.add(")");
        }
    }
    return builder.add(")").build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 75 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project butterknife by JakeWharton.

the class BindingSet method newBuilder.

static Builder newBuilder(TypeElement enclosingElement) {
    TypeMirror typeMirror = enclosingElement.asType();
    boolean isView = isSubtypeOfType(typeMirror, VIEW_TYPE);
    boolean isActivity = isSubtypeOfType(typeMirror, ACTIVITY_TYPE);
    boolean isDialog = isSubtypeOfType(typeMirror, DIALOG_TYPE);
    TypeName targetType = TypeName.get(typeMirror);
    if (targetType instanceof ParameterizedTypeName) {
        targetType = ((ParameterizedTypeName) targetType).rawType;
    }
    String packageName = getPackage(enclosingElement).getQualifiedName().toString();
    String className = enclosingElement.getQualifiedName().toString().substring(packageName.length() + 1).replace('.', '$');
    ClassName bindingClassName = ClassName.get(packageName, className + "_ViewBinding");
    boolean isFinal = enclosingElement.getModifiers().contains(Modifier.FINAL);
    return new Builder(targetType, bindingClassName, isFinal, isView, isActivity, isDialog);
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) 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