Search in sources :

Example 86 with TypeName

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

the class GeneralInjectorGeneratorHubClone method createInjectorBuilder.

protected final Builder createInjectorBuilder() {
    String injectorSimpleName = getInjectorSimpleName();
    logger.n("generated component " + injectorSimpleName);
    Builder result = TypeSpec.classBuilder(injectorSimpleName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build());
    for (TypeName typeName : getSuperInterfaces()) {
        result.addSuperinterface(typeName);
    }
    return result;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Generated(javax.annotation.Generated) Builder(com.squareup.javapoet.TypeSpec.Builder)

Example 87 with TypeName

use of com.squareup.javapoet.TypeName 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 88 with TypeName

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

the class CoreInjectorGenerator method generateSetTypeProvisionMethodForPackage.

// If dependencyInfo is not null, then it is a contributor to set binding.
private void generateSetTypeProvisionMethodForPackage(BindingKey key, Set<DependencyInfo> dependencyInfos, String suffix) {
    messager.printMessage(Kind.NOTE, TAG + ".generateSetTypeProvisionMethodForPackage: key " + key + " dI: " + dependencyInfos);
    Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
    DependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
    TypeName returnType = key.getTypeName();
    ClassName injectorClassName = getPackagedInjectorNameForDependencyInfo(key, dependencyInfo);
    TypeSpec.Builder componentSpecBuilder = 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, HashSet.class);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        methodSpecBuilder.beginControlFlow("");
        Preconditions.checkNotNull(di.getProvisionMethodElement());
        TypeName contributorType = TypeName.get(di.getProvisionMethodElement().getReturnType());
        methodSpecBuilder.addStatement("$T contributor", contributorType);
        addNewStatementToMethodSpec(key, di, injectorClassName, methodSpecBuilder, "contributor");
        if (di.getType().equals(SET)) {
            methodSpecBuilder.addStatement("result.add(contributor)");
        } else {
            Preconditions.checkState(di.getType().equals(SET_VALUES));
            methodSpecBuilder.addStatement("result.addAll(contributor)");
        }
        methodSpecBuilder.endControlFlow();
    }
    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 : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 89 with TypeName

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

the class GeneralInjectorGenerator4 method createInjectorBuilder.

protected final Builder createInjectorBuilder() {
    String injectorSimpleName = getInjectorSimpleName();
    logger.n("generated component " + injectorSimpleName);
    Builder result = TypeSpec.classBuilder(injectorSimpleName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build());
    for (TypeName typeName : getSuperInterfaces()) {
        result.addSuperinterface(typeName);
    }
    return result;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Generated(javax.annotation.Generated) Builder(com.squareup.javapoet.TypeSpec.Builder)

Example 90 with TypeName

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

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