Silly code-style fixes
git-svn-id: https://google-guice.googlecode.com/svn/trunk@115 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/AbstractErrorHandler.java b/src/com/google/inject/AbstractErrorHandler.java
index 7c038ec..d84e66a 100644
--- a/src/com/google/inject/AbstractErrorHandler.java
+++ b/src/com/google/inject/AbstractErrorHandler.java
@@ -60,8 +60,7 @@
},
new Converter<Field>(Field.class) {
public String toString(Field f) {
- return "field " + f.getDeclaringClass().getName() + "."
- + f.getName();
+ return "field " + f.getDeclaringClass().getName() + "." + f.getName();
}
},
new Converter<Class>(Class.class) {
diff --git a/src/com/google/inject/AbstractModule.java b/src/com/google/inject/AbstractModule.java
index d9b612f..8251ac1 100644
--- a/src/com/google/inject/AbstractModule.java
+++ b/src/com/google/inject/AbstractModule.java
@@ -17,12 +17,11 @@
package com.google.inject;
import com.google.inject.util.Objects;
-
import java.util.Map;
import java.util.Properties;
/**
- * A support class for {@link Module}s which reduces repetition and
+ * A support class for {@link Module Modules} which reduces repetition and
* results in a more readable configuration. Simply extends this class,
* implement {@link #configure()}, and call the inherited methods which mirror
* those found in {@link ContainerBuilder}. For example:
@@ -53,7 +52,8 @@
configure();
- } finally {
+ }
+ finally {
this.builder = null;
}
}
diff --git a/src/com/google/inject/Binding.java b/src/com/google/inject/Binding.java
index 299b1a1..0326c92 100644
--- a/src/com/google/inject/Binding.java
+++ b/src/com/google/inject/Binding.java
@@ -30,8 +30,8 @@
final Object source;
final InternalFactory<? extends T> internalFactory;
- Binding(ContainerImpl container, Key<T> key,
- Object source, InternalFactory<? extends T> internalFactory) {
+ Binding(ContainerImpl container, Key<T> key, Object source,
+ InternalFactory<? extends T> internalFactory) {
this.container = container;
this.key = key;
this.source = source;
diff --git a/src/com/google/inject/ConstructionContext.java b/src/com/google/inject/ConstructionContext.java
index eb82ee5..fde3206 100644
--- a/src/com/google/inject/ConstructionContext.java
+++ b/src/com/google/inject/ConstructionContext.java
@@ -17,11 +17,11 @@
package com.google.inject;
import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import java.util.List;
import java.util.ArrayList;
+import java.util.List;
/**
* Context of a dependency construction. Used to manage circular references.
@@ -75,22 +75,18 @@
invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>();
}
- DelegatingInvocationHandler<T> invocationHandler =
- new DelegatingInvocationHandler<T>();
+ DelegatingInvocationHandler<T> invocationHandler
+ = new DelegatingInvocationHandler<T>();
invocationHandlers.add(invocationHandler);
- return Proxy.newProxyInstance(
- expectedType.getClassLoader(),
- new Class[] { expectedType },
- invocationHandler
- );
+ return Proxy.newProxyInstance(expectedType.getClassLoader(),
+ new Class[] { expectedType }, invocationHandler);
}
void setProxyDelegates(T delegate) {
if (invocationHandlers != null) {
- for (DelegatingInvocationHandler<T> invocationHandler
- : invocationHandlers) {
- invocationHandler.setDelegate(delegate);
+ for (DelegatingInvocationHandler<T> handler : invocationHandlers) {
+ handler.setDelegate(delegate);
}
}
}
@@ -109,11 +105,14 @@
try {
return method.invoke(delegate, args);
- } catch (IllegalAccessException e) {
+ }
+ catch (IllegalAccessException e) {
throw new RuntimeException(e);
- } catch (IllegalArgumentException e) {
+ }
+ catch (IllegalArgumentException e) {
throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
+ }
+ catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
diff --git a/src/com/google/inject/ConstructorInjector.java b/src/com/google/inject/ConstructorInjector.java
index d384742..d242b91 100644
--- a/src/com/google/inject/ConstructorInjector.java
+++ b/src/com/google/inject/ConstructorInjector.java
@@ -23,7 +23,7 @@
* Injects constructors.
*
* @author crazybob@google.com (Bob Lee)
-*/
+ */
class ConstructorInjector<T> implements Factory<T> {
final Class<T> implementation;
@@ -55,17 +55,18 @@
constructor.getGenericParameterTypes(),
inject.value()
);
- } catch (ContainerImpl.MissingDependencyException e) {
+ }
+ catch (ContainerImpl.MissingDependencyException e) {
e.handle(container.errorHandler);
return null;
}
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
private Constructor<T> findConstructorIn(Class<T> implementation) {
Constructor<T> found = null;
- for (Constructor<T> constructor
- : implementation.getDeclaredConstructors()) {
+ for (Constructor<T> constructor : implementation.getDeclaredConstructors())
+ {
if (constructor.getAnnotation(Inject.class) != null) {
if (found != null) {
container.errorHandler.handle(
@@ -83,19 +84,21 @@
// instead.
try {
return implementation.getDeclaredConstructor();
- } catch (NoSuchMethodException e) {
- container.errorHandler.handle(ErrorMessages.MISSING_CONSTRUCTOR, implementation);
+ }
+ catch (NoSuchMethodException e) {
+ container.errorHandler.handle(
+ ErrorMessages.MISSING_CONSTRUCTOR, implementation);
return ContainerImpl.invalidConstructor();
}
}
/**
- * Construct an instance. Returns {@code Object} instead of {@code T}
- * because it may return a proxy.
+ * Construct an instance. Returns {@code Object} instead of {@code T} because
+ * it may return a proxy.
*/
Object construct(InternalContext context, Class<? super T> expectedType) {
- ConstructionContext<T> constructionContext =
- context.getConstructionContext(this);
+ ConstructionContext<T> constructionContext
+ = context.getConstructionContext(this);
// We have a circular reference between constructors. Return a proxy.
if (constructionContext.isConstructing()) {
@@ -115,11 +118,12 @@
// First time through...
constructionContext.startConstruction();
try {
- Object[] parameters =
- ContainerImpl.getParameters(context, parameterInjectors);
+ Object[] parameters
+ = ContainerImpl.getParameters(context, parameterInjectors);
t = newInstance(parameters);
constructionContext.setProxyDelegates(t);
- } finally {
+ }
+ finally {
constructionContext.finishConstruction();
}
@@ -128,35 +132,38 @@
constructionContext.setCurrentReference(t);
// Inject fields and methods.
- for (int i = 0; i < injectors.length; i++) {
- injectors[i].inject(context, t);
+ for (ContainerImpl.Injector injector : injectors) {
+ injector.inject(context, t);
}
return t;
- } catch (InvocationTargetException e) {
+ }
+ catch (InvocationTargetException e) {
throw new RuntimeException(e);
- } finally {
+ }
+ finally {
constructionContext.removeCurrentReference();
}
}
- @SuppressWarnings({"unchecked"})
- private T newInstance(Object[] parameters)
- throws InvocationTargetException {
- return (T) constructionProxy.newInstance(parameters);
+ @SuppressWarnings("unchecked")
+ private T newInstance(Object[] parameters) throws InvocationTargetException {
+ return constructionProxy.newInstance(parameters);
}
public T get() {
try {
return container.callInContext(new ContextualCallable<T>() {
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public T call(InternalContext context) {
return (T) construct(context, implementation);
}
});
- } catch (RuntimeException e) {
+ }
+ catch (RuntimeException e) {
throw e;
- } catch (Exception e) {
+ }
+ catch (Exception e) {
throw new RuntimeException(e);
}
}
diff --git a/src/com/google/inject/Container.java b/src/com/google/inject/Container.java
index f7c43ba..8b4eae4 100644
--- a/src/com/google/inject/Container.java
+++ b/src/com/google/inject/Container.java
@@ -16,17 +16,17 @@
package com.google.inject;
-import java.util.Map;
import java.util.List;
+import java.util.Map;
/**
* Injects dependencies into constructors, methods and fields annotated with
* @{@link Inject}.
*
- * <p>When injecting a method or constructor, you can additionally annotate
- * its parameters with @{@link Inject} and specify a dependency name. When
- * a parameter has no annotation, the container uses the name from the method
- * or constructor's @{@link Inject} annotation respectively.
+ * <p>When injecting a method or constructor, you can additionally annotate its
+ * parameters with @{@link Inject} and specify a dependency name. When a
+ * parameter has no annotation, the container uses the name from the method or
+ * constructor's @{@link Inject} annotation respectively.
*
* <p>For example:
*
@@ -64,8 +64,8 @@
* Foo foo = fooFactory.get();
* </pre>
*
- * @see ContainerBuilder
* @author crazybob@google.com (Bob Lee)
+ * @see ContainerBuilder
*/
public interface Container {
@@ -75,8 +75,8 @@
void injectMembers(Object o);
/**
- * Gets a factory which injects the given class's constructor and creates
- * new instances of {@code T}.
+ * Gets a factory which injects the given class's constructor and creates new
+ * instances of {@code T}.
*/
<T> Factory<T> getCreator(Class<T> implementation);
diff --git a/src/com/google/inject/ContainerBuilder.java b/src/com/google/inject/ContainerBuilder.java
index c2163ba..f3d6ca3 100644
--- a/src/com/google/inject/ContainerBuilder.java
+++ b/src/com/google/inject/ContainerBuilder.java
@@ -16,6 +16,7 @@
package com.google.inject;
+import com.google.inject.ContainerImpl.Injector;
import static com.google.inject.Scopes.CONTAINER;
import static com.google.inject.Scopes.CONTAINER_NAME;
import static com.google.inject.Scopes.DEFAULT;
@@ -27,9 +28,6 @@
import static com.google.inject.util.Objects.nonNull;
import com.google.inject.util.Stopwatch;
import com.google.inject.util.ToStringBuilder;
-
-import org.aopalliance.intercept.MethodInterceptor;
-
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
@@ -41,6 +39,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
+import org.aopalliance.intercept.MethodInterceptor;
/**
* Builds a dependency injection {@link Container}. Binds {@link Key}s to
@@ -52,31 +51,31 @@
* <p>Default bindings include:
*
* <ul>
- * <li>A {@code Factory<T>} for each binding of type {@code T}
- * <li>The {@link Container} iself
- * <li>The {@link Logger} for the class being injected
+ * <li>A {@code Factory<T>} for each binding of type {@code T}
+ * <li>The {@link Container} iself
+ * <li>The {@link Logger} for the class being injected
* </ul>
*
- * <p>Converts constants as needed from {@code String} to any primitive type
- * in addition to {@code enum} and {@code Class<?>}.
+ * <p>Converts constants as needed from {@code String} to any primitive type in
+ * addition to {@code enum} and {@code Class<?>}.
*
* @author crazybob@google.com (Bob Lee)
*/
public final class ContainerBuilder extends SourceConsumer {
- private static final Logger logger =
- Logger.getLogger(ContainerBuilder.class.getName());
+ private static final Logger logger
+ = Logger.getLogger(ContainerBuilder.class.getName());
- final List<BindingBuilder<?>> bindingBuilders =
- new ArrayList<BindingBuilder<?>>();
- final List<ConstantBindingBuilder> constantBindingBuilders =
- new ArrayList<ConstantBindingBuilder>();
- final List<LinkedBindingBuilder<?>> linkedBindingBuilders =
- new ArrayList<LinkedBindingBuilder<?>>();
+ final List<BindingBuilder<?>> bindingBuilders
+ = new ArrayList<BindingBuilder<?>>();
+ final List<ConstantBindingBuilder> constantBindingBuilders
+ = new ArrayList<ConstantBindingBuilder>();
+ final List<LinkedBindingBuilder<?>> linkedBindingBuilders
+ = new ArrayList<LinkedBindingBuilder<?>>();
final Map<String, Scope> scopes = new HashMap<String, Scope>();
- final List<StaticInjection> staticInjections =
- new ArrayList<StaticInjection>();
+ final List<StaticInjection> staticInjections
+ = new ArrayList<StaticInjection>();
ContainerImpl container;
@@ -85,21 +84,22 @@
*/
final Set<Message> errorMessages = new LinkedHashSet<Message>();
- private static final InternalFactory<Container> CONTAINER_FACTORY =
- new InternalFactory<Container>() {
- public Container get(InternalContext context) {
- return context.getContainer();
- }
- };
+ private static final InternalFactory<Container> CONTAINER_FACTORY
+ = new InternalFactory<Container>() {
+ public Container get(InternalContext context) {
+ return context.getContainer();
+ }
+ };
- private static final InternalFactory<Logger> LOGGER_FACTORY =
- new InternalFactory<Logger>() {
- public Logger get(InternalContext context) {
- Member member = context.getExternalContext().getMember();
- return member == null ? Logger.getAnonymousLogger()
- : Logger.getLogger(member.getDeclaringClass().getName());
- }
- };
+ private static final InternalFactory<Logger> LOGGER_FACTORY
+ = new InternalFactory<Logger>() {
+ public Logger get(InternalContext context) {
+ Member member = context.getExternalContext().getMember();
+ return member == null
+ ? Logger.getAnonymousLogger()
+ : Logger.getLogger(member.getDeclaringClass().getName());
+ }
+ };
static final String UNKNOWN_SOURCE = "[unknown source]";
@@ -150,9 +150,9 @@
* and method queries.
*
* @param classQuery matches classes the interceptor should apply to. For
- * example: {@code only(Runnable.class)}.
+ * example: {@code only(Runnable.class)}.
* @param methodQuery matches methods the interceptor should apply to. For
- * example: {@code annotatedWith(Transactional.class)}.
+ * example: {@code annotatedWith(Transactional.class)}.
* @param interceptors to apply
*/
public void intercept(Query<? super Class<?>> classQuery,
@@ -168,7 +168,8 @@
public void scope(String name, Scope scope) {
if (scopes.containsKey(nonNull(name, "name"))) {
addError(source(), ErrorMessages.DUPLICATE_SCOPES, name);
- } else {
+ }
+ else {
scopes.put(nonNull(name, "name"), nonNull(scope, "scope"));
}
}
@@ -292,12 +293,11 @@
* which were registered using {@link #requestStaticInjection(Class...)}.
*
* @param preload If true, the container will load all container-scoped
- * bindings now. If false, the container will lazily load them. Eager
- * loading is appropriate for production use (catch errors early and take
- * any performance hit up front) while lazy loading can speed development.
- *
+ * bindings now. If false, the container will lazily load them. Eager
+ * loading is appropriate for production use (catch errors early and take
+ * any performance hit up front) while lazy loading can speed development.
* @throws ContainerCreationException if configuration errors are found. The
- * expectation is that the application will log this exception and exit.
+ * expectation is that the application will log this exception and exit.
* @throws IllegalStateException if called more than once
*/
public synchronized Container create(boolean preload)
@@ -306,16 +306,14 @@
// Create the container.
ensureNotCreated();
- Map<Key<?>, Binding<?>> bindings =
- new HashMap<Key<?>, Binding<?>>();
- container = new ContainerImpl(
- proxyFactoryBuilder.create(), bindings);
+ Map<Key<?>, Binding<?>> bindings = new HashMap<Key<?>, Binding<?>>();
+ container = new ContainerImpl(proxyFactoryBuilder.create(), bindings);
createConstantBindings();
// Commands to execute before returning the Container instance.
- final List<ContextualCallable<Void>> preloaders =
- new ArrayList<ContextualCallable<Void>>();
+ final List<ContextualCallable<Void>> preloaders
+ = new ArrayList<ContextualCallable<Void>>();
createBindings(preload, preloaders);
createLinkedBindings();
@@ -380,8 +378,7 @@
}
}
- private <T> void createLinkedBinding(
- LinkedBindingBuilder<T> builder) {
+ private <T> void createLinkedBinding(LinkedBindingBuilder<T> builder) {
// TODO: Support linking to a later-declared link?
Key<? extends T> destinationKey = builder.getDestination();
if (destinationKey == null) {
@@ -396,14 +393,13 @@
return;
}
- Binding<?> binding =
- Binding.newInstance(container, builder.getKey(), builder.getSource(),
- destination.getInternalFactory());
+ Binding<?> binding = Binding.newInstance(container, builder.getKey(),
+ builder.getSource(), destination.getInternalFactory());
putBinding(binding);
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
private <T> Binding<T> getBinding(Key<T> destinationKey) {
return (Binding<T>) container.internalBindings().get(destinationKey);
}
@@ -415,13 +411,13 @@
}
}
- private <T> void createBinding(BindingBuilder<T> builder,
- boolean preload, List<ContextualCallable<Void>> preloaders) {
+ private <T> void createBinding(BindingBuilder<T> builder, boolean preload,
+ List<ContextualCallable<Void>> preloaders) {
final Key<T> key = builder.getKey();
- final InternalFactory<? extends T> factory =
- builder.getInternalFactory(container);
- Binding<?> binding = Binding.newInstance(
- container, key, builder.getSource(), factory);
+ final InternalFactory<? extends T> factory
+ = builder.getInternalFactory(container);
+ Binding<?> binding
+ = Binding.newInstance(container, key, builder.getSource(), factory);
putBinding(binding);
@@ -430,7 +426,8 @@
if (preload || builder.shouldPreload()) {
preloaders.add(new BindingPreloader(key, factory));
}
- } else {
+ }
+ else {
if (builder.shouldPreload()) {
addError(builder.getSource(), ErrorMessages.PRELOAD_NOT_ALLOWED);
}
@@ -443,16 +440,17 @@
}
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
private <T> void createConstantBinding(ConstantBindingBuilder builder) {
if (builder.hasValue()) {
Key<T> key = (Key<T>) builder.getKey();
- InternalFactory<? extends T> factory =
- (InternalFactory<? extends T>) builder.getInternalFactory();
- Binding<?> binding =
- Binding.newInstance(container, key, builder.getSource(), factory);
+ InternalFactory<? extends T> factory
+ = (InternalFactory<? extends T>) builder.getInternalFactory();
+ Binding<?> binding
+ = Binding.newInstance(container, key, builder.getSource(), factory);
putBinding(binding);
- } else {
+ }
+ else {
addError(builder.getSource(), ErrorMessages.MISSING_CONSTANT_VALUE);
}
}
@@ -461,7 +459,8 @@
Key<?> key, InternalFactory<?> factory) {
if (factories.containsKey(key)) {
addError(source, ErrorMessages.BINDING_ALREADY_SET, key);
- } else {
+ }
+ else {
factories.put(key, factory);
}
}
@@ -473,18 +472,18 @@
if (bindings.containsKey(key)) {
addError(binding.getSource(), ErrorMessages.BINDING_ALREADY_SET, key,
original.getSource());
- } else {
+ }
+ else {
bindings.put(key, binding);
}
}
/**
- * Currently we only support creating one Container instance per builder.
- * If we want to support creating more than one container per builder,
- * we should move to a "factory factory" model where we create a factory
- * instance per Container. Right now, one factory instance would be
- * shared across all the containers, which means container-scoped objects
- * would be shared, etc.
+ * Currently we only support creating one Container instance per builder. If
+ * we want to support creating more than one container per builder, we should
+ * move to a "factory factory" model where we create a factory instance per
+ * Container. Right now, one factory instance would be shared across all the
+ * containers, which means container-scoped objects would be shared, etc.
*/
private void ensureNotCreated() {
if (container != null) {
@@ -528,15 +527,14 @@
if (!this.key.hasDefaultName()) {
addError(source, ErrorMessages.NAME_ALREADY_SET);
}
-
this.key = this.key.named(name);
return this;
}
/**
- * Binds to instances of the given implementation class. The {@link
- * Container} will inject the implementation instances as well. Sets the
- * scope based on the @{@link Scoped} annotation on the implementation
+ * Binds to instances of the given implementation class. The
+ * {@link Container} will inject the implementation instances as well. Sets
+ * the scope based on the @{@link Scoped} annotation on the implementation
* class if present.
*/
public <I extends T> BindingBuilder<T> to(Class<I> implementation) {
@@ -544,9 +542,9 @@
}
/**
- * Binds to instances of the given implementation type. The {@link
- * Container} will inject the implementation instances as well. Sets the
- * scope based on the @{@link Scoped} annotation on the implementation
+ * Binds to instances of the given implementation type. The
+ * {@link Container} will inject the implementation instances as well. Sets
+ * the scope based on the @{@link Scoped} annotation on the implementation
* class if present.
*/
public <I extends T> BindingBuilder<T> to(TypeLiteral<I> implementation) {
@@ -559,11 +557,12 @@
private void setScopeFromType(Class<?> implementation) {
for (Annotation annotation : implementation.getAnnotations()) {
- Class<? extends Annotation> annotationType =
- annotation.annotationType();
+ Class<? extends Annotation> annotationType
+ = annotation.annotationType();
if (annotationType == Scoped.class) {
in(((Scoped) annotation).value());
- } else {
+ }
+ else {
Scoped scoped = annotationType.getAnnotation(Scoped.class);
if (scoped != null) {
in(scoped.value());
@@ -622,8 +621,7 @@
}
/**
- * Specifies the scope. References the name passed to {@link
- * ContainerBuilder#scope(String, Scope)}.
+ * Specifies the scope. References the name passed to {@link #scope}.
*/
public BindingBuilder<T> in(String scopeName) {
ensureScopeNotSet();
@@ -662,9 +660,9 @@
}
/**
- * Instructs the builder to eagerly load this binding when it creates
- * the container. Useful for application initialization logic. Currently
- * only supported for container-scoped bindings.
+ * Instructs the builder to eagerly load this binding when it creates the
+ * container. Useful for application initialization logic. Currently only
+ * supported for container-scoped bindings.
*/
public BindingBuilder<T> preload() {
this.preload = true;
@@ -715,8 +713,8 @@
@SuppressWarnings("unchecked")
public T get(InternalContext context) {
if (constructor == null) {
- this.constructor =
- context.getContainerImpl().getConstructor(implementation);
+ this.constructor
+ = context.getContainerImpl().getConstructor(implementation);
}
return (T) constructor.construct(context, key.getRawType());
}
@@ -929,8 +927,7 @@
final Object source;
final Class<?>[] types;
- final List<ContainerImpl.Injector> injectors =
- new ArrayList<ContainerImpl.Injector>();
+ final List<Injector> injectors = new ArrayList<Injector>();
public StaticInjection(Object source, Class<?>[] types) {
this.source = source;
@@ -954,7 +951,7 @@
void runInjectors(ContainerImpl container) {
container.callInContext(new ContextualCallable<Void>() {
public Void call(InternalContext context) {
- for (ContainerImpl.Injector injector : injectors) {
+ for (Injector injector : injectors) {
injector.inject(context, null);
}
return null;
@@ -963,8 +960,7 @@
}
}
- static class BindingPreloader
- implements ContextualCallable<Void> {
+ static class BindingPreloader implements ContextualCallable<Void> {
private final Key<?> key;
private final InternalFactory<?> factory;
@@ -975,13 +971,14 @@
}
public Void call(InternalContext context) {
- ExternalContext<?> externalContext =
- ExternalContext.newInstance(null, key, context.getContainerImpl());
+ ExternalContext<?> externalContext
+ = ExternalContext.newInstance(null, key, context.getContainerImpl());
context.setExternalContext(externalContext);
try {
factory.get(context);
return null;
- } finally {
+ }
+ finally {
context.setExternalContext(null);
}
}
diff --git a/src/com/google/inject/ContainerCreationException.java b/src/com/google/inject/ContainerCreationException.java
index 647d7e3..9976a0f 100644
--- a/src/com/google/inject/ContainerCreationException.java
+++ b/src/com/google/inject/ContainerCreationException.java
@@ -17,15 +17,14 @@
package com.google.inject;
import com.google.inject.spi.Message;
-
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
/**
- * Thrown when errors occur while creating a {@link Container}. Includes a
- * list of encountered errors. Typically, a client should catch this exception,
- * log it, and stop execution.
+ * Thrown when errors occur while creating a {@link Container}. Includes a list
+ * of encountered errors. Typically, a client should catch this exception, log
+ * it, and stop execution.
*
* @author crazybob@google.com (Bob Lee)
*/
diff --git a/src/com/google/inject/ContainerImpl.java b/src/com/google/inject/ContainerImpl.java
index e67d0f0..432d924 100644
--- a/src/com/google/inject/ContainerImpl.java
+++ b/src/com/google/inject/ContainerImpl.java
@@ -16,13 +16,11 @@
package com.google.inject;
+import static com.google.inject.ConstantConversionException.createMessage;
import com.google.inject.util.GuiceFastClass;
import com.google.inject.util.ReferenceCache;
import com.google.inject.util.Strings;
import com.google.inject.util.ToStringBuilder;
-
-import net.sf.cglib.reflect.FastMethod;
-
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
@@ -41,12 +39,14 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import net.sf.cglib.reflect.FastClass;
+import net.sf.cglib.reflect.FastMethod;
/**
* Default {@link Container} implementation.
*
- * @see ContainerBuilder
* @author crazybob@google.com (Bob Lee)
+ * @see ContainerBuilder
*/
class ContainerImpl implements Container {
@@ -78,13 +78,13 @@
PRIMITIVE_COUNTERPARTS = Collections.unmodifiableMap(counterparts);
}
- private static final Map<Class<?>, Converter<?>> PRIMITIVE_CONVERTERS =
- new PrimitiveConverters();
+ private static final Map<Class<?>, Converter<?>> PRIMITIVE_CONVERTERS
+ = new PrimitiveConverters();
final ConstructionProxyFactory constructionProxyFactory;
final Map<Key<?>, Binding<?>> bindings;
- final Map<TypeLiteral<?>, List<Binding<?>>> bindingsByType =
- new HashMap<TypeLiteral<?>, List<Binding<?>>>();
+ final Map<TypeLiteral<?>, List<Binding<?>>> bindingsByType
+ = new HashMap<TypeLiteral<?>, List<Binding<?>>>();
ErrorHandler errorHandler = new InvalidErrorHandler();
@@ -116,7 +116,7 @@
public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) {
// IntelliJ doesn't understand that the below is "correct"
//noinspection RedundantCast,RawUseOfParameterizedType
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
List<Binding<T>> bindingsForThisType = (List) bindingsByType.get(type);
if (bindingsForThisType == null) {
return Collections.emptyList();
@@ -137,7 +137,8 @@
this.errorHandler = errorHandler;
try {
runnable.run();
- } finally {
+ }
+ finally {
this.errorHandler = previous;
}
}
@@ -161,19 +162,20 @@
if (!(factoryType instanceof ParameterizedType)) {
return null;
}
- Type entryType =
- ((ParameterizedType) factoryType).getActualTypeArguments()[0];
+ Type entryType
+ = ((ParameterizedType) factoryType).getActualTypeArguments()[0];
try {
- final Factory<?> factory =
- getFactory(Key.get(entryType, key.getName()));
+ final Factory<?> factory
+ = getFactory(Key.get(entryType, key.getName()));
return new InternalFactory<T>() {
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public T get(InternalContext context) {
return (T) factory;
}
};
- } catch (ConfigurationException e) {
+ }
+ catch (ConfigurationException e) {
ErrorMessages.handleMissingBinding(errorHandler, member, key,
getNamesOfBindingsTo(key.getType()));
return invalidFactory();
@@ -181,11 +183,11 @@
}
// Auto[un]box primitives.
- Class<?> primitiveCounterpart =
- PRIMITIVE_COUNTERPARTS.get(key.getType().getRawType());
+ Class<?> primitiveCounterpart
+ = PRIMITIVE_COUNTERPARTS.get(key.getType().getRawType());
if (primitiveCounterpart != null) {
- Binding<?> counterpartBinding =
- getBinding(Key.get(primitiveCounterpart, key.getName()));
+ Binding<?> counterpartBinding
+ = getBinding(Key.get(primitiveCounterpart, key.getName()));
if (counterpartBinding != null) {
return (InternalFactory<? extends T>)
counterpartBinding.getInternalFactory();
@@ -193,8 +195,8 @@
}
// Do we have a constant String factory of the same name?
- Binding<String> stringBinding =
- getBinding(Key.get(String.class, key.getName()));
+ Binding<String> stringBinding
+ = getBinding(Key.get(String.class, key.getName()));
if (stringBinding == null || !stringBinding.isConstant()) {
return null;
}
@@ -214,7 +216,8 @@
try {
T t = converter.convert(member, key, value);
return new ConstantFactory<T>(t);
- } catch (ConstantConversionException e) {
+ }
+ catch (ConstantConversionException e) {
errorHandler.handle(e);
return null;
}
@@ -222,13 +225,12 @@
// Do we need an enum?
if (Enum.class.isAssignableFrom(type)) {
- T t = null;
+ T t;
try {
t = (T) Enum.valueOf((Class) type, value);
- } catch (IllegalArgumentException e) {
- errorHandler.handle(
- ConstantConversionException.createMessage(
- value, key, member, e.toString()));
+ }
+ catch (IllegalArgumentException e) {
+ errorHandler.handle(createMessage(value, key, member, e.toString()));
return invalidFactory();
}
return new ConstantFactory<T>(t);
@@ -238,10 +240,9 @@
if (type == Class.class) {
try {
return new ConstantFactory<T>((T) Class.forName(value));
- } catch (ClassNotFoundException e) {
- errorHandler.handle(
- ConstantConversionException.createMessage(
- value, key, member, e.toString()));
+ }
+ catch (ClassNotFoundException e) {
+ errorHandler.handle(createMessage(value, key, member, e.toString()));
return invalidFactory();
}
}
@@ -258,19 +259,19 @@
/**
* Field and method injectors.
*/
- final Map<Class<?>, List<Injector>> injectors =
- new ReferenceCache<Class<?>, List<Injector>>() {
- protected List<Injector> create(Class<?> key) {
- if (key.isInterface()) {
- errorHandler.handle(ErrorMessages.CANNOT_INJECT_INTERFACE, key);
- return Collections.emptyList();
- }
+ final Map<Class<?>, List<Injector>> injectors
+ = new ReferenceCache<Class<?>, List<Injector>>() {
+ protected List<Injector> create(Class<?> key) {
+ if (key.isInterface()) {
+ errorHandler.handle(ErrorMessages.CANNOT_INJECT_INTERFACE, key);
+ return Collections.emptyList();
+ }
- List<Injector> injectors = new ArrayList<Injector>();
- addInjectors(key, injectors);
- return injectors;
- }
- };
+ List<Injector> injectors = new ArrayList<Injector>();
+ addInjectors(key, injectors);
+ return injectors;
+ }
+ };
/**
* Recursively adds injectors for fields and methods from the given class to
@@ -320,7 +321,8 @@
if (inject != null) {
try {
injectors.add(injectorFactory.create(this, member, inject.value()));
- } catch (MissingDependencyException e) {
+ }
+ catch (MissingDependencyException e) {
if (inject.required()) {
// TODO: Report errors for more than one parameter per member.
e.handle(errorHandler);
@@ -339,7 +341,7 @@
return Collections.unmodifiableMap(bindings);
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public <T> Binding<T> getBinding(Key<T> key) {
return (Binding<T>) bindings.get(key);
}
@@ -380,9 +382,11 @@
context.setExternalContext(externalContext);
try {
field.set(o, factory.get(context));
- } catch (IllegalAccessException e) {
+ }
+ catch (IllegalAccessException e) {
throw new AssertionError(e);
- } finally {
+ }
+ finally {
context.setExternalContext(previous);
}
}
@@ -396,8 +400,8 @@
* @param parameterTypes parameter types
* @return injections
*/
- <M extends AccessibleObject & Member> ParameterInjector<?>[]
- getParametersInjectors(M member,
+ <M extends AccessibleObject & Member>
+ ParameterInjector<?>[] getParametersInjectors(M member,
Annotation[][] annotations, Type[] parameterTypes, String defaultName)
throws MissingDependencyException {
boolean defaultNameOverridden = !defaultName.equals(Key.DEFAULT_NAME);
@@ -409,11 +413,10 @@
ErrorMessages.NAME_ON_MEMBER_WITH_MULTIPLE_PARAMS, member);
}
- List<ParameterInjector<?>> parameterInjectors =
- new ArrayList<ParameterInjector<?>>();
-
- Iterator<Annotation[]> annotationsIterator =
- Arrays.asList(annotations).iterator();
+ ParameterInjector<?>[] parameterInjectors
+ = new ParameterInjector<?>[parameterTypes.length];
+ Iterator<Annotation[]> annotationsIterator
+ = Arrays.asList(annotations).iterator();
int index = 0;
for (Type parameterType : parameterTypes) {
Inject annotation = findInject(annotationsIterator.next());
@@ -425,36 +428,31 @@
errorHandler.handle(
ErrorMessages.NAME_ON_MEMBER_AND_PARAMETER, member);
}
- } else {
+ }
+ else {
name = annotation == null ? defaultName : annotation.value();
}
Key<?> key = Key.get(parameterType, name);
- parameterInjectors.add(createParameterInjector(key, member, index++));
+ parameterInjectors[index] = createParameterInjector(key, member, index);
+ index++;
}
- return toArray(parameterInjectors);
+ return parameterInjectors;
}
- <T> ParameterInjector<T> createParameterInjector(
- Key<T> key, Member member, int index) throws MissingDependencyException {
+ <T> ParameterInjector<T> createParameterInjector(Key<T> key, Member member,
+ int index) throws MissingDependencyException {
InternalFactory<? extends T> factory = getFactory(member, key);
if (factory == null) {
throw new MissingDependencyException(key, member);
}
- ExternalContext<T> externalContext =
- ExternalContext.newInstance(member, index, key, this);
+ ExternalContext<T> externalContext
+ = ExternalContext.newInstance(member, index, key, this);
return new ParameterInjector<T>(externalContext, factory);
}
- @SuppressWarnings("unchecked")
- private ParameterInjector<?>[] toArray(
- List<ParameterInjector<?>> parameterInjections) {
- return parameterInjections.toArray(
- new ParameterInjector[parameterInjections.size()]);
- }
-
/**
* Finds the {@link Inject} annotation in an array of annotations.
*/
@@ -474,8 +472,8 @@
public MethodInjector(ContainerImpl container, Method method, String name)
throws MissingDependencyException {
- this.fastMethod =
- GuiceFastClass.create(method.getDeclaringClass()).getMethod(method);
+ FastClass fastClass = GuiceFastClass.create(method.getDeclaringClass());
+ this.fastMethod = fastClass.getMethod(method);
Type[] parameterTypes = method.getGenericParameterTypes();
parameterInjectors = parameterTypes.length > 0
? container.getParametersInjectors(
@@ -486,24 +484,25 @@
public void inject(InternalContext context, Object o) {
try {
fastMethod.invoke(o, getParameters(context, parameterInjectors));
- } catch (Exception e) {
+ }
+ catch (Exception e) {
throw new RuntimeException(e);
}
}
}
- final Map<Class<?>, ConstructorInjector> constructors =
- new ReferenceCache<Class<?>, ConstructorInjector>() {
- @SuppressWarnings("unchecked")
- protected ConstructorInjector<?> create(Class<?> implementation) {
- if (implementation.isInterface()) {
- errorHandler.handle(ErrorMessages.CANNOT_INJECT_INTERFACE,
- implementation);
- }
+ final Map<Class<?>, ConstructorInjector> constructors
+ = new ReferenceCache<Class<?>, ConstructorInjector>() {
+ @SuppressWarnings("unchecked")
+ protected ConstructorInjector<?> create(Class<?> implementation) {
+ if (implementation.isInterface()) {
+ errorHandler.handle(
+ ErrorMessages.CANNOT_INJECT_INTERFACE, implementation);
+ }
- return new ConstructorInjector(ContainerImpl.this, implementation);
- }
- };
+ return new ConstructorInjector(ContainerImpl.this, implementation);
+ }
+ };
/**
* A placeholder. This enables us to continue processing and gather more
@@ -515,11 +514,12 @@
}
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
static <T> Constructor<T> invalidConstructor() {
try {
return (Constructor<T>) InvalidConstructor.class.getDeclaredConstructor();
- } catch (NoSuchMethodException e) {
+ }
+ catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
@@ -540,7 +540,8 @@
context.setExternalContext(externalContext);
try {
return factory.get(context);
- } finally {
+ }
+ finally {
context.setExternalContext(previous);
}
}
@@ -623,8 +624,8 @@
final InternalFactory<? extends T> factory = getFactory(null, key);
if (factory == null) {
- throw new ConfigurationException("Missing binding to " +
- AbstractErrorHandler.convert(key) + ".");
+ throw new ConfigurationException(
+ "Missing binding to " + AbstractErrorHandler.convert(key) + ".");
}
return new Factory<T>() {
@@ -636,7 +637,8 @@
ExternalContext.newInstance(null, key, ContainerImpl.this));
try {
return factory.get(context);
- } finally {
+ }
+ finally {
context.setExternalContext(previous);
}
}
@@ -645,12 +647,12 @@
};
}
- final ThreadLocal<InternalContext[]> localContext =
- new ThreadLocal<InternalContext[]>() {
- protected InternalContext[] initialValue() {
- return new InternalContext[1];
- }
- };
+ final ThreadLocal<InternalContext[]> localContext
+ = new ThreadLocal<InternalContext[]>() {
+ protected InternalContext[] initialValue() {
+ return new InternalContext[1];
+ }
+ };
/**
* Looks up thread local context. Creates (and removes) a new context if
@@ -662,11 +664,13 @@
reference[0] = new InternalContext(this);
try {
return callable.call(reference[0]);
- } finally {
+ }
+ finally {
// Only remove the context if this call created it.
reference[0] = null;
}
- } else {
+ }
+ else {
// Someone else will clean up this context.
return callable.call(reference[0]);
}
@@ -741,17 +745,19 @@
<T> void putParser(final Class<T> primitive) {
try {
Class<?> wrapper = PRIMITIVE_COUNTERPARTS.get(primitive);
- final Method parser = wrapper.getMethod("parse" +
- Strings.capitalize(primitive.getName()), String.class);
+ final Method parser = wrapper.getMethod(
+ "parse" + Strings.capitalize(primitive.getName()), String.class);
Converter<T> converter = new Converter<T>() {
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public T convert(Member member, Key<T> key, String value)
throws ConstantConversionException {
try {
return (T) parser.invoke(null, value);
- } catch (IllegalAccessException e) {
+ }
+ catch (IllegalAccessException e) {
throw new AssertionError(e);
- } catch (InvocationTargetException e) {
+ }
+ catch (InvocationTargetException e) {
throw new ConstantConversionException(member, key, value,
e.getTargetException());
}
@@ -759,7 +765,8 @@
};
put(wrapper, converter);
put(primitive, converter);
- } catch (NoSuchMethodException e) {
+ }
+ catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
@@ -777,14 +784,14 @@
throws ConstantConversionException;
}
- private static final InternalFactory<?> INVALID_FACTORY =
- new InternalFactory<Object>() {
- public Object get(InternalContext context) {
- throw new AssertionError();
- }
- };
+ private static final InternalFactory<?> INVALID_FACTORY
+ = new InternalFactory<Object>() {
+ public Object get(InternalContext context) {
+ throw new AssertionError();
+ }
+ };
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
static <T> InternalFactory<T> invalidFactory() {
return (InternalFactory<T>) INVALID_FACTORY;
}
diff --git a/src/com/google/inject/ContainerScoped.java b/src/com/google/inject/ContainerScoped.java
index 7d1408e..8a6175d 100644
--- a/src/com/google/inject/ContainerScoped.java
+++ b/src/com/google/inject/ContainerScoped.java
@@ -16,10 +16,10 @@
package com.google.inject;
-import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
/**
* Apply this to implementation classes when you want one instance per
@@ -27,7 +27,7 @@
*
* @author crazybob@google.com (Bob Lee)
*/
-@Target(ElementType.TYPE)
-@Retention(RUNTIME)
-@Scoped(Scopes.CONTAINER_NAME)
-public @interface ContainerScoped {}
+@Target(ElementType.TYPE) @Retention(RUNTIME) @Scoped(Scopes.CONTAINER_NAME)
+public @interface ContainerScoped {
+
+}
diff --git a/src/com/google/inject/Context.java b/src/com/google/inject/Context.java
index 536ea50..52265cf 100644
--- a/src/com/google/inject/Context.java
+++ b/src/com/google/inject/Context.java
@@ -38,8 +38,8 @@
Member getMember();
/**
- * Gets the index of the parameter which is being injected. Returns
- * {@code -1} if a parameter isn't being injected.
+ * Gets the index of the parameter which is being injected. Returns {@code -1}
+ * if a parameter isn't being injected.
*/
int getParameterIndex();
diff --git a/src/com/google/inject/DefaultConstructionProxyFactory.java b/src/com/google/inject/DefaultConstructionProxyFactory.java
index 02c8266..d414a53 100644
--- a/src/com/google/inject/DefaultConstructionProxyFactory.java
+++ b/src/com/google/inject/DefaultConstructionProxyFactory.java
@@ -17,12 +17,10 @@
package com.google.inject;
import com.google.inject.util.GuiceFastClass;
-
-import net.sf.cglib.reflect.FastClass;
-import net.sf.cglib.reflect.FastConstructor;
-
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import net.sf.cglib.reflect.FastClass;
+import net.sf.cglib.reflect.FastConstructor;
/**
* Default {@link ConstructionProxyFactory} implementation. Simply invokes the
@@ -31,16 +29,15 @@
*
* @author crazybob@google.com (Bob Lee)
*/
-class DefaultConstructionProxyFactory
- implements ConstructionProxyFactory {
+class DefaultConstructionProxyFactory implements ConstructionProxyFactory {
public <T> ConstructionProxy<T> get(Constructor<T> constructor) {
- FastClass fastClass =
- GuiceFastClass.create(constructor.getDeclaringClass());
- final FastConstructor fastConstructor =
- fastClass.getConstructor(constructor);
+ Class<T> classToConstruct = constructor.getDeclaringClass();
+ FastClass fastClass = GuiceFastClass.create(classToConstruct);
+ final FastConstructor fastConstructor
+ = fastClass.getConstructor(constructor);
return new ConstructionProxy<T>() {
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public T newInstance(Object... arguments)
throws InvocationTargetException {
return (T) fastConstructor.newInstance(arguments);
diff --git a/src/com/google/inject/ErrorMessages.java b/src/com/google/inject/ErrorMessages.java
index fe5ef5f..904fe77 100644
--- a/src/com/google/inject/ErrorMessages.java
+++ b/src/com/google/inject/ErrorMessages.java
@@ -38,9 +38,10 @@
Key<?> key, List<String> otherNames) {
if (otherNames.isEmpty()) {
errorHandler.handle(MISSING_BINDING, key, member);
- } else {
- errorHandler.handle(MISSING_BINDING_BUT_OTHERS_EXIST,
- key, member, otherNames);
+ }
+ else {
+ errorHandler.handle(
+ MISSING_BINDING_BUT_OTHERS_EXIST, key, member, otherNames);
}
}
@@ -54,8 +55,7 @@
static final String TOO_MANY_CONSTRUCTORS = "More than one constructor"
+ " annotated with @Inject found in %s. " + CONSTRUCTOR_RULES;
- static final String DUPLICATE_SCOPES =
- "A scope named '%s' already exists.";
+ static final String DUPLICATE_SCOPES = "A scope named '%s' already exists.";
static final String MISSING_CONSTANT_VALUE = "Missing constant value. Please"
+ " call to(...).";
@@ -70,8 +70,8 @@
static final String NAME_ALREADY_SET = "Binding name is set more than once.";
- static final String IMPLEMENTATION_ALREADY_SET =
- "Implementation is set more than once.";
+ static final String IMPLEMENTATION_ALREADY_SET = "Implementation is set more"
+ + " than once.";
static final String SCOPE_NOT_FOUND = "Scope named '%s' not found."
+ " Available scope names: %s";
@@ -82,8 +82,8 @@
+ " implementation with with an annotation which is annotated with"
+ " @Scoped.";
- static final String CONSTANT_VALUE_ALREADY_SET =
- "Constant value is set more than once.";
+ static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is set more"
+ + " than once.";
static final String LINK_DESTINATION_ALREADY_SET = "Link destination is"
+ " set more than once.";
diff --git a/src/com/google/inject/ExternalContext.java b/src/com/google/inject/ExternalContext.java
index abea51d..e299652 100644
--- a/src/com/google/inject/ExternalContext.java
+++ b/src/com/google/inject/ExternalContext.java
@@ -20,8 +20,8 @@
import java.util.LinkedHashMap;
/**
- * An immutable snapshot of the current context which is safe to
- * expose to client code.
+ * An immutable snapshot of the current context which is safe to expose to
+ * client code.
*
* @author crazybob@google.com (Bob Lee)
*/
@@ -64,8 +64,8 @@
}}.toString();
}
- static <T> ExternalContext<T> newInstance(Member member,
- Key<T> key, ContainerImpl container) {
+ static <T> ExternalContext<T> newInstance(
+ Member member, Key<T> key, ContainerImpl container) {
return new ExternalContext<T>(member, -1, key, container);
}
diff --git a/src/com/google/inject/FactoryToInternalFactoryAdapter.java b/src/com/google/inject/FactoryToInternalFactoryAdapter.java
index ec975f8..6f4149c 100644
--- a/src/com/google/inject/FactoryToInternalFactoryAdapter.java
+++ b/src/com/google/inject/FactoryToInternalFactoryAdapter.java
@@ -18,7 +18,7 @@
/**
* @author crazybob@google.com (Bob Lee)
-*/
+ */
class FactoryToInternalFactoryAdapter<T> implements Factory<T> {
private final ContainerImpl container;
@@ -32,8 +32,7 @@
}
public T get() {
- return container.callInContext(
- new ContextualCallable<T>() {
+ return container.callInContext(new ContextualCallable<T>() {
public T call(InternalContext context) {
return internalFactory.get(context);
}
diff --git a/src/com/google/inject/Inject.java b/src/com/google/inject/Inject.java
index ea04dbf..087d6ca 100644
--- a/src/com/google/inject/Inject.java
+++ b/src/com/google/inject/Inject.java
@@ -16,9 +16,12 @@
package com.google.inject;
-import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
import java.lang.annotation.Retention;
-import static java.lang.annotation.RetentionPolicy.*;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
/**
@@ -27,7 +30,7 @@
*
* @author crazybob@google.com (Bob Lee)
*/
-@Target({METHOD, CONSTRUCTOR, FIELD, PARAMETER})
+@Target({ METHOD, CONSTRUCTOR, FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface Inject {
@@ -37,8 +40,8 @@
String value() default Key.DEFAULT_NAME;
/**
- * Whether or not injection is required. Applicable only to methods and
- * fields (not constructors or parameters).
+ * Whether or not injection is required. Applicable only to methods and fields
+ * (not constructors or parameters).
*/
boolean required() default true;
}
diff --git a/src/com/google/inject/InterceptorStackCallback.java b/src/com/google/inject/InterceptorStackCallback.java
index 015a045..ad74e69 100644
--- a/src/com/google/inject/InterceptorStackCallback.java
+++ b/src/com/google/inject/InterceptorStackCallback.java
@@ -16,15 +16,13 @@
package com.google.inject;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Method;
+import java.util.List;
import net.sf.cglib.proxy.MethodProxy;
-
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
-import java.lang.reflect.Method;
-import java.lang.reflect.AccessibleObject;
-import java.util.List;
-
/**
* Intercepts a method with a stack of interceptors.
*
@@ -38,14 +36,14 @@
public InterceptorStackCallback(Method method,
List<MethodInterceptor> interceptors) {
this.method = method;
- this.interceptors = interceptors.toArray(
- new MethodInterceptor[interceptors.size()]);
+ this.interceptors
+ = interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
public Object intercept(Object proxy, Method method, Object[] arguments,
MethodProxy methodProxy) throws Throwable {
- return new InterceptedMethodInvocation(
- proxy, methodProxy, arguments).proceed();
+ return new InterceptedMethodInvocation(proxy, methodProxy, arguments)
+ .proceed();
}
class InterceptedMethodInvocation implements MethodInvocation {
@@ -66,9 +64,10 @@
try {
index++;
return index == interceptors.length
- ? methodProxy.invokeSuper(proxy, arguments)
- : interceptors[index].invoke(this);
- } finally {
+ ? methodProxy.invokeSuper(proxy, arguments)
+ : interceptors[index].invoke(this);
+ }
+ finally {
index--;
}
}
diff --git a/src/com/google/inject/InternalContext.java b/src/com/google/inject/InternalContext.java
index e8184f9..4b35c39 100644
--- a/src/com/google/inject/InternalContext.java
+++ b/src/com/google/inject/InternalContext.java
@@ -16,8 +16,8 @@
package com.google.inject;
-import java.util.Map;
import java.util.HashMap;
+import java.util.Map;
/**
* Internal context. Used to coordinate injections and support circular
@@ -50,9 +50,10 @@
ConstructionContext<T> constructionContext = new ConstructionContext<T>();
constructionContexts.put(key, constructionContext);
return constructionContext;
- } else {
- ConstructionContext<T> constructionContext =
- (ConstructionContext<T>) constructionContexts.get(key);
+ }
+ else {
+ ConstructionContext<T> constructionContext
+ = (ConstructionContext<T>) constructionContexts.get(key);
if (constructionContext == null) {
constructionContext = new ConstructionContext<T>();
constructionContexts.put(key, constructionContext);
diff --git a/src/com/google/inject/InternalToContextualFactoryAdapter.java b/src/com/google/inject/InternalToContextualFactoryAdapter.java
index 1f916d3..6256829 100644
--- a/src/com/google/inject/InternalToContextualFactoryAdapter.java
+++ b/src/com/google/inject/InternalToContextualFactoryAdapter.java
@@ -19,8 +19,7 @@
/**
* @author crazybob@google.com (Bob Lee)
*/
-class InternalToContextualFactoryAdapter<T>
- implements InternalFactory<T> {
+class InternalToContextualFactoryAdapter<T> implements InternalFactory<T> {
private final ContextualFactory<? extends T> factory;
diff --git a/src/com/google/inject/Key.java b/src/com/google/inject/Key.java
index 3902a63..bc40e75 100644
--- a/src/com/google/inject/Key.java
+++ b/src/com/google/inject/Key.java
@@ -17,17 +17,16 @@
package com.google.inject;
import static com.google.inject.util.Objects.nonNull;
-
import java.lang.reflect.Type;
/**
- * Binding key consisting of a type and a name. Matches the type and
- * name ({@link Inject#value()}) at a point of injection.
+ * Binding key consisting of a type and a name. Matches the type and name
+ * ({@link Inject#value()}) at a point of injection.
*
- * <p>For example, <tt>new Key<List<String>>("cities") {}</tt> will match:
+ * <p>For example, {@code new Key<List<String>>("cities") {}} will match:
*
* <pre>
- * @Inject("cities")
+ * {@literal @}Inject("cities")
* public void setList(List<String> cities) {
* ...
* }
@@ -50,17 +49,17 @@
* Constructs a new key. Derives the type from this class's type parameter.
*
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
- * parameter in the anonymous class's type hierarchy so we can reconstitute
- * it at runtime despite erasure.
+ * parameter in the anonymous class's type hierarchy so we can reconstitute it
+ * at runtime despite erasure.
*
* <p>Example usage for a binding of type {@code Foo} named "bar":
* {@code new Key<Foo>("bar") {}}.
*/
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
protected Key(String name) {
this.name = nonNull(name, "name");
- this.typeLiteral =
- (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass());
+ this.typeLiteral
+ = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass());
this.hashCode = computeHashCode();
}
@@ -75,16 +74,14 @@
/**
* Unsafe. Constructs a key from a manually specified type.
*/
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
private Key(Type type, String name) {
this.name = nonNull(name, "name");
this.typeLiteral = (TypeLiteral<T>) TypeLiteral.get(type);
this.hashCode = computeHashCode();
}
- /**
- * Constructs a key from a manually specified type.
- */
+ /** Constructs a key from a manually specified type. */
private Key(TypeLiteral<T> typeLiteral, String name) {
this.name = nonNull(name, "name");
this.typeLiteral = typeLiteral;
@@ -119,7 +116,7 @@
/**
* Gets the binding name.
*/
- public String getName() {
+ public String getName() {
return name;
}
diff --git a/src/com/google/inject/MethodAspect.java b/src/com/google/inject/MethodAspect.java
index 2d71add..19bcd31 100644
--- a/src/com/google/inject/MethodAspect.java
+++ b/src/com/google/inject/MethodAspect.java
@@ -16,14 +16,12 @@
package com.google.inject;
-import com.google.inject.util.Objects;
import com.google.inject.query.Query;
-
-import org.aopalliance.intercept.MethodInterceptor;
-
+import com.google.inject.util.Objects;
import java.lang.reflect.Method;
-import java.util.List;
import java.util.Arrays;
+import java.util.List;
+import org.aopalliance.intercept.MethodInterceptor;
/**
* Ties a query to a method interceptor.
@@ -36,12 +34,12 @@
final Query<? super Method> methodQuery;
final List<MethodInterceptor> interceptors;
- MethodAspect(Query<? super Class<?>> classQuery, Query<? super Method> methodQuery,
- MethodInterceptor... interceptors) {
+ MethodAspect(Query<? super Class<?>> classQuery,
+ Query<? super Method> methodQuery, MethodInterceptor... interceptors) {
this.classQuery = Objects.nonNull(classQuery, "class query");
this.methodQuery = Objects.nonNull(methodQuery, "method query");
- this.interceptors =
- Arrays.asList(Objects.nonNull(interceptors, "interceptors"));
+ this.interceptors
+ = Arrays.asList(Objects.nonNull(interceptors, "interceptors"));
}
boolean matches(Class<?> clazz) {
diff --git a/src/com/google/inject/ProxyFactory.java b/src/com/google/inject/ProxyFactory.java
index 8ec4f9a..55eb5e1 100644
--- a/src/com/google/inject/ProxyFactory.java
+++ b/src/com/google/inject/ProxyFactory.java
@@ -19,16 +19,6 @@
import com.google.inject.util.GuiceFastClass;
import com.google.inject.util.GuiceNamingPolicy;
import com.google.inject.util.ReferenceCache;
-
-import net.sf.cglib.proxy.Callback;
-import net.sf.cglib.proxy.CallbackFilter;
-import net.sf.cglib.proxy.Enhancer;
-import net.sf.cglib.proxy.NoOp;
-import net.sf.cglib.reflect.FastClass;
-import net.sf.cglib.reflect.FastConstructor;
-
-import org.aopalliance.intercept.MethodInterceptor;
-
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -36,6 +26,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import net.sf.cglib.proxy.Callback;
+import net.sf.cglib.proxy.CallbackFilter;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.NoOp;
+import net.sf.cglib.reflect.FastClass;
+import net.sf.cglib.reflect.FastConstructor;
+import org.aopalliance.intercept.MethodInterceptor;
/**
* Proxies classes applying interceptors to methods as specified in
@@ -46,15 +43,15 @@
class ProxyFactory implements ConstructionProxyFactory {
final List<MethodAspect> methodAspects;
- final ConstructionProxyFactory defaultFactory =
- new DefaultConstructionProxyFactory();
+ final ConstructionProxyFactory defaultFactory
+ = new DefaultConstructionProxyFactory();
ProxyFactory(List<MethodAspect> methodAspects) {
this.methodAspects = methodAspects;
}
- Map<Constructor<?>, ConstructionProxy<?>> constructionProxies =
- new ReferenceCache<Constructor<?>, ConstructionProxy<?>>() {
+ Map<Constructor<?>, ConstructionProxy<?>> constructionProxies
+ = new ReferenceCache<Constructor<?>, ConstructionProxy<?>>() {
protected ConstructionProxy<?> create(Constructor<?> constructor) {
return createConstructionProxy(constructor);
}
@@ -62,17 +59,18 @@
/**
* Gets a factory for the given type. Uses the zero-arg constructor. Wraps
- * exceptions in {@code RuntimeException} including
- * {@code InvocationTargetException}.
+ * exceptions in {@link RuntimeException} including
+ * {@link InvocationTargetException}.
*/
public <T> Factory<T> getFactory(Class<T> type) throws NoSuchMethodException {
- final ConstructionProxy<T> constructionProxy =
- createConstructionProxy(type.getDeclaredConstructor());
+ final ConstructionProxy<T> constructionProxy
+ = createConstructionProxy(type.getDeclaredConstructor());
return new Factory<T>() {
public T get() {
try {
return constructionProxy.newInstance();
- } catch (InvocationTargetException e) {
+ }
+ catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
@@ -99,22 +97,20 @@
final Map<Method, Integer> indices = new HashMap<Method, Integer>();
// Create method/interceptor holders and record indices.
- List<MethodInterceptorsPair> methodInterceptorsPairs =
- new ArrayList<MethodInterceptorsPair>();
+ List<MethodInterceptorsPair> methodInterceptorsPairs
+ = new ArrayList<MethodInterceptorsPair>();
for (int i = 0; i < methods.size(); i++) {
Method method = methods.get(i);
methodInterceptorsPairs.add(new MethodInterceptorsPair(method));
indices.put(method, i);
}
- // Iterate over aspects and add interceptors for the methods they apply
- // to.
+ // Iterate over aspects and add interceptors for the methods they apply to
boolean anyMatched = false;
for (MethodAspect methodAspect : applicableAspects) {
- for (MethodInterceptorsPair methodInterceptorsPair
- : methodInterceptorsPairs) {
- if (methodAspect.matches(methodInterceptorsPair.method)) {
- methodInterceptorsPair.addAll(methodAspect.interceptors());
+ for (MethodInterceptorsPair pair : methodInterceptorsPairs) {
+ if (methodAspect.matches(pair.method)) {
+ pair.addAll(methodAspect.interceptors());
anyMatched = true;
}
}
@@ -128,14 +124,14 @@
// noinspection unchecked
Class<? extends Callback>[] callbackTypes = new Class[methods.size()];
for (int i = 0; i < methods.size(); i++) {
- MethodInterceptorsPair methodInterceptorsPair =
- methodInterceptorsPairs.get(i);
- if (!methodInterceptorsPair.hasInterceptors()) {
+ MethodInterceptorsPair pair = methodInterceptorsPairs.get(i);
+ if (!pair.hasInterceptors()) {
callbacks[i] = NoOp.INSTANCE;
callbackTypes[i] = NoOp.class;
- } else {
+ }
+ else {
callbacks[i] = new InterceptorStackCallback(
- methodInterceptorsPair.method, methodInterceptorsPair.interceptors);
+ pair.method, pair.interceptors);
callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
}
}
@@ -167,10 +163,10 @@
<T> ConstructionProxy<T> createConstructionProxy(Class<?> clazz,
Class[] parameterTypes) {
FastClass fastClass = GuiceFastClass.create(clazz);
- final FastConstructor fastConstructor =
- fastClass.getConstructor(parameterTypes);
+ final FastConstructor fastConstructor
+ = fastClass.getConstructor(parameterTypes);
return new ConstructionProxy<T>() {
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public T newInstance(Object... arguments)
throws InvocationTargetException {
return (T) fastConstructor.newInstance(arguments);
@@ -199,7 +195,7 @@
}
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
public <T> ConstructionProxy<T> get(Constructor<T> constructor) {
return (ConstructionProxy<T>) constructionProxies.get(constructor);
}
diff --git a/src/com/google/inject/ProxyFactoryBuilder.java b/src/com/google/inject/ProxyFactoryBuilder.java
index 597b047..99810e1 100644
--- a/src/com/google/inject/ProxyFactoryBuilder.java
+++ b/src/com/google/inject/ProxyFactoryBuilder.java
@@ -17,12 +17,10 @@
package com.google.inject;
import com.google.inject.query.Query;
-
-import org.aopalliance.intercept.MethodInterceptor;
-
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
+import org.aopalliance.intercept.MethodInterceptor;
/**
* Creates a {@link ProxyFactory}.
@@ -38,9 +36,9 @@
* and method queries.
*
* @param classQuery matches classes the interceptor should apply to. For
- * example: {@code only(Runnable.class)}.
+ * example: {@code only(Runnable.class)}.
* @param methodQuery matches methods the interceptor should apply to. For
- * example: {@code annotatedWith(Transactional.class)}.
+ * example: {@code annotatedWith(Transactional.class)}.
* @param interceptors to apply
*/
public ProxyFactoryBuilder intercept(Query<? super Class<?>> classQuery,
diff --git a/src/com/google/inject/Scope.java b/src/com/google/inject/Scope.java
index bd94191..9b62f81 100644
--- a/src/com/google/inject/Scope.java
+++ b/src/com/google/inject/Scope.java
@@ -17,26 +17,25 @@
package com.google.inject;
/**
- * A scope which bound objects can reside in. Add a new scope using {@link
- * com.google.inject.ContainerBuilder#scope(String, Scope)} and reference it from
- * bindings using its name.
+ * A scope which bound objects can reside in. Add a new scope using
+ * {@link ContainerBuilder#scope} and reference it from bindings using its name.
*
- * <p>Scope implementations should override {@code toString()} and include the
- * creator's {@code toString()} output. Doing so aids debugging.
+ * <p>Scope implementations should override {@link #toString} and include the
+ * creator's {#toString} output. Doing so aids debugging.
*
* @author crazybob@google.com (Bob Lee)
*/
public interface Scope {
/**
- * Scopes a factory. The returned factory returns objects from this scope.
- * If an object does not exist in this scope, the factory can use the given
+ * Scopes a factory. The returned factory returns objects from this scope. If
+ * an object does not exist in this scope, the factory can use the given
* creator to create one.
*
* @param key binding key
* @param creator creates new instances as needed
* @return a new factory which only delegates to the given factory when an
- * instance of the requested object doesn't already exist in the scope
+ * instance of the requested object doesn't already exist in the scope
*/
public <T> Factory<T> scope(Key<T> key, Factory<T> creator);
}
diff --git a/src/com/google/inject/Scoped.java b/src/com/google/inject/Scoped.java
index c3dae5e..b6da7a3 100644
--- a/src/com/google/inject/Scoped.java
+++ b/src/com/google/inject/Scoped.java
@@ -25,13 +25,12 @@
* Annotates an implementation class with the name of its scope.
*
* <p>If you apply {@code Scoped} to another annotation, that annotation will
- * act as an alias and the user will not have to explicitly specify a name.
- * See {@link com.google.inject.ContainerScoped} for an example.
+ * act as an alias and the user will not have to explicitly specify a name. See
+ * {@link ContainerScoped} for an example.
*
- * @see com.google.inject.ContainerBuilder#scope(String, Scope)
* @author crazybob
*/
-@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
+@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RUNTIME)
public @interface Scoped {
diff --git a/src/com/google/inject/Scopes.java b/src/com/google/inject/Scopes.java
index b0b634c..86a6ca3 100644
--- a/src/com/google/inject/Scopes.java
+++ b/src/com/google/inject/Scopes.java
@@ -23,7 +23,8 @@
*/
public class Scopes {
- private Scopes() {}
+ private Scopes() {
+ }
/**
* Name of the default scope.
@@ -53,14 +54,16 @@
private volatile T instance;
- // DCL is safe as of Java 5, which we obviously require
+ // DCL on a volatile is safe as of Java 5, which we obviously require
@SuppressWarnings("DoubleCheckedLocking")
public T get() {
if (instance == null) {
- // Use a pretty coarse lock. We don't want to run into deadlocks
- // when two threads try to load circularly-dependent objects.
- // Maybe one of these days we will identify independent graphs of
- // objects and offer to load them in parallel.
+ /*
+ * Use a pretty coarse lock. We don't want to run into deadlocks
+ * when two threads try to load circularly-dependent objects.
+ * Maybe one of these days we will identify independent graphs of
+ * objects and offer to load them in parallel.
+ */
synchronized (Container.class) {
if (instance == null) {
instance = creator.get();
@@ -76,4 +79,4 @@
};
}
};
-}
\ No newline at end of file
+}
diff --git a/src/com/google/inject/TypeLiteral.java b/src/com/google/inject/TypeLiteral.java
index 391c256..3ae040f 100644
--- a/src/com/google/inject/TypeLiteral.java
+++ b/src/com/google/inject/TypeLiteral.java
@@ -17,26 +17,23 @@
package com.google.inject;
import static com.google.inject.util.Objects.nonNull;
-
-import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
/**
* Represents a generic type {@code T}. Java doesn't yet provide a way to
* represent generic types, so this class does. Forces clients to create a
- * subclass of this class which enables retrieval the type information
- * even at runtime.
+ * subclass of this class which enables retrieval the type information even at
+ * runtime.
*
- * <p>For example, to create a {@code TypeLiteral} for {@code List<String>},
- * you can create an empty anonymous inner class:
+ * <p>For example, to create a type literal for {@code List<String>}, you can
+ * create an empty anonymous inner class:
*
- * <pre>
- * TypeLiteral<List<String>> listOfString =
- * new TypeLiteral<List<String>>() {};
- * </pre>
+ * <p>
+ * {@code TypeLiteral<List<String>> list = new TypeLiteral<List<String>>() {};}
*
- * <p>Assumes {@code Type} implements {@code equals()} and {@code hashCode()}
- * as a value (as opposed to identity) comparison.
+ * <p>Assumes that type {@code T} implements {@link Object#equals} and
+ * {@link Object#hashCode()} as value (as opposed to identity) comparison.
*
* @author crazybob@google.com (Bob Lee)
*/
@@ -50,13 +47,10 @@
* parameter.
*
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
- * parameter in the anonymous class's type hierarchy so we can reconstitute
- * it at runtime despite erasure.
- *
- * <p>For example: {@code TypeLiteral<List<String>> t = new
- * TypeLiteral<List<String>>() {};}
+ * parameter in the anonymous class's type hierarchy so we can reconstitute it
+ * at runtime despite erasure.
*/
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
protected TypeLiteral() {
this.type = getSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) getRawType(type);
@@ -65,7 +59,7 @@
/**
* Unsafe. Constructs a type literal manually.
*/
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings("unchecked")
private TypeLiteral(Type type) {
this.rawType = (Class<? super T>) getRawType(nonNull(type, "type"));
this.type = type;
@@ -89,12 +83,13 @@
return new SimpleTypeLiteral<Object>(getSuperclassTypeParameter(subclass));
}
- @SuppressWarnings({"unchecked"})
+ @SuppressWarnings({ "unchecked" })
private static Class<?> getRawType(Type type) {
if (type instanceof Class<?>) {
// type is a normal class.
return (Class<?>) type;
- } else {
+ }
+ else {
// type is a parameterized type.
if (!(type instanceof ParameterizedType)) {
unexpectedType(type, ParameterizedType.class);
@@ -143,8 +138,8 @@
public String toString() {
return type instanceof Class<?>
- ? ((Class<?>) type).getName()
- : type.toString();
+ ? ((Class<?>) type).getName()
+ : type.toString();
}
static void unexpectedType(Type type, Class<?> expected) {