Skip to content

Commit

Permalink
Various fixes from review, including style fixes for square's codebas…
Browse files Browse the repository at this point in the history
…e, typos, clean up some extra logging, etc.
  • Loading branch information
cgruber committed Jun 27, 2013
1 parent 0be45bd commit 66e14ce
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package dagger.internal.codegen;

import static dagger.internal.loaders.GeneratedAdapters.INJECT_ADAPTER_SUFFIX;
import static dagger.internal.loaders.GeneratedAdapters.STATIC_INJECTION_SUFFIX;

import com.squareup.java.JavaWriter;
import dagger.MembersInjector;
import dagger.internal.Binding;
Expand Down Expand Up @@ -57,6 +54,8 @@
import static dagger.internal.codegen.TypeUtils.isCallableConstructor;
import static dagger.internal.codegen.TypeUtils.rawTypeToString;
import static dagger.internal.codegen.TypeUtils.typeToString;
import static dagger.internal.loaders.GeneratedAdapters.INJECT_ADAPTER_SUFFIX;
import static dagger.internal.loaders.GeneratedAdapters.STATIC_INJECTION_SUFFIX;
import static java.lang.reflect.Modifier.FINAL;
import static java.lang.reflect.Modifier.PRIVATE;
import static java.lang.reflect.Modifier.PUBLIC;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/dagger/ObjectGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package dagger;

import dagger.internal.FailoverLoader;

import dagger.internal.Binding;
import dagger.internal.FailoverLoader;
import dagger.internal.Keys;
import dagger.internal.Linker;
import dagger.internal.Loader;
Expand Down
42 changes: 10 additions & 32 deletions core/src/main/java/dagger/internal/FailoverLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,24 @@
import dagger.internal.loaders.ReflectiveAtInjectBinding;
import dagger.internal.loaders.ReflectiveModuleAdapter;
import dagger.internal.loaders.ReflectiveStaticInjection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Handles loading/finding of modules, injection bindings, and static injections by use of a
* strategy of "load the appropriate generaged code" or, if no such code is found, create a
* strategy of "load the appropriate generated code" or, if no such code is found, create a
* reflective equivalent.
*/
public final class FailoverLoader implements Loader {
private static final Logger logger = Logger.getLogger(Loader.class.getName());

/**
* Obtains a module adapter for {@code module} from the first responding resolver.
*/
@Override public <T> ModuleAdapter<T> getModuleAdapter(Class<? extends T> type, T instance) {
try {
ModuleAdapter<T> result = GeneratedAdapters.initModuleAdapter(type);
if (result == null) {
result = ReflectiveModuleAdapter.createAdaptor(type);
}
result.module = (instance == null) ? result.newModule() : instance;
return result;
} catch (RuntimeException e) {
logNotFound("Module adapter", type.getName(), e);
throw e;
ModuleAdapter<T> result = GeneratedAdapters.initModuleAdapter(type);
if (result == null) {
result = ReflectiveModuleAdapter.create(type);
}
result.module = (instance != null) ? instance : result.newModule();
return result;
}

@Override public Binding<?> getAtInjectBinding(String key, String className,
Expand All @@ -64,28 +56,14 @@ public final class FailoverLoader implements Loader {
return result;
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not find " + className + " needed for binding " + key, e);
} catch (RuntimeException e) {
logNotFound("Binding", className, e);
throw e;
}
}

@Override public StaticInjection getStaticInjection(Class<?> injectedClass) {
try {
StaticInjection result = GeneratedAdapters.initStaticInjection(injectedClass);
if (result == null) {
result = ReflectiveStaticInjection.create(injectedClass);
}
return result;
} catch (RuntimeException e) {
logNotFound("Static injection", injectedClass.getName(), e);
throw e;
}
}

private void logNotFound(String type, String name, RuntimeException e) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, String.format("Could not initialize a %s for %s.", type, name), e);
StaticInjection result = GeneratedAdapters.initStaticInjection(injectedClass);
if (result == null) {
result = ReflectiveStaticInjection.create(injectedClass);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private <T> void handleSetBindings(Map<String, Binding<?>> bindings, Method meth
* Creates a ReflectiveModuleAdapter or throws an {@code IllegalArgumentException}.
*/
@SuppressWarnings("unchecked") // Runtime checks validate that the result type matches 'T'.
public static <T> ModuleAdapter<T> createAdaptor(Class<? extends T> moduleClass) {
public static <T> ModuleAdapter<T> create(Class<? extends T> moduleClass) {
Module annotation = moduleClass.getAnnotation(Module.class);
if (annotation == null) {
throw new IllegalArgumentException("No @Module on " + moduleClass.getName());
Expand Down

0 comments on commit 66e14ce

Please sign in to comment.