Search in sources :

Example 11 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)

Example 12 with ParameterizedTypeName

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

the class NewInjectorGenerator method generateProvisionMethodForSet.

private void generateProvisionMethodForSet(NewBindingKey key, TypeElement referencingClass, String suffix) {
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
    // messager.printMessage(Kind.NOTE, "generateProvisionMethodForSet: " + 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);
    methodSpecBuilder.addStatement("$T result = new $T<>()", type, HashSet.class);
    methodSpecBuilder.addStatement("$T setVar", type);
    methodSpecBuilder.addStatement("$T elementVar", elementType);
    SetMultimap<PackageElement, NewDependencyInfo> packageToDependencyInfoMap = HashMultimap.create();
    for (NewDependencyInfo dependencyInfo : dependencies.get(key)) {
        packageToDependencyInfoMap.put(Utils.getPackage(dependencyInfo.getSourceClassElement()), dependencyInfo);
    }
    for (PackageElement pkg : packageToDependencyInfoMap.keySet()) {
        // messager.printMessage(Kind.NOTE, String.format("generateProvisionMethodForSet for %s from"
        // +
        // " %s", key, packageToDependencyInfoMap.get(pkg)));
        generateSetTypeProvisionMethodForPackage(key, packageToDependencyInfoMap.get(pkg), suffix);
        NewDependencyInfo dependencyInfo = Iterables.getFirst(packageToDependencyInfoMap.get(pkg), null);
        Preconditions.checkNotNull(dependencyInfo, String.format("no dependencyInfo for set key %s in module %s", key, pkg));
        ClassName packagedInjectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
        methodSpecBuilder.addStatement("setVar = $L.$N().$N()", TOP_LEVEL_INJECTOR_FIELD, Utils.getGetMethodName(packagedInjectorClassName), getProvisionMethodName(key));
        methodSpecBuilder.addStatement("result.addAll($L)", "setVar");
    }
    methodSpecBuilder.addStatement("return result");
    componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) PackageElement(javax.lang.model.element.PackageElement) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 13 with ParameterizedTypeName

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

the class NewInjectorGenerator method getClassFromKey.

// Handles generic case.
private TypeElement getClassFromKey(NewBindingKey key) {
    Preconditions.checkArgument(Iterables.getOnlyElement(Utils.getDependencyInfo(dependencies, key)).getProvisionMethodElement() == null, String.format("Key: %s", key));
    ClassName className;
    TypeName typeName = key.getTypeName();
    if (typeName instanceof ClassName) {
        className = (ClassName) typeName;
    } else {
        Preconditions.checkState(typeName instanceof ParameterizedTypeName, String.format("typeName: %s", typeName));
        className = ((ParameterizedTypeName) typeName).rawType;
    }
    String classNameString = Utils.getClassCanonicalName(className);
    TypeElement result = processingEnv.getElementUtils().getTypeElement(classNameString);
    Preconditions.checkNotNull(result, String.format("Did not find TypeElement for %s", classNameString));
    return result;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 14 with ParameterizedTypeName

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

the class NewInjectorGenerator method generateMapTypeProvisionMethodForPackage.

// If dependencyInfo is not null, then it is a contributor to map biding.
private void generateMapTypeProvisionMethodForPackage(final NewBindingKey key, Set<NewDependencyInfo> dependencyInfos, String suffix) {
    Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
    NewDependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
    TypeName returnType = key.getTypeName();
    ClassName injectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(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);
    NewBindingKey mapValueKey = NewBindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (NewDependencyInfo di : dependencyInfos) {
        AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
            return;
        }
        Object mapKey = Utils.getAnnotationValue(mapKeyMirror, "value").getValue();
        methodSpecBuilder.addStatement("mapKey = ($T) $S", mapKeyType, mapKey);
        if (Utils.isMapWithBuiltinValueType(key)) {
            TypeElement scope = scopeCalculator.calculate(key);
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
        } else {
            addNewStatementToMethodSpec(mapValueKey, di, injectorClassName, methodSpecBuilder, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
    methodSpecBuilder.addStatement("return result");
    componentSpecBuilder.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) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) 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 15 with ParameterizedTypeName

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

the class Utils method getSourceCodeName.

/**
   * Returns "com_Foo" for com.Foo, "com_Foo_com_Bar_com_Baz" for Foo<Bar, Baz>.
   * upper_bounds_UpperBound_Foo for "? extends Foo" and
   * lower_bounds_LowerBound_Foo for "? super Foo". Assuming raw types are not
   * used, there will be not conflicts.
   */
public static String getSourceCodeName(TypeName typeName) {
    Preconditions.checkNotNull(typeName);
    if (typeName.isPrimitive()) {
        return typeName.toString();
    } else if (typeName instanceof ClassName) {
        return getClassCanonicalName((ClassName) typeName).replace(".", "_");
    } else if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName p = (ParameterizedTypeName) typeName;
        StringBuilder builder = new StringBuilder(getSourceCodeName(p.rawType));
        for (TypeName t : p.typeArguments) {
            builder.append("_").append(getSourceCodeName(t));
        }
        return builder.toString();
    } else if (typeName instanceof WildcardTypeName) {
        WildcardTypeName w = (WildcardTypeName) typeName;
        if (w.upperBounds.size() > 0) {
            return "upper_bounds_" + getSourceCodeName(w.upperBounds.get(0));
        } else {
            Preconditions.checkState(w.lowerBounds.size() > 0);
            return "lower_bounds_" + getSourceCodeName(w.lowerBounds.get(0));
        }
    } else if (typeName instanceof ArrayTypeName) {
        ArrayTypeName arrayTypeName = (ArrayTypeName) typeName;
        return "ArrayOf" + getSourceCodeName(arrayTypeName.componentType);
    }
    throw new RuntimeException("Unexpected type: " + typeName);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) 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