Skip to content

Commit

Permalink
Various fixes from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgruber committed Jun 12, 2013
1 parent 203114d commit 7b56b7e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
30 changes: 12 additions & 18 deletions core/src/main/java/dagger/internal/FailoverLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012 Square, Inc.
* Copyright (C) 2012 Google, Inc.
* 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.
Expand Down Expand Up @@ -53,23 +53,17 @@ public final class FailoverLoader implements Loader {
ClassLoader classLoader, boolean mustHaveInjections) {
try {
Binding<?> result = GeneratedAdapters.initInjectAdapter(className, classLoader);
if (result != null) {
return result;
}

// Fall back on reflection.
Class<?> c;
try {
if (result == null) {
// A null classloader is the system classloader.
classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader();
c = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
Class<?> c = classLoader.loadClass(className);
if (!c.isInterface()) {
result = ReflectiveAtInjectBinding.create(c, mustHaveInjections);
}
}
if (c.isInterface()) {
return null;
}
return ReflectiveAtInjectBinding.create(c, mustHaveInjections);
return result;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (RuntimeException e) {
logNotFound("Binding", className, e);
throw e;
Expand All @@ -80,9 +74,9 @@ public final class FailoverLoader implements Loader {
try {
StaticInjection result = GeneratedAdapters.initStaticInjection(injectedClass);
if (result != null) {
return result;
result = ReflectiveStaticInjection.create(injectedClass);
}
return ReflectiveStaticInjection.create(injectedClass);
return result;
} catch (RuntimeException e) {
logNotFound("Static injection", injectedClass.getName(), e);
throw e;
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/dagger/internal/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*/
public interface Loader {
/**
* Returns a binding that uses {@code @Inject} annotations, or null if no such binding can beNo.
* Returns a binding that uses {@code @Inject} annotations, or null if no valid binding can
* be found or created.
*/
Binding<?> getAtInjectBinding(
String key, String className, ClassLoader classLoader, boolean mustHaveInjections);
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/dagger/internal/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
public final class Modules {

private Modules() { }

/**
* Returns a full set of module adapters, including module adapters for included
* modules.
Expand Down
26 changes: 13 additions & 13 deletions core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2012 Square, Inc.
* 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.
Expand Down Expand Up @@ -27,7 +28,7 @@
* A utility for loading and initializing generated adapters.
*/
public final class GeneratedAdapters {
public static final String SEPARATOR = "$$";
private static final String SEPARATOR = "$$";
public static final String INJECT_ADAPTER_SUFFIX = SEPARATOR + "InjectAdapter";
public static final String MODULE_ADAPTER_SUFFIX = SEPARATOR + "ModuleAdapter";
public static final String STATIC_INJECTION_SUFFIX = SEPARATOR + "StaticInjection";
Expand All @@ -36,40 +37,39 @@ public final class GeneratedAdapters {
private GeneratedAdapters() { }

public static <T> ModuleAdapter<T> initModuleAdapter(Class<? extends T> moduleClass) {
return instantiate(moduleClass.getName(), MODULE_ADAPTER_SUFFIX, moduleClass.getClassLoader());
return instantiate(moduleClass.getName() + MODULE_ADAPTER_SUFFIX, moduleClass.getClassLoader());
}

public static Binding<?> initInjectAdapter(String className, ClassLoader classLoader) {
return instantiate(className, INJECT_ADAPTER_SUFFIX, classLoader);
return instantiate(className + INJECT_ADAPTER_SUFFIX, classLoader);
}

public static StaticInjection initStaticInjection(Class<?> injectedClass) {
return instantiate(injectedClass.getName(), STATIC_INJECTION_SUFFIX,
return instantiate(injectedClass.getName() + STATIC_INJECTION_SUFFIX,
injectedClass.getClassLoader());
}

private static <T> T instantiate(String className, String suffix, ClassLoader classLoader) {
String name = className + suffix;
private static <T> T instantiate(String name, ClassLoader classLoader) {
try {
// A null classloader is the system classloader.
classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader();
Class<?> generatedClass = classLoader.loadClass(name);
Constructor<?> constructor = generatedClass.getConstructor();
Constructor<?> constructor = generatedClass.getDeclaredConstructor();
constructor.setAccessible(true);
return (T) constructor.newInstance();
} catch (ClassNotFoundException e) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, className + " could not be found.", e);
logger.log(Level.FINE, name + " could not be found.", e);
}
return null; // Not finding a class is not inherently an error, unlike finding a bad class.
} catch (NoSuchMethodException e) {
throw new RuntimeException("No valid constructor found on " + className, e);
throw new RuntimeException("No default constructor found on " + name, e);
} catch (InstantiationException e) {
throw new RuntimeException("Failed to initialize " + className, e);
throw new RuntimeException("Failed to initialize " + name, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to initialize " + className, e);
throw new RuntimeException("Failed to initialize " + name, e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Error while initializing " + className, e.getCause());
throw new RuntimeException("Error while initializing " + name, e.getCause());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static String[] injectableTypesToKeys(Class<?>[] injectableTypes) {
}

@Override public void getBindings(Map<String, Binding<?>> bindings) {
for (Class<?> c = moduleClass; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = moduleClass; !c.equals(Object.class); c = c.getSuperclass()) {
for (Method method : c.getDeclaredMethods()) {
Provides provides = method.getAnnotation(Provides.class);
if (provides != null) {
Expand Down Expand Up @@ -138,7 +138,7 @@ public static <T> ModuleAdapter<T> createAdaptor(Class<? extends T> moduleClass)
if (annotation == null) {
throw new IllegalArgumentException("No @Module on " + moduleClass.getName());
}
if (moduleClass.getSuperclass() != Object.class) {
if (!moduleClass.getSuperclass().equals(Object.class)) {
throw new IllegalArgumentException(
"Modules must not extend from other classes: " + moduleClass.getName());
}
Expand Down

0 comments on commit 7b56b7e

Please sign in to comment.