Skip to content

Commit

Permalink
closes issue square#261: add Provides.Type.SET_VALUES
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancole authored and sgoldfed committed Jul 29, 2013
1 parent 9bee7b0 commit bccf788
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

Version 1.1.0 *(TBD)*
----------------------------

* Allow multiple contributions to Set binding via `Provides.Type.SET_VALUES`


Version 1.0.1 *(2013-06-03)*
----------------------------

Expand Down
52 changes: 52 additions & 0 deletions compiler/src/it/extension-graph-setvalues/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2013 Square, Inc.
Copyright (C) 2013 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.
-->
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.dagger.tests</groupId>
<artifactId>extension-graph-setvalues</artifactId>
<version>HEAD-SNAPSHOT</version>
<name>Dagger Integration Test Basic</name>
<dependencies>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger</artifactId>
<version>@dagger.version@</version>
</dependency>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger-compiler</artifactId>
<version>@dagger.version@</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2013 Google, Inc.
* Copyright (C) 2013 Square, 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 test;

import dagger.ObjectGraph;
import dagger.Module;
import dagger.Provides;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import static dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;

/**
* Contributions to {@code SET_VALUES} binding do not affect Set of providers.
*/
class TestApp implements Runnable {
@Inject Set<Provider<String>> providers;
@Inject Set<String> strings;

@Override public void run() {
System.out.println(strings);
}

public static void main(String[] args) {
ObjectGraph root = ObjectGraph.create(new RootModule());
ObjectGraph extension = root.plus(new ExtensionModule());
extension.get(TestApp.class).run();
}

@Module(injects = TestApp.class)
static class RootModule {
@Provides Set<Provider<String>> providers() {
return new HashSet<Provider<String>>();
}
@Provides(type = SET_VALUES) Set<String> strings() {
return new HashSet<String>();
}
}

@Module(addsTo = RootModule.class, injects = TestApp.class)
static class ExtensionModule {
@Provides(type = SET) String addToSet() {
return "contributed";
}
}
}
52 changes: 52 additions & 0 deletions compiler/src/it/multiple-modules-setvalues/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2013 Square, Inc.
Copyright (C) 2013 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.
-->
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.dagger.tests</groupId>
<artifactId>multiple-modules-setvalues</artifactId>
<version>HEAD-SNAPSHOT</version>
<name>Dagger Integration Test Basic</name>
<dependencies>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger</artifactId>
<version>@dagger.version@</version>
</dependency>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger-compiler</artifactId>
<version>@dagger.version@</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2013 Google, Inc.
* Copyright (C) 2013 Square, 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 test;

import dagger.ObjectGraph;
import dagger.Module;
import dagger.Provides;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import static dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;

/**
* Contributions to {@code SET_VALUES} binding do not affect Set of providers.
*/
class TestApp implements Runnable {
@Inject Set<Provider<String>> providers;
@Inject Set<String> strings;

@Override public void run() {
System.out.println(strings);
}

public static void main(String[] args) {
ObjectGraph root = ObjectGraph.create(new RootModule(), new ContributingModule());
root.get(TestApp.class).run();
}

@Module(injects = TestApp.class)
static class RootModule {
@Provides Set<Provider<String>> providers() {
return new HashSet<Provider<String>>();
}
@Provides(type = SET_VALUES) Set<String> strings() {
return new HashSet<String>();
}
}

@Module(injects = TestApp.class, complete = false)
static class ContributingModule {
@Provides(type = SET) String addToSet() {
return "contributed";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import javax.tools.JavaFileManager;
import javax.tools.StandardLocation;

import static dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;
import static dagger.internal.codegen.TypeUtils.getAnnotation;
import static dagger.internal.codegen.TypeUtils.getPackage;
import static dagger.internal.codegen.TypeUtils.isInterface;
Expand Down Expand Up @@ -184,7 +186,8 @@ private Map<String, Binding<?>> processCompleteModule(TypeElement rootModule,

Binding previous = addTo.get(key);
if (previous != null) {
if (provides.type() == Provides.Type.SET && previous instanceof SetBinding) {
if ((provides.type() == SET || provides.type() == SET_VALUES)
&& previous instanceof SetBinding) {
// No duplicate bindings error if both bindings are set bindings.
} else {
String message = "Duplicate bindings for " + key;
Expand All @@ -206,6 +209,10 @@ private Map<String, Binding<?>> processCompleteModule(TypeElement rootModule,
SetBinding.add(addTo, setKey, binding);
break;

case SET_VALUES:
SetBinding.add(addTo, key, binding);
break;

default:
throw new AssertionError("Unknown @Provides type " + provides.type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import static dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;
import static dagger.internal.codegen.AdapterJavadocs.binderTypeDocs;
import static dagger.internal.codegen.TypeUtils.adapterName;
import static dagger.internal.codegen.TypeUtils.getAnnotation;
Expand Down Expand Up @@ -319,6 +321,13 @@ private void writeModuleAdapter(TypeElement type, Map<String, Object> module,
bindingClassName(providerMethod, methodToClassName, methodNameToNextId));
break;
}
case SET_VALUES: {
String key = GeneratorKeys.get(providerMethod);
writer.emitStatement("SetBinding.add(map, %s, new %s(module))",
JavaWriter.stringLiteral(key),
bindingClassName(providerMethod, methodToClassName, methodNameToNextId));
break;
}
default:
throw new AssertionError("Unknown @Provides type " + provides.type());
}
Expand Down Expand Up @@ -364,7 +373,8 @@ private boolean checkForDependencies(List<ExecutableElement> providerMethods) {

private boolean checkForMultibindings(List<ExecutableElement> providerMethods) {
for (ExecutableElement element : providerMethods) {
if (element.getAnnotation(Provides.class).type() == Provides.Type.SET) {
Provides.Type providesType = element.getAnnotation(Provides.class).type();
if (providesType == SET || providesType == SET_VALUES) {
return true;
}
}
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/dagger/Provides.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ enum Type {
* method as parameters. The {@code Set<T>} produced from the accumulation of values will be
* immutable.
*/
SET
SET,

/**
* Like {@link #SET}, except the method's return type is {@code Set<T>}, where any values are
* contributed to the set. An example use is to provide a default empty set binding, which is
* otherwise not possible using {@link #SET}.
*/
SET_VALUES;
}

Type type() default Type.UNIQUE;
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/dagger/internal/SetBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ public SetBinding(String key, Object requiredBy) {
}
}

@SuppressWarnings("unchecked") // Bindings<T> are the only thing added to contributors.
@SuppressWarnings("unchecked") // Only Binding<T> and Set<T> are added to contributors.
@Override public Set<T> get() {
Set<T> result = new LinkedHashSet<T>(contributors.size());
for (Binding<?> contributor : contributors) {
result.add((T) contributor.get()); // Let runtime exceptions through.
Object contribution = contributor.get(); // Let runtime exceptions through.
if (contributor.provideKey.equals(provideKey)) {
result.addAll((Set<T>) contribution);
} else {
result.add((T) contribution);
}
}
return Collections.unmodifiableSet(result);
}
Expand Down

0 comments on commit bccf788

Please sign in to comment.