All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dagger.internal.codegen.ModuleProcessingStep Maven / Gradle / Ivy

There is a newer version: 2.55
Show newest version
/*
 * Copyright (C) 2014 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dagger.internal.codegen;

import com.google.auto.common.BasicAnnotationProcessor;
import com.google.auto.common.MoreElements;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static javax.lang.model.element.ElementKind.METHOD;

/**
 * An annotation processor for generating Dagger implementation code based on the {@link Module}
 * (and {@link Provides}) annotation.
 *
 * @author Gregory Kick
 * @since 2.0
 */
final class ModuleProcessingStep implements BasicAnnotationProcessor.ProcessingStep {
  private final Messager messager;
  private final ModuleValidator moduleValidator;
  private final ProvidesMethodValidator providesMethodValidator;
  private final BindsMethodValidator bindsMethodValidator;
  private final ProvisionBinding.Factory provisionBindingFactory;
  private final FactoryGenerator factoryGenerator;
  private final Set processedModuleElements = Sets.newLinkedHashSet();

  ModuleProcessingStep(
      Messager messager,
      ModuleValidator moduleValidator,
      ProvidesMethodValidator providesMethodValidator,
      ProvisionBinding.Factory provisionBindingFactory,
      BindsMethodValidator bindsMethodValidator,
      FactoryGenerator factoryGenerator) {
    this.messager = messager;
    this.moduleValidator = moduleValidator;
    this.providesMethodValidator = providesMethodValidator;
    this.bindsMethodValidator = bindsMethodValidator;
    this.provisionBindingFactory = provisionBindingFactory;
    this.factoryGenerator = factoryGenerator;
  }

  @Override
  public Set> annotations() {
    return ImmutableSet.of(Module.class, Provides.class, Binds.class);
  }

  @Override
  public Set process(
      SetMultimap, Element> elementsByAnnotation) {
    // first, check and collect all provides methods
    ImmutableSet validProvidesMethods =
        validateProvidesMethods(elementsByAnnotation);

    // second, check and collect all bind methods
    ImmutableSet validBindsMethods = validateBindsMethods(elementsByAnnotation);

    // process each module
    for (Element moduleElement :
        Sets.difference(elementsByAnnotation.get(Module.class), processedModuleElements)) {
      ValidationReport report =
          moduleValidator.validate(MoreElements.asType(moduleElement));
      report.printMessagesTo(messager);

      if (report.isClean()) {
        ImmutableSet.Builder moduleProvidesMethodsBuilder =
            ImmutableSet.builder();
        ImmutableSet.Builder moduleBindsMethodsBuilder =
            ImmutableSet.builder();
        List moduleMethods =
            ElementFilter.methodsIn(moduleElement.getEnclosedElements());
        for (ExecutableElement methodElement : moduleMethods) {
          if (isAnnotationPresent(methodElement, Provides.class)) {
            moduleProvidesMethodsBuilder.add(methodElement);
          }
          if (isAnnotationPresent(methodElement, Binds.class)) {
            moduleBindsMethodsBuilder.add(methodElement);
          }
        }
        ImmutableSet moduleProvidesMethods =
            moduleProvidesMethodsBuilder.build();
        ImmutableSet moduleBindsMethods =
            moduleBindsMethodsBuilder.build();

        if (Sets.difference(moduleProvidesMethods, validProvidesMethods).isEmpty()
            && Sets.difference(moduleBindsMethods, validBindsMethods).isEmpty()) {
          // all of the provides and bind methods in this module are valid!
          // time to generate some factories!
          ImmutableSet bindings =
              FluentIterable.from(moduleProvidesMethods)
                  .transform(
                      new Function() {
                        @Override
                        public ProvisionBinding apply(ExecutableElement providesMethod) {
                          return provisionBindingFactory.forProvidesMethod(
                              providesMethod,
                              MoreElements.asType(providesMethod.getEnclosingElement()));
                        }
                      })
                  .toSet();

          try {
            for (ProvisionBinding binding : bindings) {
              factoryGenerator.generate(binding);
            }
          } catch (SourceFileGenerationException e) {
            e.printMessageTo(messager);
          }
        }
      }
      processedModuleElements.add(moduleElement);
    }
    return ImmutableSet.of();
  }

  /* TODO(gak): Add an interface for Validators and combine these two methods and the ones in
   * ProducerModuleProcessingStep */

  private ImmutableSet validateBindsMethods(
      SetMultimap, Element> elementsByAnnotation) {
    ImmutableSet.Builder validBindsMethodsBuilder = ImmutableSet.builder();
    for (Element bindElement : elementsByAnnotation.get(Binds.class)) {
      if (bindElement.getKind().equals(METHOD)) {
        ExecutableElement bindsMethodElement = (ExecutableElement) bindElement;
        ValidationReport methodReport =
            bindsMethodValidator.validate(bindsMethodElement);
        methodReport.printMessagesTo(messager);
        if (methodReport.isClean()) {
          validBindsMethodsBuilder.add(bindsMethodElement);
        }
      }
    }
    return validBindsMethodsBuilder.build();
  }

  private ImmutableSet validateProvidesMethods(
      SetMultimap, Element> elementsByAnnotation) {
    ImmutableSet.Builder validProvidesMethodsBuilder = ImmutableSet.builder();
    for (Element providesElement : elementsByAnnotation.get(Provides.class)) {
      if (providesElement.getKind().equals(METHOD)) {
        ExecutableElement providesMethodElement = (ExecutableElement) providesElement;
        ValidationReport methodReport =
            providesMethodValidator.validate(providesMethodElement);
        methodReport.printMessagesTo(messager);
        if (methodReport.isClean()) {
          validProvidesMethodsBuilder.add(providesMethodElement);
        }
      }
    }
    return validProvidesMethodsBuilder.build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy