Search in sources :

Example 6 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project glide by bumptech.

the class RootModuleGenerator method generateGetExcludedModuleClasses.

// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private static MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
    TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
    ParameterizedTypeName classOfWildcardOfObjet = ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
    ParameterizedTypeName setOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(Set.class), classOfWildcardOfObjet);
    ParameterizedTypeName hashSetOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(HashSet.class), classOfWildcardOfObjet);
    MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(setOfClassOfWildcardOfObject);
    if (excludedClassNames.isEmpty()) {
        builder.addStatement("return $T.emptySet()", Collections.class);
    } else {
        builder.addStatement("$T excludedClasses = new $T()", setOfClassOfWildcardOfObject, hashSetOfClassOfWildcardOfObject);
        for (String excludedClassName : excludedClassNames) {
            // TODO: Remove this when we no longer support manifest parsing.
            // Using a Literal ($L) instead of a type ($T) to get a fully qualified import that allows
            // us to suppress deprecation warnings. Aimed at deprecated GlideModules.
            builder.addStatement("excludedClasses.add($L.class)", excludedClassName);
        }
        builder.addStatement("return excludedClasses");
    }
    return builder.build();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Set(java.util.Set) HashSet(java.util.HashSet) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HashSet(java.util.HashSet)

Example 7 with ParameterizedTypeName

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

the class Utils method hasBuiltinBinding.

public static boolean hasBuiltinBinding(TypeName type) {
    if (!(type instanceof ParameterizedTypeName)) {
        return false;
    }
    ParameterizedTypeName typeName = (ParameterizedTypeName) type;
    ClassName rawType = typeName.rawType;
    return rawType.equals(ClassName.get(Provider.class)) || rawType.equals(ClassName.get(Lazy.class));
}
Also used : ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 8 with ParameterizedTypeName

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

the class Utils method getElementKeyForBuiltinBinding.

/**
   * Return {@link NewBindingKey} for element of the give {@link NewBindingKey} that
   * has built-in binding, null if not built-in building.
   */
@Nullable
public static NewBindingKey getElementKeyForBuiltinBinding(NewBindingKey key) {
    if (!hasBuiltinBinding(key)) {
        return null;
    }
    ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) key.getTypeName();
    TypeName typeName = Iterables.getOnlyElement(parameterizedTypeName.typeArguments);
    AnnotationSpec qualifier = key.getQualifier();
    return NewBindingKey.get(typeName, qualifier);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Nullable(javax.annotation.Nullable)

Example 9 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 10 with ParameterizedTypeName

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

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)31 TypeName (com.squareup.javapoet.TypeName)23 ClassName (com.squareup.javapoet.ClassName)21 TypeSpec (com.squareup.javapoet.TypeSpec)14 MethodSpec (com.squareup.javapoet.MethodSpec)13 TypeElement (javax.lang.model.element.TypeElement)10 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)9 TypeMirror (javax.lang.model.type.TypeMirror)8 Map (java.util.Map)6 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)5 FieldSpec (com.squareup.javapoet.FieldSpec)5 ParameterSpec (com.squareup.javapoet.ParameterSpec)5 TypeVariableName (com.squareup.javapoet.TypeVariableName)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Builder (com.squareup.javapoet.TypeSpec.Builder)4 Attribute (io.requery.meta.Attribute)4 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)3 CodeBlock (com.squareup.javapoet.CodeBlock)3