Search in sources :

Example 96 with TypeName

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

the class GeneralInjectorGenerator4 method generateScopedProvisionMethod.

protected final void generateScopedProvisionMethod(Builder componentSpecBuilder, BindingKey key) {
    TypeName returnTypeName = getAccessibleTypeName(key);
    MethodSpec.Builder builder = MethodSpec.methodBuilder(getProvisionMethodName(key)).returns(returnTypeName).addModifiers(Modifier.PUBLIC);
    builder.addStatement("$T result = $N", returnTypeName.box(), getFieldName(key)).beginControlFlow("if (result == null)").beginControlFlow("synchronized($L)", getLockFieldName(key)).addStatement("result = $N", getFieldName(key)).beginControlFlow("if (result == null)").addStatement("result = $L = $L()", getFieldName(key), getProvisionMethodName(key) + UNSCOPED_SUFFIX).endControlFlow().endControlFlow().endControlFlow().addStatement("return result");
    componentSpecBuilder.addMethod(builder.build());
    initializeBuilder.addStatement("$L = $L()", getFieldName(key), getProvisionMethodName(key));
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 97 with TypeName

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

the class PackagedHubInterfaceGenerator method generateAbstractInjectionMethod.

private void generateAbstractInjectionMethod(TypeElement cls) {
    if (!classesWithInjectionMethodGenerated.add(cls)) {
        return;
    }
    // TODO: this can only handle generic where type variables do not involve DI. A more general
    // solution
    // is needed.
    TypeName typeName = TypeName.get(cls.asType());
    if (typeName instanceof ParameterizedTypeName) {
        typeName = ((ParameterizedTypeName) typeName).rawType;
    }
    MethodSpec.Builder builder = MethodSpec.methodBuilder("inject").addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).addParameter(typeName, "v");
    interfaceBuilder.addMethod(builder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 98 with TypeName

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

the class HubInjectorGenerator4 method createStringProvidingKey.

private String createStringProvidingKey(BindingKey key) {
    // logger.n("key: %s", key);
    generateProvisionMethodIfNeeded(key);
    StringBuilder builder = new StringBuilder();
    TypeName typeName = key.getTypeName();
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    DependencyInfo dependencyInfo = dependencyInfos == null ? null : Preconditions.checkNotNull(Iterables.getFirst(dependencyInfos, null), "DI not found for key: " + key);
    if (dependencyInfo != null && dependencyInfo.getDependencySourceType().equals(DependencySourceType.CTOR_INJECTED_CLASS) && typeName instanceof ParameterizedTypeName && !utils.isProviderOrLazy(key) && !utils.isOptional(key)) {
        generateGenericInjectorIfNeeded(dependencyInfo);
        // generic ctor injected class
        builder = new StringBuilder("new ").append(utils.getGenericInjectorName(key)).append("().generate(");
        for (BindingKey k : utils.sortBindingKeys(dependencyInfo.getDependencies())) {
            builder.append(createStringProvidingKey(k)).append(", ");
        }
        utils.trimTrailing(builder, ", ");
        builder.append(")");
    } else {
        // types from hub or this
        builder.append(getProvisionMethodName(key)).append("()");
    }
    // logger.n("result: %s", builder.toString());
    return builder.toString();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 99 with TypeName

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

the class HubInjectorGenerator4 method generateGenericInjectorIfNeeded.

private void generateGenericInjectorIfNeeded(DependencyInfo dependencyInfo) {
    BindingKey dependant = dependencyInfo.getDependant();
    if (!generatedGenericInjectors.add(utils.getGenericInjectorName(dependant))) {
        return;
    }
    Builder genericInjectorBuilder = TypeSpec.classBuilder(utils.getGenericInjectorSimpleName(dependant)).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build()).addModifiers(Modifier.PUBLIC);
    TypeName returnType = dependant.getTypeName();
    MethodSpec.Builder generateBuilder = MethodSpec.methodBuilder("generate").addModifiers(Modifier.PUBLIC).returns(returnType);
    for (BindingKey key : utils.sortBindingKeys(dependencyInfo.getDependencies())) {
        generateBuilder.addParameter(key.getTypeName(), utils.getSourceCodeName(key));
    }
    DeclaredType type = (DeclaredType) utils.getTypeFromTypeName(returnType);
    TypeElement classElement = (TypeElement) type.asElement();
    ExecutableElement ctor = Preconditions.checkNotNull(utils.findInjectedCtor(classElement), "injector ctor not found for dI: " + dependencyInfo);
    TypeMirror ctorTypeMirror = types.asMemberOf(type, ctor);
    List<BindingKey> ctorDependencies = utils.getCtorDependencies(dependencies, dependant);
    StringBuilder statementBuilder = new StringBuilder("$T result = new $T(");
    for (BindingKey key : ctorDependencies) {
        statementBuilder.append(utils.getSourceCodeName(key)).append(", ");
    }
    utils.trimTrailing(statementBuilder, ", ");
    statementBuilder.append(")");
    generateBuilder.addStatement(statementBuilder.toString(), returnType, returnType);
    for (Element element : classElement.getEnclosedElements()) {
        if (element.getKind().equals(ElementKind.FIELD) && utils.isInjected(element)) {
            generateBuilder.addStatement("result.$N = $L", element.getSimpleName(), utils.getSourceCodeName(utils.getKeyForField(type, element)));
        }
        if (element.getKind().equals(ElementKind.METHOD) && utils.isInjected(element)) {
            statementBuilder = new StringBuilder("result.$L(");
            for (BindingKey key : utils.getDependenciesFromMethod((ExecutableType) types.asMemberOf(type, element), (ExecutableElement) element)) {
                statementBuilder.append(utils.getSourceCodeName(key)).append(", ");
            }
            utils.trimTrailing(statementBuilder, ", ");
            statementBuilder.append(")");
            generateBuilder.addStatement(statementBuilder.toString(), element.getSimpleName());
        }
    }
    generateBuilder.addStatement("return result");
    genericInjectorBuilder.addMethod(generateBuilder.build());
    JavaFile javaFile = JavaFile.builder(utils.getPackageString(utils.getClassFromKey(dependant)), genericInjectorBuilder.build()).build();
    try {
        javaFile.writeTo(processingEnv.getFiler());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : 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) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) TypeMirror(javax.lang.model.type.TypeMirror) JavaFile(com.squareup.javapoet.JavaFile) DeclaredType(javax.lang.model.type.DeclaredType)

Example 100 with TypeName

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

the class HubInjectorGenerator4 method generateGetPackagedInjectorMethod.

/**
 * generate a getXXX() that returns xxx field, which is a {@link DoubleCheckLazyProvider}.
 */
protected void generateGetPackagedInjectorMethod(String packageString) {
    generateGetPackagedInjectorMethodInternal(packageString);
    ClassName packagedInjectorClassName = getPackagedInjectorClassName(packageString);
    String packagedInjectorSourceName = Utils.getSourceCodeName(packagedInjectorClassName);
    TypeName fieldTypeName = ParameterizedTypeName.get(ClassName.get(DoubleCheckLazyProvider.class), packagedInjectorClassName);
    generateFieldIfNeeded(fieldTypeName, packagedInjectorSourceName, "$T.create(()->$L())", ClassName.get(DoubleCheckLazyProvider.class), getInternalPackagedInjectorGetterName(packagedInjectorClassName));
    injectorBuilder.addMethod(MethodSpec.methodBuilder(utils.getGetMethodName(packagedInjectorClassName)).addModifiers(Modifier.PRIVATE).returns(packagedInjectorClassName).addStatement("return $L.get()", packagedInjectorSourceName).build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ClassName(com.squareup.javapoet.ClassName)

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