Search in sources :

Example 1 with Binding

use of dagger.internal.Binding in project dagger by square.

the class InjectAdapterProcessor method generateInjectAdapter.

/**
   * Write a companion class for {@code type} that extends {@link Binding}.
   *
   * @param constructor the injectable constructor, or null if this binding
   *     supports members injection only.
   */
private void generateInjectAdapter(TypeElement type, ExecutableElement constructor, List<Element> fields) throws IOException {
    String packageName = getPackage(type).getQualifiedName().toString();
    TypeMirror supertype = getApplicationSupertype(type);
    if (supertype != null) {
        supertype = processingEnv.getTypeUtils().erasure(supertype);
    }
    ClassName injectedClassName = ClassName.get(type);
    ClassName adapterClassName = adapterName(injectedClassName, INJECT_ADAPTER_SUFFIX);
    boolean isAbstract = type.getModifiers().contains(ABSTRACT);
    boolean injectMembers = !fields.isEmpty() || supertype != null;
    boolean disambiguateFields = !fields.isEmpty() && (constructor != null) && !constructor.getParameters().isEmpty();
    boolean dependent = injectMembers || ((constructor != null) && !constructor.getParameters().isEmpty());
    TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addModifiers(PUBLIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(Binding.class), injectedClassName)).addJavadoc("$L", bindingTypeDocs(injectableType(type.asType()), isAbstract, injectMembers, dependent).toString());
    for (Element field : fields) {
        result.addField(memberBindingField(disambiguateFields, field));
    }
    if (constructor != null) {
        for (VariableElement parameter : constructor.getParameters()) {
            result.addField(parameterBindingField(disambiguateFields, parameter));
        }
    }
    if (supertype != null) {
        result.addField(supertypeBindingField(supertype));
    }
    result.addMethod(writeInjectAdapterConstructor(constructor, type, injectedClassName));
    if (dependent) {
        result.addMethod(attachMethod(constructor, fields, disambiguateFields, injectedClassName, supertype, true));
        result.addMethod(getDependenciesMethod(constructor, fields, disambiguateFields, supertype, true));
    }
    if (constructor != null) {
        result.addMethod(getMethod(constructor, disambiguateFields, injectMembers, injectedClassName));
    }
    if (injectMembers) {
        result.addMethod(membersInjectMethod(fields, disambiguateFields, injectedClassName, supertype));
    }
    JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
    javaFile.writeTo(processingEnv.getFiler());
}
Also used : Binding(dagger.internal.Binding) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Util.rawTypeToString(dagger.internal.codegen.Util.rawTypeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) VariableElement(javax.lang.model.element.VariableElement) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with Binding

use of dagger.internal.Binding in project dagger by square.

the class GraphAnalysisProcessor method process.

/**
   * Perform full-graph analysis on complete modules. This checks that all of
   * the module's dependencies are satisfied.
   */
@Override
public boolean process(Set<? extends TypeElement> types, RoundEnvironment env) {
    if (!env.processingOver()) {
        // passes.
        for (Element e : env.getElementsAnnotatedWith(Module.class)) {
            if (!(e instanceof TypeElement)) {
                error("@Module applies to a type, " + e.getSimpleName() + " is a " + e.getKind(), e);
                continue;
            }
            delayedModuleNames.add(((TypeElement) e).getQualifiedName().toString());
        }
        return false;
    }
    Set<Element> modules = new LinkedHashSet<Element>();
    for (String moduleName : delayedModuleNames) {
        modules.add(elements().getTypeElement(moduleName));
    }
    for (Element element : modules) {
        Map<String, Object> annotation = null;
        try {
            annotation = getAnnotation(Module.class, element);
        } catch (CodeGenerationIncompleteException e) {
            // skip this element. An up-stream compiler error is in play.
            continue;
        }
        TypeElement moduleType = (TypeElement) element;
        if (annotation == null) {
            error("Missing @Module annotation.", moduleType);
            continue;
        }
        if (annotation.get("complete").equals(Boolean.TRUE)) {
            Map<String, Binding<?>> bindings;
            try {
                bindings = processCompleteModule(moduleType, false);
                new ProblemDetector().detectCircularDependencies(bindings.values());
            } catch (ModuleValidationException e) {
                error("Graph validation failed: " + e.getMessage(), e.source);
                continue;
            } catch (InvalidBindingException e) {
                error("Graph validation failed: " + e.getMessage(), elements().getTypeElement(e.type));
                continue;
            } catch (RuntimeException e) {
                if (ERROR_NAMES_TO_PROPAGATE.contains(e.getClass().getName())) {
                    throw e;
                }
                error("Unknown error " + e.getClass().getName() + " thrown by javac in graph validation: " + e.getMessage(), moduleType);
                continue;
            }
            try {
                writeDotFile(moduleType, bindings);
            } catch (IOException e) {
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Graph visualization failed. Please report this as a bug.\n\n" + sw, moduleType);
            }
        }
        if (annotation.get("library").equals(Boolean.FALSE)) {
            Map<String, Binding<?>> bindings = processCompleteModule(moduleType, true);
            try {
                new ProblemDetector().detectUnusedBinding(bindings.values());
            } catch (IllegalStateException e) {
                error("Graph validation failed: " + e.getMessage(), moduleType);
            }
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProvidesBinding(dagger.internal.ProvidesBinding) Binding(dagger.internal.Binding) SetBinding(dagger.internal.SetBinding) CodeGenerationIncompleteException(dagger.internal.codegen.Util.CodeGenerationIncompleteException) InvalidBindingException(dagger.internal.Binding.InvalidBindingException) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) StringWriter(java.io.StringWriter) ProblemDetector(dagger.internal.ProblemDetector) FileObject(javax.tools.FileObject) Module(dagger.Module) PrintWriter(java.io.PrintWriter)

Aggregations

Binding (dagger.internal.Binding)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 VariableElement (javax.lang.model.element.VariableElement)2 ClassName (com.squareup.javapoet.ClassName)1 JavaFile (com.squareup.javapoet.JavaFile)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Module (dagger.Module)1 InvalidBindingException (dagger.internal.Binding.InvalidBindingException)1 ProblemDetector (dagger.internal.ProblemDetector)1 ProvidesBinding (dagger.internal.ProvidesBinding)1 SetBinding (dagger.internal.SetBinding)1 CodeGenerationIncompleteException (dagger.internal.codegen.Util.CodeGenerationIncompleteException)1 Util.elementToString (dagger.internal.codegen.Util.elementToString)1 Util.rawTypeToString (dagger.internal.codegen.Util.rawTypeToString)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 LinkedHashSet (java.util.LinkedHashSet)1