Skip to content

Commit

Permalink
Merge pull request square#444 from google/moe_writing_branch_from_f53…
Browse files Browse the repository at this point in the history
…c3f9f3b293085727489fa39fb1134a4d36aaf

Moe sync 08/23
  • Loading branch information
ronshapiro authored Aug 23, 2016
2 parents 3009ccc + d434756 commit 570d124
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ private Optional<CodeBlock> initializeContributionBinding(BindingKey bindingKey)
case DELEGATE:
CodeBlock delegatingCodeBlock = CodeBlock.of(
"($T) $L",
binding.frameworkClass(),
binding.bindingType().frameworkClass(),
getMemberSelect(
Iterables.getOnlyElement(binding.dependencies()).bindingKey())
.getExpressionFor(name));
Expand Down
7 changes: 0 additions & 7 deletions compiler/src/main/java/dagger/internal/codegen/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
*/
abstract class Binding extends BindingDeclaration implements HasBindingType {

/**
* Returns the framework class associated with this binding.
*/
Class<?> frameworkClass() {
return bindingType().frameworkClass();
}

/** The {@link Key} that is provided by this binding. */
@Override
public abstract Key key();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.google.common.collect.TreeTraverser;
import dagger.Component;
import dagger.Reusable;
import dagger.Subcomponent;
import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor;
import dagger.internal.codegen.Key.HasKey;
import dagger.producers.Produced;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import dagger.MembersInjector;
import dagger.producers.Producer;
import javax.inject.Provider;
Expand All @@ -37,6 +39,9 @@ enum BindingType {
PRODUCTION(Producer.class),
;

static final ImmutableSet<BindingType> CONTRIBUTION_TYPES =
Sets.immutableEnumSet(PROVISION, PRODUCTION);

/** An object that is associated with a {@link BindingType}. */
interface HasBindingType {
/** The binding type of this object. */
Expand All @@ -45,7 +50,7 @@ interface HasBindingType {

private final Class<?> frameworkClass;

private BindingType(Class<?> frameworkClass) {
BindingType(Class<?> frameworkClass) {
this.frameworkClass = frameworkClass;
}

Expand Down
100 changes: 100 additions & 0 deletions compiler/src/main/java/dagger/internal/codegen/BindingTypeMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2014 The Dagger Authors.
*
* 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 static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.BindingType.CONTRIBUTION_TYPES;
import static dagger.internal.codegen.BindingType.MEMBERS_INJECTION;
import static dagger.internal.codegen.BindingType.PRODUCTION;
import static dagger.internal.codegen.BindingType.PROVISION;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import dagger.producers.Producer;
import javax.inject.Provider;

/**
* A mapper for associating a {@link DependencyRequest.Kind} to a {@link BindingType}, dependent on
* the type of code to be generated (e.g., for {@link Provider} or {@link Producer}).
*/
enum BindingTypeMapper {
FOR_PROVIDER() {
@Override public BindingType getBindingType(DependencyRequest.Kind requestKind) {
switch (requestKind) {
case INSTANCE:
case PROVIDER:
case PROVIDER_OF_LAZY:
case LAZY:
return PROVISION;
case MEMBERS_INJECTOR:
return MEMBERS_INJECTION;
case PRODUCED:
case PRODUCER:
throw new IllegalArgumentException(requestKind.toString());
default:
throw new AssertionError(requestKind);
}
}
},
FOR_PRODUCER() {
@Override public BindingType getBindingType(DependencyRequest.Kind requestKind) {
switch (requestKind) {
case INSTANCE:
case PRODUCED:
case PRODUCER:
return PRODUCTION;
case PROVIDER:
case PROVIDER_OF_LAZY:
case LAZY:
return PROVISION;
case MEMBERS_INJECTOR:
return MEMBERS_INJECTION;
default:
throw new AssertionError(requestKind);
}
}
};

static BindingTypeMapper forBindingType(BindingType bindingType) {
return bindingType.equals(PRODUCTION) ? FOR_PRODUCER : FOR_PROVIDER;
}

abstract BindingType getBindingType(DependencyRequest.Kind requestKind);

/**
* Returns the {@link BindingType} to use for a collection of requests of the same
* {@link BindingKey}. This allows factories to only take a single argument for multiple requests
* of the same key.
*/
BindingType getBindingType(Iterable<DependencyRequest> requests) {
ImmutableSet<BindingType> classes = FluentIterable.from(requests)
.transform(new Function<DependencyRequest, BindingType>() {
@Override public BindingType apply(DependencyRequest request) {
return getBindingType(request.kind());
}
})
.toSet();
if (classes.size() == 1) {
return getOnlyElement(classes);
} else if (classes.equals(CONTRIBUTION_TYPES)) {
return PROVISION;
} else {
throw new IllegalArgumentException("Bad set of framework classes: " + classes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import static com.google.auto.common.MoreElements.getAnnotationMirror;
import static dagger.internal.codegen.ConfigurationAnnotations.enclosedBuilders;
import static dagger.internal.codegen.ConfigurationAnnotations.getComponentDependencies;
import static dagger.internal.codegen.ConfigurationAnnotations.getComponentModules;
import static dagger.internal.codegen.ConfigurationAnnotations.getTransitiveModules;
import static dagger.internal.codegen.ConfigurationAnnotations.validateComponentDependencies;
import static dagger.internal.codegen.ErrorMessages.COMPONENT_ANNOTATED_REUSABLE;
import static javax.lang.model.element.ElementKind.CLASS;
import static javax.lang.model.element.ElementKind.INTERFACE;
Expand Down Expand Up @@ -239,6 +241,9 @@ public ComponentValidationReport validate(final TypeElement subject,

AnnotationMirror componentMirror =
getAnnotationMirror(subject, componentKind.annotationType()).get();
if (componentKind.isTopLevel()) {
validateComponentDependencies(builder, getComponentDependencies(componentMirror));
}
ImmutableList<TypeMirror> moduleTypes = getComponentModules(componentMirror);
moduleValidator.validateReferencedModules(
subject, builder, moduleTypes, componentKind.moduleKinds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import javax.lang.model.util.SimpleTypeVisitor6;
import javax.lang.model.util.Types;

/**
Expand Down Expand Up @@ -110,6 +111,30 @@ static ImmutableList<TypeMirror> convertClassArrayToListOfTypes(
return TO_LIST_OF_TYPES.visit(getAnnotationValue(annotationMirror, elementName), elementName);
}

static <T extends Element> void validateComponentDependencies(
ValidationReport.Builder<T> report, Iterable<TypeMirror> types) {
validateTypesAreDeclared(report, types, "component dependency");
}

private static <T extends Element> void validateTypesAreDeclared(
final ValidationReport.Builder<T> report, Iterable<TypeMirror> types, final String typeName) {
for (TypeMirror type : types) {
type.accept(new SimpleTypeVisitor6<Void, Void>(){
@Override
protected Void defaultAction(TypeMirror e, Void aVoid) {
report.addError(String.format("%s is not a valid %s type", e, typeName));
return null;
}

@Override
public Void visitDeclared(DeclaredType t, Void aVoid) {
// Declared types are valid
return null;
}
}, null);
}
}

private static final AnnotationValueVisitor<ImmutableList<TypeMirror>, String> TO_LIST_OF_TYPES =
new SimpleAnnotationValueVisitor6<ImmutableList<TypeMirror>, String>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ FactoryCreationStrategy factoryCreationStrategy() {
final TypeMirror factoryType() {
switch (contributionType()) {
case MAP:
return MapType.from(key()).unwrappedValueType(frameworkClass());
return MapType.from(key()).unwrappedValueType(bindingType().frameworkClass());
case SET:
return SetType.from(key()).elementType();
case SET_VALUES:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ abstract class FrameworkDependency {
*/
abstract BindingKey bindingKey();

/**
* The framework class to use for these requests.
*/
abstract Class<?> frameworkClass();
/** The binding type of the framework dependency. */
abstract BindingType bindingType();

/** The framework class to use for these requests. */
final Class<?> frameworkClass() {
return bindingType().frameworkClass();
}

/**
* The dependency requests that are all satisfied by one framework instance.
Expand Down Expand Up @@ -99,8 +102,8 @@ abstract class FrameworkDependency {
* instances of Binding, because it really depends on the order of the binding's dependencies,
* and two equal instances of Binding may have the same dependencies in a different order. */
static ImmutableSet<FrameworkDependency> frameworkDependenciesForBinding(Binding binding) {
DependencyRequestMapper dependencyRequestMapper =
DependencyRequestMapper.forBindingType(binding.bindingType());
BindingTypeMapper bindingTypeMapper =
BindingTypeMapper.forBindingType(binding.bindingType());
ImmutableSet.Builder<FrameworkDependency> frameworkDependencies = ImmutableSet.builder();
for (Collection<DependencyRequest> requests : groupByUnresolvedKey(binding)) {
frameworkDependencies.add(
Expand All @@ -109,7 +112,7 @@ static ImmutableSet<FrameworkDependency> frameworkDependenciesForBinding(Binding
FluentIterable.from(requests)
.transform(DependencyRequest.BINDING_KEY_FUNCTION)
.toSet()),
dependencyRequestMapper.getFrameworkClass(requests),
bindingTypeMapper.getBindingType(requests),
ImmutableSet.copyOf(requests)));
}
return frameworkDependencies.build();
Expand Down
Loading

0 comments on commit 570d124

Please sign in to comment.