Search in sources :

Example 61 with ClassName

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

the class NewInjectorGenerator method generateInjectorBuilder.

private void generateInjectorBuilder(ComponentInfo componentInfo, TypeSpec.Builder injectorBuilder) {
    TypeSpec.Builder builderBuilder = TypeSpec.classBuilder("Builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    ClassName injectorClassName = ClassName.get(topLevelPackageString, getTopLevelInjectorName(componentInfo));
    boolean hasParent = componentTree.get(componentInfo) != null;
    ClassName parentClassName = null;
    // set parent inject methods.
    if (hasParent) {
        parentClassName = ClassName.get(topLevelPackageString, getTopLevelInjectorName(componentTree.get(componentInfo)));
        Utils.addSetMethod(injectorClassName, builderBuilder, parentClassName);
    }
    /**
     * Set module methods. Theoretically, is a passed module is needed by a injector should be
     * decided by whether it is needed. Here is just a simpler way. All scoped modules go to
     * injector of the same scope. All unscoped modules go to every injector. It is not possible to
     * have modules of different scope in a injector. This way the modules created is a superset of
     * what are required. But that's fine because null can be passed in for modules not needed. This
     * is even unnoticeable when using builder to create injector. The null module is just never
     * referenced. Otherwise it is a bug, sadly it is not caught until runtime. This makes it easier
     * to create wrapper component defined dagger around core injectors. Anyway, passed module is
     * not a good idea and could/should be removed.
     */
    List<TypeElement> allNonNullaryCtorModules = new ArrayList<>();
    allNonNullaryCtorModules.addAll(nonNullaryCtorModules.get(componentInfo));
    allNonNullaryCtorModules.addAll(nonNullaryCtorUnscopedModules);
    for (TypeElement m : allNonNullaryCtorModules) {
        Utils.addSetMethod(injectorClassName, builderBuilder, (ClassName) ClassName.get(m.asType()));
    }
    // build() method.
    MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).returns(injectorClassName);
    StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
    if (hasParent) {
        String parentInjectorFieldName = Utils.getSourceCodeName(parentClassName);
        returnCodeBuilder.append(parentInjectorFieldName);
        if (!allNonNullaryCtorModules.isEmpty()) {
            returnCodeBuilder.append(", ");
        }
    }
    if (!allNonNullaryCtorModules.isEmpty()) {
        for (TypeElement module : Utils.sortByFullName(allNonNullaryCtorModules)) {
            String moduleFiledName = Utils.getSourceCodeName(TypeName.get(module.asType()));
            returnCodeBuilder.append(moduleFiledName).append(", ");
        }
        int size = returnCodeBuilder.length();
        returnCodeBuilder.delete(size - 2, size);
    }
    returnCodeBuilder.append(");");
    buildMethodBuilder.addCode(returnCodeBuilder.toString(), injectorClassName);
    builderBuilder.addMethod(buildMethodBuilder.build());
    injectorBuilder.addType(builderBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 62 with ClassName

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

the class NewInjectorGenerator method generateInheritedProvisionMethods.

private void generateInheritedProvisionMethods(ClassName packagedInjectorClassName) {
    ComponentInfo component = getComponentFromPackagedInjectorClassName(packagedInjectorClassName);
    Preconditions.checkArgument(componentTree.get(component) != null, String.format("No inherited provision methods to generate for %s", packagedInjectorClassName));
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
    ClassName containingPackagedInjectorClassName = getInjectorNameOfScope(packagedInjectorClassName, componentTree.get(component).getScope());
    for (NewBindingKey key : generatedBindingsForPackagedInjector.get(containingPackagedInjectorClassName)) {
        String provisionMethodName = getProvisionMethodName(key);
        componentSpecBuilder.addMethod(MethodSpec.methodBuilder(provisionMethodName).addModifiers(Modifier.PUBLIC).returns(key.getTypeName()).addStatement("return $L.$L()", CONTAINING_PACKAGED_INJECTOR_FIELD, provisionMethodName).build());
        Preconditions.checkState(generatedBindingsForPackagedInjector.put(packagedInjectorClassName, key), String.format("Injector %s already provides %s.", packagedInjectorClassName, key));
    }
}
Also used : Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 63 with ClassName

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

the class NewInjectorGenerator method generateProvisionMethodAndAppendAsParameter.

private void generateProvisionMethodAndAppendAsParameter(NewBindingKey key, TypeElement referencingClass, ClassName packagedInjectorClassName, StringBuilder builder) {
    TypeElement scope = getComponentFromPackagedInjectorClassName(packagedInjectorClassName).getScope();
    generateProvisionMethodIfNeeded(key, referencingClass);
    ClassName dependencyPackagedInjectorClassName = getInjectorOfScope(key, referencingClass, scope);
    if (!dependencyPackagedInjectorClassName.equals(packagedInjectorClassName)) {
        builder.append(TOP_LEVEL_INJECTOR_FIELD).append(".").append(Utils.getGetMethodName(dependencyPackagedInjectorClassName)).append("().");
    }
    builder.append(getProvisionMethodName(key)).append("(), ");
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName)

Example 64 with ClassName

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

the class Utils method getDependencyInfoByGeneric.

/**
   * Return {@link NewDependencyInfo} for the generalized {@link NewBindingKey} for
   * the give key. Null if not applicable or not exist.
   */
public static NewDependencyInfo getDependencyInfoByGeneric(SetMultimap<NewBindingKey, NewDependencyInfo> dependencies, NewBindingKey key) {
    TypeName typeName = key.getTypeName();
    Preconditions.checkArgument(key.getQualifier() == null, String.format("Binding to %s is supposed to be resolved through generic type of %s" + "but has non-null qualifier.", key, typeName));
    if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
        ClassName rawTypeName = parameterizedTypeName.rawType;
        NewBindingKey rawKey = NewBindingKey.get(rawTypeName);
        if (dependencies.containsKey(rawKey)) {
            NewDependencyInfo dependencyInfo = Iterables.getOnlyElement(dependencies.get(rawKey));
            TypeName formalTypeName = dependencyInfo.getDependant().getTypeName();
            Preconditions.checkState(formalTypeName instanceof ParameterizedTypeName, String.format("Formal type %s is not of type ParameterizedTypeName. Related actual type is %s", formalTypeName, parameterizedTypeName));
            Map<TypeVariableName, TypeName> mapTypeVariableToSpecialized = getMapFromTypeVariableToSpecialized(parameterizedTypeName, (ParameterizedTypeName) formalTypeName);
            Set<NewBindingKey> specializedDependencies = specializeIfNeeded(dependencyInfo.getDependencies(), mapTypeVariableToSpecialized);
            return new NewDependencyInfo(key, specializedDependencies, dependencyInfo.getSourceClassElement(), dependencyInfo.getProvisionMethodElement(), dependencyInfo.getType());
        }
    }
    return null;
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 65 with ClassName

use of com.squareup.javapoet.ClassName in project requery by requery.

the class EntityGenerator method generateListeners.

private void generateListeners(MethodSpec.Builder constructor) {
    for (Map.Entry<Element, ? extends ListenerDescriptor> entry : entity.listeners().entrySet()) {
        for (Annotation annotation : entry.getValue().listenerAnnotations()) {
            String annotationName = annotation.annotationType().getSimpleName().replace("Persist", "Insert").replace("Remove", "Delete");
            String methodName = Names.lowerCaseFirst(annotationName);
            // avoid hardcoding the package name here
            Element listener = elements.getTypeElement(PreInsertListener.class.getCanonicalName());
            PackageElement packageElement = elements.getPackageOf(listener);
            // generate the listener name
            String packageName = packageElement.getQualifiedName().toString();
            ClassName listenerName = ClassName.get(packageName, annotationName + "Listener");
            ParameterizedTypeName getterType = ParameterizedTypeName.get(listenerName, typeName);
            TypeSpec.Builder listenerBuilder = TypeSpec.anonymousClassBuilder("").addSuperinterface(getterType).addMethod(CodeGeneration.overridePublicMethod(methodName).addParameter(typeName, "entity").addStatement("$L()", entry.getKey().getSimpleName()).build());
            constructor.addStatement("$L.modifyListeners().add$L($L)", PROXY_NAME, annotationName + "Listener", listenerBuilder.build());
        }
    }
}
Also used : ClassName(com.squareup.javapoet.ClassName) PreInsertListener(io.requery.proxy.PreInsertListener) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

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