Massive refactoring to exception handling. I'm trying to simplify things, but they are currently a little bit more complicated. I'll do another round shortly.
The main benefit of this change is that now all of our error handling flows through one class: Errors.java.
It takes care of
- managing the current source line,
- managing the current InjectionPoint
- building Messages
- toStrings
Because of this refactoring we now use almost exactly the same code for both ProvisionException and CreationException. The consequence of this is that ProvisionExceptions now include a full error report -- all of the classes injected. "Fail fast, but not too fast" now applies to Provide-time as well as Injector-create time.
I also made InjectionPoint into a public class in SPI. It replaces dependency. I like this change because "dependency" is a very abstract name, whereas InjectionPoint is very Guicey. Guice injects stuff. Dependencies are a consequence of this, but I like the API better exposing the core Guice abstractions directly.
This entire change needs further doc, simplification and cleanup. Todo.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@518 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/RequestStaticInjectionCommandProcessor.java b/src/com/google/inject/RequestStaticInjectionCommandProcessor.java
index 6bdc85e..e883cd6 100644
--- a/src/com/google/inject/RequestStaticInjectionCommandProcessor.java
+++ b/src/com/google/inject/RequestStaticInjectionCommandProcessor.java
@@ -16,10 +16,12 @@
package com.google.inject;
+import com.google.common.collect.Lists;
+import com.google.inject.InjectorImpl.SingleMemberInjector;
import com.google.inject.commands.RequestStaticInjectionCommand;
-import com.google.inject.internal.ErrorHandler;
-
-import java.util.ArrayList;
+import com.google.inject.internal.Errors;
+import com.google.inject.internal.ResolveFailedException;
+import com.google.inject.spi.SourceProviders;
import java.util.List;
/**
@@ -30,11 +32,10 @@
*/
class RequestStaticInjectionCommandProcessor extends CommandProcessor {
- private final List<StaticInjection> staticInjections
- = new ArrayList<StaticInjection>();
+ private final List<StaticInjection> staticInjections = Lists.newArrayList();
- RequestStaticInjectionCommandProcessor(ErrorHandler errorHandler) {
- super(errorHandler);
+ RequestStaticInjectionCommandProcessor(Errors errors) {
+ super(errors);
}
@Override public Boolean visitRequestStaticInjection(RequestStaticInjectionCommand command) {
@@ -56,14 +57,11 @@
}
}
- /**
- * A requested static injection.
- */
+ /** A requested static injection. */
private class StaticInjection {
final Object source;
final Class<?> type;
- final List<InjectorImpl.SingleMemberInjector> memberInjectors
- = new ArrayList<InjectorImpl.SingleMemberInjector>();
+ final List<SingleMemberInjector> memberInjectors = Lists.newArrayList();
public StaticInjection(Object source, Class type) {
this.source = source;
@@ -71,26 +69,29 @@
}
void validate(final InjectorImpl injector) {
- injector.withDefaultSource(source,
- new Runnable() {
- public void run() {
- injector.addSingleInjectorsForFields(
- type.getDeclaredFields(), true, memberInjectors);
- injector.addSingleInjectorsForMethods(
- type.getDeclaredMethods(), true, memberInjectors);
- }
- });
- }
-
- void injectMembers(InjectorImpl injector) {
- injector.callInContext(new ContextualCallable<Void>() {
- public Void call(InternalContext context) {
- for (InjectorImpl.SingleMemberInjector injector : memberInjectors) {
- injector.inject(context, null);
- }
- return null;
+ SourceProviders.withDefault(source, new Runnable() {
+ public void run() {
+ injector.addSingleInjectorsForFields(
+ type.getDeclaredFields(), true, memberInjectors, errors);
+ injector.addSingleInjectorsForMethods(
+ type.getDeclaredMethods(), true, memberInjectors, errors);
}
});
}
+
+ void injectMembers(InjectorImpl injector) {
+ try {
+ injector.callInContext(new ContextualCallable<Void>() {
+ public Void call(InternalContext context) {
+ for (SingleMemberInjector injector : memberInjectors) {
+ injector.inject(errors, context, null);
+ }
+ return null;
+ }
+ });
+ } catch (ResolveFailedException e) {
+ throw new AssertionError();
+ }
+ }
}
-}
\ No newline at end of file
+}