Skip to content

Commit

Permalink
Document the behavior of ignoring duplicate installations of the same…
Browse files Browse the repository at this point in the history
… module instance.

Nit: invert an if statement.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=308673499
  • Loading branch information
ad-fu authored and cpovirk committed Apr 28, 2020
1 parent 1174710 commit 88154da
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions core/src/com/google/inject/spi/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,51 +314,53 @@ void scanForAnnotatedMethods() {

@Override
public void install(Module module) {
if (!modules.containsKey(module)) {
RecordingBinder binder = this;
boolean unwrapModuleSource = false;
// Update the module source for the new module
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.
Class<?> delegateClass = ((ProviderMethodsModule) module).getDelegateModuleClass();
if (moduleSource == null
|| !moduleSource.getModuleClassName().equals(delegateClass.getName())) {
moduleSource = getModuleSource(delegateClass);
unwrapModuleSource = true;
}
} else {
moduleSource = getModuleSource(module.getClass());
// Ignore duplicate installations of the same module instance.
if (modules.containsKey(module)) {
return;
}
RecordingBinder binder = this;
boolean unwrapModuleSource = false;
// Update the module source for the new module
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.
Class<?> delegateClass = ((ProviderMethodsModule) module).getDelegateModuleClass();
if (moduleSource == null
|| !moduleSource.getModuleClassName().equals(delegateClass.getName())) {
moduleSource = getModuleSource(delegateClass);
unwrapModuleSource = true;
}
boolean skipScanning = false;
if (module instanceof PrivateModule) {
binder = (RecordingBinder) binder.newPrivateBinder();
// Store the module in the private binder too so we scan for it.
binder.modules.put(module, new ModuleInfo(binder, moduleSource, false));
skipScanning = true; // don't scan this module in the parent's module set.
}
// Always store this in the parent binder (even if it was a private module)
// so that we know not to process it again, and so that scanners inherit down.
modules.put(module, new ModuleInfo(binder, moduleSource, skipScanning));
try {
module.configure(binder);
} catch (RuntimeException e) {
Collection<Message> messages = Errors.getMessagesFromThrowable(e);
if (!messages.isEmpty()) {
elements.addAll(messages);
} else {
addError(e);
}
}
binder.install(ProviderMethodsModule.forModule(module));
// We are done with this module, so undo module source change
if (unwrapModuleSource) {
moduleSource = moduleSource.getParent();
} else {
moduleSource = getModuleSource(module.getClass());
unwrapModuleSource = true;
}
boolean skipScanning = false;
if (module instanceof PrivateModule) {
binder = (RecordingBinder) binder.newPrivateBinder();
// Store the module in the private binder too so we scan for it.
binder.modules.put(module, new ModuleInfo(binder, moduleSource, false));
skipScanning = true; // don't scan this module in the parent's module set.
}
// Always store this in the parent binder (even if it was a private module)
// so that we know not to process it again, and so that scanners inherit down.
modules.put(module, new ModuleInfo(binder, moduleSource, skipScanning));
try {
module.configure(binder);
} catch (RuntimeException e) {
Collection<Message> messages = Errors.getMessagesFromThrowable(e);
if (!messages.isEmpty()) {
elements.addAll(messages);
} else {
addError(e);
}
}
binder.install(ProviderMethodsModule.forModule(module));
// We are done with this module, so undo module source change
if (unwrapModuleSource) {
moduleSource = moduleSource.getParent();
}
}

@Override
Expand Down

0 comments on commit 88154da

Please sign in to comment.