forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=158996281
- Loading branch information
1 parent
93bd6ad
commit 3b50e3d
Showing
8 changed files
with
806 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2017 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; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* A fluent builder class that returns a {@link Set}. Used in component implementations where a set | ||
* must be created in one fluent statement for inlined request fulfillments. | ||
*/ | ||
public final class SetBuilder<T> { | ||
private final List<T> contributions; | ||
|
||
private SetBuilder(int estimatedSize) { | ||
contributions = new ArrayList<>(estimatedSize); | ||
} | ||
|
||
/** | ||
* {@code estimatedSize} is the number of bindings which contribute to the set. They may each | ||
* provide {@code [0..n)} instances to the set. Because the final size is unknown, {@code | ||
* contributions} are collected in a list and only hashed in {@link #create()}. | ||
*/ | ||
public static <T> SetBuilder<T> newSetBuilder(int estimatedSize) { | ||
return new SetBuilder<T>(estimatedSize); | ||
} | ||
|
||
public SetBuilder<T> add(T t) { | ||
contributions.add(t); | ||
return this; | ||
} | ||
|
||
public SetBuilder<T> addAll(Collection<? extends T> collection) { | ||
contributions.addAll(collection); | ||
return this; | ||
} | ||
|
||
public Set<T> create() { | ||
switch (contributions.size()) { | ||
case 0: | ||
return Collections.emptySet(); | ||
case 1: | ||
return Collections.singleton(contributions.get(0)); | ||
default: | ||
return Collections.unmodifiableSet(new HashSet<>(contributions)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
java/dagger/internal/codegen/SetBindingRequestFulfillment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (C) 2017 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 com.squareup.javapoet.CodeBlock.of; | ||
import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.CodeBlock; | ||
import dagger.internal.SetBuilder; | ||
import java.util.Collections; | ||
import javax.lang.model.type.TypeMirror; | ||
|
||
/** | ||
* A {@link RequestFulfillment} for {@link | ||
* dagger.internal.codegen.ContributionBinding.Kind#SYNTHETIC_MULTIBOUND_SET} | ||
*/ | ||
final class SetBindingRequestFulfillment extends SimpleInvocationRequestFulfillment { | ||
private final ProvisionBinding binding; | ||
private final ImmutableMap<BindingKey, ResolvedBindings> resolvedBindingsMap; | ||
private final RequestFulfillmentRegistry registry; | ||
|
||
SetBindingRequestFulfillment( | ||
BindingKey bindingKey, | ||
ProvisionBinding binding, | ||
ImmutableMap<BindingKey, ResolvedBindings> resolvedBindingsMap, | ||
RequestFulfillmentRegistry registry, | ||
RequestFulfillment delegate) { | ||
super(bindingKey, delegate); | ||
this.binding = binding; | ||
this.resolvedBindingsMap = resolvedBindingsMap; | ||
this.registry = registry; | ||
} | ||
|
||
@Override | ||
CodeBlock getSimpleInvocation(DependencyRequest request, ClassName requestingClass) { | ||
// TODO(ronshapiro): if you have ImmutableSet on your classpath, use ImmutableSet.Builder | ||
// otherwise, we can consider providing our own static factories for multibinding cases where | ||
// all of the dependencies are @IntoSet | ||
switch (binding.dependencies().size()) { | ||
case 0: | ||
return collectionsStaticFactoryInvocation( | ||
request, requestingClass, CodeBlock.of("emptySet()")); | ||
case 1: | ||
{ | ||
DependencyRequest dependency = getOnlyElement(binding.dependencies()); | ||
if (isSingleValue(dependency)) { | ||
return collectionsStaticFactoryInvocation( | ||
request, | ||
requestingClass, | ||
CodeBlock.of( | ||
"singleton($L)", | ||
getRequestFulfillmentForDependency(dependency, requestingClass))); | ||
} | ||
} | ||
// fall through | ||
default: | ||
CodeBlock.Builder instantiation = CodeBlock.builder(); | ||
instantiation | ||
.add("$T.", SetBuilder.class) | ||
.add(maybeTypeParameter(request, requestingClass)) | ||
.add("newSetBuilder($L)", binding.dependencies().size()); | ||
for (DependencyRequest dependency : binding.dependencies()) { | ||
String builderMethod = isSingleValue(dependency) ? "add" : "addAll"; | ||
instantiation.add( | ||
".$L($L)", | ||
builderMethod, | ||
getRequestFulfillmentForDependency(dependency, requestingClass)); | ||
} | ||
return instantiation.add(".create()").build(); | ||
} | ||
} | ||
|
||
private CodeBlock getRequestFulfillmentForDependency( | ||
DependencyRequest dependency, ClassName requestingClass) { | ||
return registry | ||
.getRequestFulfillment(dependency.bindingKey()) | ||
.getSnippetForDependencyRequest(dependency, requestingClass); | ||
} | ||
|
||
private static CodeBlock collectionsStaticFactoryInvocation( | ||
DependencyRequest request, ClassName requestingClass, CodeBlock methodInvocation) { | ||
return CodeBlock.builder() | ||
.add("$T.", Collections.class) | ||
.add(maybeTypeParameter(request, requestingClass)) | ||
.add(methodInvocation) | ||
.build(); | ||
} | ||
|
||
private static CodeBlock maybeTypeParameter( | ||
DependencyRequest request, ClassName requestingClass) { | ||
TypeMirror elementType = SetType.from(request.key()).elementType(); | ||
return isTypeAccessibleFrom(elementType, requestingClass.packageName()) | ||
? of("<$T>", elementType) | ||
: of(""); | ||
} | ||
|
||
private boolean isSingleValue(DependencyRequest dependency) { | ||
return resolvedBindingsMap | ||
.get(dependency.bindingKey()) | ||
.contributionBinding() | ||
.contributionType() | ||
.equals(ContributionType.SET); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
java/dagger/internal/codegen/SimpleInvocationRequestFulfillment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2016 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 com.google.common.util.concurrent.Futures; | ||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.CodeBlock; | ||
|
||
/** | ||
* A {@link RequestFulfillment} that can fulfill its request with a simple call when possible, and | ||
* otherwise delegates to a backing provider field. | ||
*/ | ||
abstract class SimpleInvocationRequestFulfillment extends RequestFulfillment { | ||
private final RequestFulfillment delegate; | ||
|
||
SimpleInvocationRequestFulfillment(BindingKey bindingKey, RequestFulfillment delegate) { | ||
super(bindingKey); | ||
this.delegate = delegate; | ||
} | ||
|
||
abstract CodeBlock getSimpleInvocation(DependencyRequest request, ClassName requestingClass); | ||
|
||
@Override | ||
final CodeBlock getSnippetForDependencyRequest( | ||
DependencyRequest request, ClassName requestingClass) { | ||
switch (request.kind()) { | ||
case INSTANCE: | ||
return getSimpleInvocation(request, requestingClass); | ||
case FUTURE: | ||
return CodeBlock.of( | ||
"$T.immediateFuture($L)", Futures.class, getSimpleInvocation(request, requestingClass)); | ||
default: | ||
return delegate.getSnippetForDependencyRequest(request, requestingClass); | ||
} | ||
} | ||
|
||
@Override | ||
final CodeBlock getSnippetForFrameworkDependency( | ||
FrameworkDependency frameworkDependency, ClassName requestingClass) { | ||
return delegate.getSnippetForFrameworkDependency(frameworkDependency, requestingClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.