Search in sources :

Example 56 with ClassName

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

the class NewInjectorGenerator method addCallingProvisionMethod.

/**
   * Adds "[topLevelInjector.getxxxPackagedInjector.]getxxx()" to the builder.
   */
private void addCallingProvisionMethod(StringBuilder stringBuilder, NewBindingKey key, TypeElement referencingClass, ClassName packagedInjectorClassName) {
    generateProvisionMethodIfNeeded(key, referencingClass);
    ClassName originalPackagedInjector = getInjectorFor(key, referencingClass);
    ComponentInfo givenComponent = getComponentFromPackagedInjectorClassName(packagedInjectorClassName);
    ClassName targetPackagedInjectorClassName = getInjectorNameOfScope(originalPackagedInjector, givenComponent.getScope());
    if (!targetPackagedInjectorClassName.equals(packagedInjectorClassName)) {
        // System.out.println("addCallingProvisionMethod. packageInjector: " +
        // packagedInjectorClassName
        // + " field key: " + key);
        stringBuilder.append(TOP_LEVEL_INJECTOR_FIELD).append(".").append(Utils.getGetMethodName(targetPackagedInjectorClassName)).append("().");
    }
    stringBuilder.append(getProvisionMethodName(key)).append("()");
}
Also used : ClassName(com.squareup.javapoet.ClassName)

Example 57 with ClassName

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

the class NewInjectorGenerator method generateProvisionMethodForProviderOrLazy.

/**
   * For key like javax.inject.Provider<Foo> and dagger.Lazy<Foo>. Qualifier, if presented, will
   * also apply to element binding.
   */
private void generateProvisionMethodForProviderOrLazy(NewBindingKey key, TypeElement referencingClass, String suffix) {
    // System.out.println(String.format(
    // "generateProvisionMethodForProviderOrLazy: key %s, referencingClass: %s, suffix : %s.", key,
    // referencingClass, suffix));
    ClassName injectorClassName = getInjectorFor(key, referencingClass);
    TypeSpec anonymousTypeSpec = createAnonymousBuiltinTypeForUniqueBinding(injectorClassName, key, referencingClass);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName()).addStatement("return $L", anonymousTypeSpec);
    Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
    componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 58 with ClassName

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

the class NewInjectorGenerator method generateProvisionMethodFromClass.

/**
   * Handles generic.
   */
private void generateProvisionMethodFromClass(NewBindingKey key, TypeElement referencingClass, String suffix) {
    // System.out.println(
    // "generateProvisionMethodFromClass. key: " + key + " referencingClass: " +
    // referencingClass);
    ClassName packagedInjectorClassName = getInjectorFor(key, referencingClass);
    TypeSpec.Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
    TypeElement cls = getClassFromKey(key);
    ExecutableElement ctor = Utils.findInjectedCtor(cls, processingEnv);
    Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
    List<NewBindingKey> dependencyKeys = Utils.getDependenciesFromExecutableElement(ctor);
    if (key.getTypeName() instanceof ParameterizedTypeName) {
        List<NewBindingKey> specializedKeys = new ArrayList<>();
        Map<TypeVariableName, TypeName> map = Utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(), (ParameterizedTypeName) TypeName.get(cls.asType()));
        for (NewBindingKey k : dependencyKeys) {
            specializedKeys.add(Utils.specializeIfNeeded(k, map));
        }
        dependencyKeys = specializedKeys;
    }
    // System.out.println("generateProvisionMethodFromClass. dependencyKeys: " +
    // dependencyKeys);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
    methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName());
    StringBuilder builder = new StringBuilder("$T result = new $T(");
    if (dependencyKeys.size() > 0) {
        for (NewBindingKey dependencyKey : dependencyKeys) {
            // System.out.println("generateProvisionMethodFromClass. dependencyKey: "
            // + dependencyKey);
            generateProvisionMethodAndAppendAsParameter(dependencyKey, cls, packagedInjectorClassName, builder);
        }
        builder.delete(builder.length() - 2, builder.length());
    }
    builder.append(")");
    methodSpecBuilder.addStatement(builder.toString(), key.getTypeName(), key.getTypeName());
    if (Utils.hasInjectedFieldsOrMethods(cls, processingEnv)) {
        // System.out.println("generateProvisionMethodFromClass. hasInjected");
        generateInjectionMethod(key, referencingClass);
        methodSpecBuilder.addStatement("inject(result)");
    }
    methodSpecBuilder.addStatement("return result");
    injectorSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 59 with ClassName

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

the class NewInjectorGenerator method generateGetModuleMethod.

/**
   * Creates get_Foo_Module() for FooModule.
   */
