Skip to content

Commit

Permalink
Create a dagger/guice interoperability library, which permits (with l…
Browse files Browse the repository at this point in the history
…imitations) the use of Dagger modules in Guice.

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=86617720
  • Loading branch information
cgruber authored and sameb committed Feb 24, 2015
1 parent 32780fa commit 0910c1e
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 59 deletions.
2 changes: 2 additions & 0 deletions build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jmx.src.dir=extensions/jmx/src
jndi.src.dir=extensions/jndi/src
throwingproviders.src.dir=extensions/throwingproviders/src
multibindings.src.dir=extensions/multibindings/src
daggeradapter.src.dir=extensions/dagger-adapter/src
privatemodules.src.dir=extensions/privatemodules/src
lifecycle.src.dir=extensions/lifecycle/src
persist.src.dir=extensions/persist/src
Expand All @@ -26,6 +27,7 @@ javadoc.packagenames=com.google.inject,com.google.inject.spi,\
com.google.inject.assistedinject,\
com.google.inject.throwingproviders,\
com.google.inject.multibindings,\
com.google.inject.daggeradapter,\
com.google.inject.privatemodules,\
com.google.inject.util,\
com.google.inject.persist,\
Expand Down
13 changes: 11 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</path>

<path id="javadoc.classpath">
<path refid="compile.classpath"/>
<path refid="compile.classpath"/>
<fileset dir="extensions">
<include name="*/lib/*.jar"/>
<include name="*/lib/*.jar"/>
</fileset>
<pathelement location="${build.dir}/classes"/>
</path>
Expand All @@ -35,6 +35,7 @@
<ant antfile="extensions/jndi/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/throwingproviders/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/multibindings/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/dagger-adapter/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/persist/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/grapher/build.xml" target="distjars" inheritAll="false"/>
<ant antfile="extensions/testlib/build.xml" target="distjars" inheritAll="false"/>
Expand Down Expand Up @@ -63,6 +64,9 @@
<copy toDir="${build.dir}/dist">
<fileset dir="extensions/multibindings/build" includes="*.jar"/>
</copy>
<copy toDir="${build.dir}/dist">
<fileset dir="extensions/dagger-adapter/build" includes="*.jar"/>
</copy>
<copy toDir="${build.dir}/dist">
<fileset dir="extensions/persist/build" includes="*.jar"/>
</copy>
Expand Down Expand Up @@ -159,6 +163,7 @@
<fileset dir="${jndi.src.dir}"/>
<fileset dir="${throwingproviders.src.dir}"/>
<fileset dir="${multibindings.src.dir}"/>
<fileset dir="${daggeradapter.src.dir}"/>
<fileset dir="${persist.src.dir}"/>
<fileset dir="${struts2.src.dir}"/>
<fileset dir="${grapher.src.dir}"/>
Expand Down Expand Up @@ -214,6 +219,9 @@
<group title="Multibinder Extension" packages="com.google.inject.multibindings"/>
<fileset dir="${multibindings.src.dir}"/>

<group title="Dagger Adapter" packages="com.google.inject.daggeradapter"/>
<fileset dir="${daggeradapter.src.dir}"/>

<group title="ThrowingProviders Extension" packages="com.google.inject.throwingproviders"/>
<fileset dir="${throwingproviders.src.dir}"/>

Expand Down Expand Up @@ -302,6 +310,7 @@
<ant dir="extensions/jndi" antfile="build.xml" target="clean"/>
<ant dir="extensions/throwingproviders" antfile="build.xml" target="clean"/>
<ant dir="extensions/multibindings" antfile="build.xml" target="clean"/>
<ant dir="extensions/dagger-adapter" antfile="build.xml" target="clean"/>
<ant dir="extensions/persist" antfile="build.xml" target="clean"/>
<ant dir="extensions/grapher" antfile="build.xml" target="clean"/>
<ant dir="extensions/testlib" antfile="build.xml" target="clean"/>
Expand Down
12 changes: 6 additions & 6 deletions core/src/com/google/inject/internal/ProviderMethodsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.ModuleAnnotatedMethodScanner;
import com.google.inject.spi.Dependency;
import com.google.inject.spi.InjectionPoint;
import com.google.inject.spi.Message;
import com.google.inject.spi.ModuleAnnotatedMethodScanner;
import com.google.inject.util.Modules;

