Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #70 from cgruber/renames
Browse files Browse the repository at this point in the history
rename get() -> create(), and getInstance() -> get()
  • Loading branch information
swankjesse committed Oct 22, 2012
2 parents cfefb68 + 0c423be commit bbcf3c7
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 78 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ By convention, `@Provides` methods are named with a `provide` prefix and module

### Building the Graph

The `@Inject` and `@Provides`-annotated classes form a graph of objects, linked by their dependencies. Obtain this graph by calling `ObjectGraph.get()`, which accepts one or more modules:
The `@Inject` and `@Provides`-annotated classes form a graph of objects, linked by their dependencies. Obtain this graph by calling `ObjectGraph.create()`, which accepts one or more modules:

```java
ObjectGraph objectGraph = ObjectGraph.get(new DripCoffeeModule());
ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule());
```

In order to put the graph to use we need to create an **entry point**. This is usually the main class that starts the application. In this example, the `CoffeeApp` class serves as the entry point. We ask the graph to provide an injected instance of this type:
Expand All @@ -113,8 +113,8 @@ class CoffeeApp implements Runnable {
}

public static void main(String[] args) {
ObjectGraph objectGraph = ObjectGraph.get(new DripCoffeeModule());
CoffeeApp coffeeApp = objectGraph.getInstance(CoffeeApp.class);
ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule());
CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class);
...
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ class LegacyModule {
Use `ObjectGraph.injectStatics()` to populate these static fields with their injected values:

```java
ObjectGraph objectGraph = ObjectGraph.get(new LegacyModule());
ObjectGraph objectGraph = ObjectGraph.create(new LegacyModule());
objectGraph.injectStatics();
```

Expand Down Expand Up @@ -324,7 +324,7 @@ public class CoffeeMakerTest {
@Inject Heater heater;

@Before public void setUp() {
ObjectGraph.get(new TestModule()).inject(this);
ObjectGraph.create(new TestModule()).inject(this);
}

@Module(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestApp implements Runnable {
}

public static void main(String[] args) {
ObjectGraph.get(new TestModule()).getInstance(TestApp.class).run();
ObjectGraph.create(new TestModule()).get(TestApp.class).run();
}

static class Dependency {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestApp implements Runnable {
}

public static void main(String[] args) {
ObjectGraph.get(new TestModule()).getInstance(TestApp.class).run();
ObjectGraph.create(new TestModule()).get(TestApp.class).run();
}

static interface Dependency {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/dagger/ObjectGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final class ObjectGraph {
* tools for graph validation, or call {@link #validate} to find problems in
* the graph at runtime.
*/
public static ObjectGraph get(Object... modules) {
public static ObjectGraph create(Object... modules) {

RuntimeAggregatingPlugin plugin = new RuntimeAggregatingPlugin(
new ClassloadingPlugin(), new ReflectivePlugin());
Expand Down Expand Up @@ -175,7 +175,7 @@ public void injectStatics() {
* @throws IllegalArgumentException if {@code type} is not one of this object
* graph's entry point types.
*/
public <T> T getInstance(Class<T> type) {
public <T> T get(Class<T> type) {
String key = Keys.get(type);
@SuppressWarnings("unchecked") // The linker matches keys to bindings by their type.
Binding<T> binding = (Binding<T>) getEntryPointBinding(key, key);
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/dagger/InjectStaticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertThat(InjectsOneField.staticField).isNull();
graph.injectStatics();
assertThat(InjectsOneField.staticField).isEqualTo("static");
Expand All @@ -63,7 +63,7 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertThat(InjectsStaticAndNonStatic.staticField).isNull();
graph.injectStatics();
assertThat(InjectsStaticAndNonStatic.staticField).isEqualTo("static");
Expand All @@ -82,7 +82,7 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertThat(InjectsStaticAndNonStatic.staticField).isNull();
InjectsStaticAndNonStatic object = new InjectsStaticAndNonStatic();
graph.inject(object);
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/dagger/InjectionOfLazyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class TestModule {

private <T> T injectWithModule(T ep, Object ... modules) {
// TODO(cgruber): Make og.inject(foo) return foo properly.
ObjectGraph og = ObjectGraph.get(modules);
ObjectGraph og = ObjectGraph.create(modules);
og.inject(ep);
return ep;
}
Expand Down
64 changes: 32 additions & 32 deletions core/src/test/java/dagger/InjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
G g = entryPoint.gProvider.get();
assertThat(g.a).isNotNull();
assertThat(g.b).isNotNull();
Expand Down Expand Up @@ -107,7 +107,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);

assertThat(entryPoint.aProvider.get()).isNotNull();
assertThat(entryPoint.aProvider.get()).isNotNull();
Expand All @@ -129,7 +129,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.fProvider.get()).isSameAs(entryPoint.fProvider.get());
assertThat(entryPoint.iProvider.get()).isSameAs(entryPoint.iProvider.get());
}
Expand Down Expand Up @@ -160,7 +160,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.a).isNotNull();
assertThat(one).isSameAs(entryPoint.aOne);
assertThat(two).isSameAs(entryPoint.aTwo);
Expand All @@ -185,7 +185,7 @@ class TestModule {

TestEntryPoint entryPoint = new TestEntryPoint();
TestModule module = new TestModule();
ObjectGraph.get(module).inject(entryPoint);
ObjectGraph.create(module).inject(entryPoint);
entryPoint.lProvider.get();

assertThat(module.a1).isNotNull();
Expand Down Expand Up @@ -216,7 +216,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);

assertThat(entryPoint.f1).isSameAs(entryPoint.f2);
assertThat(entryPoint.f1).isSameAs(entryPoint.n1.f1);
Expand All @@ -242,7 +242,7 @@ class TestEntryPoint {
class TestModule {
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.validate();
fail();
Expand All @@ -263,7 +263,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.q.f).isNotNull();
}

Expand Down Expand Up @@ -297,7 +297,7 @@ class TestModule {
R.injected = false;
TestEntryPoint entryPoint = new TestEntryPoint();
TestModule module = new TestModule();
ObjectGraph.get(module).inject(entryPoint);
ObjectGraph.create(module).inject(entryPoint);

assertThat(R.injected).isFalse();
assertThat(module.sInjected).isFalse();
Expand Down Expand Up @@ -325,7 +325,7 @@ class TestModule {
}

try {
ObjectGraph.get(new TestModule());
ObjectGraph.create(new TestModule());
fail();
} catch (IllegalArgumentException expected) {
}
Expand All @@ -344,7 +344,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.aProvider.get()).isSameAs(entryPoint.aProvider.get());
}

Expand All @@ -371,7 +371,7 @@ class OverridesModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new BaseModule(), new OverridesModule()).inject(entryPoint);
ObjectGraph.create(new BaseModule(), new OverridesModule()).inject(entryPoint);
E e = entryPoint.eProvider.get();
assertThat(e).isNotNull();
assertThat(e.f).isNotNull();
Expand All @@ -386,7 +386,7 @@ class TestEntryPoint {
class TestModule {
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.validate();
fail();
Expand All @@ -403,7 +403,7 @@ class TestEntryPoint {
class TestModule {
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.validate();
fail();
Expand Down Expand Up @@ -438,7 +438,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.extendsParameterizedType.string).isEqualTo("injected");
}

Expand All @@ -455,7 +455,7 @@ class TestModule {
}

TestEntryPoint entryPoint = new TestEntryPoint();
ObjectGraph.get(new TestModule()).inject(entryPoint);
ObjectGraph.create(new TestModule()).inject(entryPoint);
assertThat(entryPoint.listOfStrings).isEqualTo(Arrays.asList("a", "b"));
}

Expand All @@ -472,7 +472,7 @@ class TestModule {
}

try {
ObjectGraph.get(new TestModule());
ObjectGraph.create(new TestModule());
fail();
} catch (UnsupportedOperationException expected) {
}
Expand All @@ -494,7 +494,7 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.validate();
fail();
Expand All @@ -507,7 +507,7 @@ class TestModule {
class TestModule {
}

ObjectGraph.get(new TestModule());
ObjectGraph.create(new TestModule());
}

@Test public void getInstance() {
Expand All @@ -520,9 +520,9 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
assertEquals(0, (int) graph.getInstance(Integer.class));
assertEquals(1, (int) graph.getInstance(Integer.class));
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertEquals(0, (int) graph.get(Integer.class));
assertEquals(1, (int) graph.get(Integer.class));
}

@Test public void getInstanceRequiresEntryPoint() {
Expand All @@ -533,9 +533,9 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.getInstance(Integer.class);
graph.get(Integer.class);
fail();
} catch (IllegalArgumentException expected) {
}
Expand All @@ -549,8 +549,8 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
assertEquals(1, (int) graph.getInstance(int.class));
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertEquals(1, (int) graph.get(int.class));
}

@Test public void getInstanceOfArray() {
Expand All @@ -561,8 +561,8 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
assertEquals("[1, 2, 3]", Arrays.toString(graph.getInstance(int[].class)));
ObjectGraph graph = ObjectGraph.create(new TestModule());
assertEquals("[1, 2, 3]", Arrays.toString(graph.get(int[].class)));
}

@Test public void getInstanceAndInjectMembersUseDifferentKeys() {
Expand All @@ -584,8 +584,8 @@ BoundTwoWays provideBoundTwoWays() {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
BoundTwoWays provided = graph.getInstance(BoundTwoWays.class);
ObjectGraph graph = ObjectGraph.create(new TestModule());
BoundTwoWays provided = graph.get(BoundTwoWays.class);
assertEquals("Pepsi", provided.s);

BoundTwoWays membersInjected = new BoundTwoWays();
Expand All @@ -601,7 +601,7 @@ static class NoInjections {
class TestModule {
}

ObjectGraph.get(new TestModule()).validate();
ObjectGraph.create(new TestModule()).validate();
}

@Test public void nonEntryPointNeedsInjectAnnotation() {
Expand All @@ -612,7 +612,7 @@ class TestModule {
}
}

ObjectGraph graph = ObjectGraph.get(new TestModule());
ObjectGraph graph = ObjectGraph.create(new TestModule());
try {
graph.validate();
fail();
Expand Down

0 comments on commit bbcf3c7

Please sign in to comment.