private void generateGetModuleMethod(TypeElement scope, NewDependencyInfo dependencyInfo) {
    Preconditions.checkArgument(dependencyInfo.getProvisionMethodElement() != null, String.format("Expect one from module but get %s.", dependencyInfo));
    TypeElement module = dependencyInfo.getSourceClassElement();
    TypeName moduleTypeName = TypeName.get(module.asType());
    ClassName packagedInjectorClassName = getPackagedInjectorForNewDependencyInfo(scope, dependencyInfo);
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
    String moduleCanonicalNameConverted = Utils.getQualifiedName(module).replace(".", "_");
    componentSpecBuilder.addField(moduleTypeName, moduleCanonicalNameConverted, Modifier.PRIVATE);
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Utils.getGetMethodName(module)).addModifiers(Modifier.PRIVATE).addStatement("$T result = $N", module, moduleCanonicalNameConverted).returns(moduleTypeName);
    methodBuilder.beginControlFlow("if (result == null)");
    if (!isPassedModule(module)) {
        methodBuilder.addStatement("result = $N = new $T()", moduleCanonicalNameConverted, module);
    } else {
        methodBuilder.addStatement("result = $N = $L.$N()", moduleCanonicalNameConverted, TOP_LEVEL_INJECTOR_FIELD, Utils.getGetMethodName(module));
    }
    methodBuilder.endControlFlow();
    methodBuilder.addStatement("return result");
    MethodSpec methodSpec = methodBuilder.build();
    componentSpecBuilder.addMethod(methodSpec);
    modulesWithGetter.get(packagedInjectorClassName).put(ClassName.get(module), methodSpec);
}
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) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 60 with ClassName

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

the class NewInjectorGenerator method generateInjectionMethod.

private void generateInjectionMethod(TypeElement cls, ClassName packagedInjectorClassName, String methodName, boolean isPublic) {
    if (!injectedClassNamesForPackagedInjector.put(packagedInjectorClassName, ClassName.get(cls))) {
        return;
    }
    // messager.printMessage(Kind.NOTE,
    // String.format("generateInjectionMethod. cls: %s, injector: %s, method: %s", cls,
    // packagedInjectorClassName, methodName));
    Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(methodName).addModifiers(isPublic ? Modifier.PUBLIC : Modifier.PRIVATE).addParameter(ClassName.get(cls), "arg");
    // Inject closest ancestor first.
    TypeElement clsClosestInjectAncestor = getSuper(cls);
    while (clsClosestInjectAncestor != null && !Utils.hasInjectedFieldsOrMethods(clsClosestInjectAncestor, processingEnv)) {
        clsClosestInjectAncestor = getSuper(clsClosestInjectAncestor);
    }
    if (clsClosestInjectAncestor != null) {
        TypeElement scope = getComponentFromPackagedInjectorClassName(packagedInjectorClassName).getScope();
        generateInjectionMethod(clsClosestInjectAncestor, scope);
        ClassName ancestorPackagedInjector = getPackagedInjectorNameOfScope(Utils.getPackageString(clsClosestInjectAncestor), scope);
        StringBuilder stringBuilder = new StringBuilder();
        if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
            stringBuilder.append(TOP_LEVEL_INJECTOR_FIELD).append(".$N().");
        }
        stringBuilder.append("inject(($T) arg)");
        if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
            methodSpecBuilder.addStatement(stringBuilder.toString(), Utils.getGetMethodName(ancestorPackagedInjector), ClassName.get(clsClosestInjectAncestor));
        } else {
            // TODO(freeman): ClassName.get() removed the type parameters for now. Support it.
            methodSpecBuilder.addStatement(stringBuilder.toString(), ClassName.get(clsClosestInjectAncestor));
        }
    }
    for (VariableElement field : Utils.getInjectedFields(cls, processingEnv)) {
        // System.out.println("generateInjectionMethod. field: " + field);
        TypeMirror fieldType = field.asType();
        AnnotationMirror fieldQualifier = Utils.getQualifier(field);
        NewBindingKey fieldKey = NewBindingKey.get(fieldType, fieldQualifier);
        StringBuilder stringBuilder = new StringBuilder("arg.").append(field.getSimpleName()).append(" = ");
        addCallingProvisionMethod(stringBuilder, fieldKey, cls, packagedInjectorClassName);
        methodSpecBuilder.addStatement(stringBuilder.toString());
    }
    for (ExecutableElement method : Utils.getInjectedMethods(cls, processingEnv)) {
        // System.out.println("generateInjectionMethod. method: " + method);
        StringBuilder builder = new StringBuilder("arg.").append(method.getSimpleName()).append("(");
        List<NewBindingKey> methodArgs = Utils.getDependenciesFromExecutableElement(method);
        if (methodArgs.size() > 0) {
            for (NewBindingKey dependentKey : methodArgs) {
                addCallingProvisionMethod(builder, dependentKey, cls, packagedInjectorClassName);
                builder.append(", ");
            }
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append(")");
        methodSpecBuilder.addStatement(builder.toString());
    }
    componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Builder(com.squareup.javapoet.TypeSpec.Builder) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

ClassName (com.squareup.javapoet.ClassName)89 TypeSpec (com.squareup.javapoet.TypeSpec)56 MethodSpec (com.squareup.javapoet.MethodSpec)37 TypeElement (javax.lang.model.element.TypeElement)32 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)30 TypeName (com.squareup.javapoet.TypeName)25 Builder (com.squareup.javapoet.TypeSpec.Builder)16 TypeMirror (javax.lang.model.type.TypeMirror)15 Element (javax.lang.model.element.Element)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 NotNull (org.jetbrains.annotations.NotNull)10 CodeBlock (com.squareup.javapoet.CodeBlock)9 FieldSpec (com.squareup.javapoet.FieldSpec)9 ArrayList (java.util.ArrayList)9 VariableElement (javax.lang.model.element.VariableElement)9 Map (java.util.Map)8 JavaFile (com.squareup.javapoet.JavaFile)7 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)7 IOException (java.io.IOException)7 ProtoType (com.squareup.wire.schema.ProtoType)6