import java.lang.annotation.Annotation;
Expand All @@ -55,8 +55,8 @@ public final class ProviderMethodsModule implements Module {
private static ModuleAnnotatedMethodScanner PROVIDES_BUILDER =
new ModuleAnnotatedMethodScanner() {
@Override
public <T> Key<T> prepareMethod(Binder binder, Annotation annotation, Key<T> key,
InjectionPoint injectionPoint) {
public <T> Key<T> prepareMethod(
Binder binder, Annotation annotation, Key<T> key, InjectionPoint injectionPoint) {
return key;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public static Module forModule(Module module) {
/**
* Returns a module which creates bindings methods in the module that match the scanner.
*/
public static Module forModule(Module module, ModuleAnnotatedMethodScanner scanner) {
public static Module forModule(Object module, ModuleAnnotatedMethodScanner scanner) {
return forObject(module, false, scanner);
}

Expand All @@ -114,8 +114,8 @@ private static Module forObject(Object object, boolean skipFastClassGeneration,
return new ProviderMethodsModule(object, skipFastClassGeneration, scanner);
}

public Module getDelegateModule() {
return delegate instanceof Module ? (Module) delegate : null;
public Object getDelegateModule() {
return delegate;
}

@Override
Expand Down
56 changes: 35 additions & 21 deletions core/src/com/google/inject/spi/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package com.google.inject.spi;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.inject.internal.InternalFlags.IncludeStackTraceOption;
import static com.google.inject.internal.InternalFlags.getIncludeStackTraceOption;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Binding;
Expand All @@ -44,6 +42,7 @@
import com.google.inject.internal.ConstantBindingBuilderImpl;
import com.google.inject.internal.Errors;
import com.google.inject.internal.ExposureBuilder;
import com.google.inject.internal.InternalFlags.IncludeStackTraceOption;
import com.google.inject.internal.PrivateElementsImpl;
import com.google.inject.internal.ProviderMethodsModule;
import com.google.inject.internal.util.SourceProvider;
Expand Down Expand Up @@ -111,14 +110,15 @@ public static List<Element> getElements(Stage stage, Iterable<? extends Module>
StackTraceElements.clearCache();
return Collections.unmodifiableList(binder.elements);
}

private static class ElementsAsModule implements Module {
private final Iterable<? extends Element> elements;

ElementsAsModule(Iterable<? extends Element> elements) {
this.elements = elements;
}

@Override
public void configure(Binder binder) {
for (Element element : elements) {
element.applyTo(binder);
Expand Down Expand Up @@ -191,6 +191,7 @@ private RecordingBinder(RecordingBinder parent, PrivateElementsImpl privateEleme
}

/*if[AOP]*/
@Override
public void bindInterceptor(
Matcher<? super Class<?>> classMatcher,
Matcher<? super Method> methodMatcher,
Expand All @@ -200,19 +201,23 @@ public void bindInterceptor(
}
/*end[AOP]*/

@Override
public void bindScope(Class<? extends Annotation> annotationType, Scope scope) {
elements.add(new ScopeBinding(getElementSource(), annotationType, scope));
}

@Override
@SuppressWarnings("unchecked") // it is safe to use the type literal for the raw type
public void requestInjection(Object instance) {
requestInjection((TypeLiteral<Object>) TypeLiteral.get(instance.getClass()), instance);
}

@Override
public <T> void requestInjection(TypeLiteral<T> type, T instance) {
elements.add(new InjectionRequest<T>(getElementSource(), type, instance));
}

@Override
public <T> MembersInjector<T> getMembersInjector(final TypeLiteral<T> typeLiteral) {
final MembersInjectorLookup<T> element
= new MembersInjectorLookup<T>(getElementSource(), typeLiteral);
Expand Down Expand Up @@ -244,21 +249,20 @@ public void install(Module module) {
Binder binder = this;
boolean unwrapModuleSource = false;
// Update the module source for the new module
if (!(module instanceof ProviderMethodsModule)) {
moduleSource = getModuleSource(module);
unwrapModuleSource = true;
} else {
if (module instanceof ProviderMethodsModule) {
// There are two reason's we'd want to get the module source in a ProviderMethodsModule.
// ModuleAnnotatedMethodScanner lets users scan their own modules for @Provides-like
// bindings. If they install the module at a top-level, then moduleSource can be null.
// Also, if they pass something other than 'this' to it, we'd have the wrong source.
Module delegate = ((ProviderMethodsModule) module).getDelegateModule();
if (delegate != null
&& (moduleSource == null
|| !moduleSource.getModuleClassName().equals(delegate.getClass().getName()))) {
Object delegate = ((ProviderMethodsModule) module).getDelegateModule();
if (moduleSource == null
|| !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) {
moduleSource = getModuleSource(delegate);
unwrapModuleSource = true;
}
} else {
moduleSource = getModuleSource(module);
unwrapModuleSource = true;
}
if (module instanceof PrivateModule) {
binder = binder.newPrivateBinder();
Expand Down Expand Up @@ -348,37 +352,45 @@ public RecordingBinder skipSources(Class... classesToSkip) {
return new RecordingBinder(this, null, newSourceProvider);
}

@Override
public PrivateBinder newPrivateBinder() {
PrivateElementsImpl privateElements = new PrivateElementsImpl(getElementSource());
RecordingBinder binder = new RecordingBinder(this, privateElements);
elements.add(privateElements);
return binder;
}


@Override
public void disableCircularProxies() {
elements.add(new DisableCircularProxiesOption(getElementSource()));
}


@Override
public void requireExplicitBindings() {
elements.add(new RequireExplicitBindingsOption(getElementSource()));
elements.add(new RequireExplicitBindingsOption(getElementSource()));
}


@Override
public void requireAtInjectOnConstructors() {
elements.add(new RequireAtInjectOnConstructorsOption(getElementSource()));
}

@Override
public void requireExactBindingAnnotations() {
elements.add(new RequireExactBindingAnnotationsOption(getElementSource()));
}

@Override
public void expose(Key<?> key) {
exposeInternal(key);
}

@Override
public AnnotatedElementBuilder expose(Class<?> type) {
return exposeInternal(Key.get(type));
}

@Override
public AnnotatedElementBuilder expose(TypeLiteral<?> type) {
return exposeInternal(Key.get(type));
}
Expand All @@ -388,7 +400,9 @@ private <T> AnnotatedElementBuilder exposeInternal(Key<T> key) {
addError("Cannot expose %s on a standard binder. "
+ "Exposed bindings are only applicable to private binders.", key);
return new AnnotatedElementBuilder() {
@Override
public void annotatedWith(Class<? extends Annotation> annotationType) {}
@Override
public void annotatedWith(Annotation annotation) {}
};
}
Expand All @@ -398,7 +412,7 @@ public void annotatedWith(Annotation annotation) {}
return builder;
}

private ModuleSource getModuleSource(Module module) {
private ModuleSource getModuleSource(Object module) {
StackTraceElement[] partialCallStack;
if (getIncludeStackTraceOption() == IncludeStackTraceOption.COMPLETE) {
partialCallStack = getPartialCallStack(new Throwable().getStackTrace());
Expand Down Expand Up @@ -426,7 +440,7 @@ private ElementSource getElementSource() {
}
IncludeStackTraceOption stackTraceOption = getIncludeStackTraceOption();
if (stackTraceOption == IncludeStackTraceOption.COMPLETE ||
(stackTraceOption == IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE
(stackTraceOption == IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE
&& declaringSource == null)) {
callStack = new Throwable().getStackTrace();
}
Expand All @@ -450,9 +464,9 @@ private ElementSource getElementSource() {
}

/**
* Removes the {@link #moduleSource} call stack from the beginning of current call stack. It
* also removes the last two elements in order to make {@link #install(Module)} the last call
* in the call stack.
* Removes the {@link #moduleSource} call stack from the beginning of current call stack. It
* also removes the last two elements in order to make {@link #install(Module)} the last call
* in the call stack.
*/
private StackTraceElement[] getPartialCallStack(StackTraceElement[] callStack) {
int toSkip = 0;
Expand All @@ -466,7 +480,7 @@ private StackTraceElement[] getPartialCallStack(StackTraceElement[] callStack) {
System.arraycopy(callStack, 1, partialCallStack, 0, chunkSize);
return partialCallStack;
}

@Override public String toString() {
return "Binder";
}
Expand Down
Loading

0 comments on commit 0910c1e

Please sign in to comment.