diff options
author | sberlin <sberlin@d779f126-a31b-0410-b53b-1d3aecad763e> | 2011-06-28 23:50:03 +0000 |
---|---|---|
committer | sberlin <sberlin@d779f126-a31b-0410-b53b-1d3aecad763e> | 2011-06-28 23:50:03 +0000 |
commit | 4e11457f34addf5d10fe0c31cefd54c75c37b540 (patch) | |
tree | 66e9940474e7a84c93fc16de6ae8cddea9bc8615 /core/src/com/google | |
parent | 7855aca14b1130e04b695f85c08e7b20867129f7 (diff) | |
download | guice-4e11457f34addf5d10fe0c31cefd54c75c37b540.tar.gz |
fixup whitespace
git-svn-id: https://google-guice.googlecode.com/svn/trunk@1563 d779f126-a31b-0410-b53b-1d3aecad763e
Diffstat (limited to 'core/src/com/google')
15 files changed, 1083 insertions, 1083 deletions
diff --git a/core/src/com/google/inject/internal/AbstractBindingProcessor.java b/core/src/com/google/inject/internal/AbstractBindingProcessor.java index ae4b8483..7feab8aa 100644 --- a/core/src/com/google/inject/internal/AbstractBindingProcessor.java +++ b/core/src/com/google/inject/internal/AbstractBindingProcessor.java @@ -1,167 +1,167 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import java.util.Set;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Binding;
-import com.google.inject.Injector;
-import com.google.inject.Key;
-import com.google.inject.MembersInjector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import com.google.inject.Scope;
-import com.google.inject.TypeLiteral;
-import com.google.common.collect.ImmutableSet;
-import com.google.inject.spi.DefaultBindingTargetVisitor;
-
-/**
- * Guarantees that processing of Binding elements happens in a sane way.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-abstract class AbstractBindingProcessor extends AbstractProcessor {
-
- // It's unfortunate that we have to maintain a blacklist of specific
- // classes, but we can't easily block the whole package because of
- // all our unit tests.
- private static final Set<Class<?>> FORBIDDEN_TYPES = ImmutableSet.<Class<?>>of(
- AbstractModule.class,
- Binder.class,
- Binding.class,
- Injector.class,
- Key.class,
- MembersInjector.class,
- Module.class,
- Provider.class,
- Scope.class,
- TypeLiteral.class);
- // TODO(jessewilson): fix BuiltInModule, then add Stage
-
- protected final ProcessedBindingData bindingData;
-
- AbstractBindingProcessor(Errors errors, ProcessedBindingData bindingData) {
- super(errors);
- this.bindingData = bindingData;
- }
-
- protected <T> UntargettedBindingImpl<T> invalidBinding(
- InjectorImpl injector, Key<T> key, Object source) {
- return new UntargettedBindingImpl<T>(injector, key, source);
- }
-
- protected void putBinding(BindingImpl<?> binding) {
- Key<?> key = binding.getKey();
-
- Class<?> rawType = key.getTypeLiteral().getRawType();
- if (FORBIDDEN_TYPES.contains(rawType)) {
- errors.cannotBindToGuiceType(rawType.getSimpleName());
- return;
- }
-
- BindingImpl<?> original = injector.getExistingBinding(key);
- if (original != null) {
- // If it failed because of an explicit duplicate binding...
- if (injector.state.getExplicitBinding(key) != null) {
- try {
- if(!isOkayDuplicate(original, binding, injector.state)) {
- errors.bindingAlreadySet(key, original.getSource());
- return;
- }
- } catch(Throwable t) {
- errors.errorCheckingDuplicateBinding(key, original.getSource(), t);
- return;
- }
- } else {
- // Otherwise, it failed because of a duplicate JIT binding
- // in the parent
- errors.jitBindingAlreadySet(key);
- return;
- }
- }
-
- // prevent the parent from creating a JIT binding for this key
- injector.state.parent().blacklist(key, binding.getSource());
- injector.state.putBinding(key, binding);
- }
-
- /**
- * We tolerate duplicate bindings if one exposes the other or if the two bindings
- * are considered duplicates (see {@link Bindings#areDuplicates(BindingImpl, BindingImpl)}.
- *
- * @param original the binding in the parent injector (candidate for an exposing binding)
- * @param binding the binding to check (candidate for the exposed binding)
- */
- private boolean isOkayDuplicate(BindingImpl<?> original, BindingImpl<?> binding, State state) {
- if (original instanceof ExposedBindingImpl) {
- ExposedBindingImpl exposed = (ExposedBindingImpl) original;
- InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector();
- return (exposedFrom == binding.getInjector());
- } else {
- original = (BindingImpl<?>)state.getExplicitBindingsThisLevel().get(binding.getKey());
- // If no original at this level, the original was on a parent, and we don't
- // allow deduplication between parents & children.
- if(original == null) {
- return false;
- } else {
- return original.equals(binding);
- }
- }
- }
-
- private <T> void validateKey(Object source, Key<T> key) {
- Annotations.checkForMisplacedScopeAnnotations(
- key.getTypeLiteral().getRawType(), source, errors);
- }
-
- /**
- * Processor for visiting bindings. Each overriden method that wants to
- * actually process the binding should call prepareBinding first.
- */
- abstract class Processor<T, V> extends DefaultBindingTargetVisitor<T, V> {
- final Object source;
- final Key<T> key;
- final Class<? super T> rawType;
- Scoping scoping;
-
- Processor(BindingImpl<T> binding) {
- source = binding.getSource();
- key = binding.getKey();
- rawType = key.getTypeLiteral().getRawType();
- scoping = binding.getScoping();
- }
-
- protected void prepareBinding() {
- validateKey(source, key);
- scoping = Scoping.makeInjectable(scoping, injector, errors);
- }
-
- protected void scheduleInitialization(final BindingImpl<?> binding) {
- bindingData.addUninitializedBinding(new Runnable() {
- public void run() {
- try {
- binding.getInjector().initializeBinding(binding, errors.withSource(source));
- } catch (ErrorsException e) {
- errors.merge(e.getErrors());
- }
- }
- });
- }
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import java.util.Set; + +import com.google.inject.AbstractModule; +import com.google.inject.Binder; +import com.google.inject.Binding; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.MembersInjector; +import com.google.inject.Module; +import com.google.inject.Provider; +import com.google.inject.Scope; +import com.google.inject.TypeLiteral; +import com.google.common.collect.ImmutableSet; +import com.google.inject.spi.DefaultBindingTargetVisitor; + +/** + * Guarantees that processing of Binding elements happens in a sane way. + * + * @author sameb@google.com (Sam Berlin) + */ +abstract class AbstractBindingProcessor extends AbstractProcessor { + + // It's unfortunate that we have to maintain a blacklist of specific + // classes, but we can't easily block the whole package because of + // all our unit tests. + private static final Set<Class<?>> FORBIDDEN_TYPES = ImmutableSet.<Class<?>>of( + AbstractModule.class, + Binder.class, + Binding.class, + Injector.class, + Key.class, + MembersInjector.class, + Module.class, + Provider.class, + Scope.class, + TypeLiteral.class); + // TODO(jessewilson): fix BuiltInModule, then add Stage + + protected final ProcessedBindingData bindingData; + + AbstractBindingProcessor(Errors errors, ProcessedBindingData bindingData) { + super(errors); + this.bindingData = bindingData; + } + + protected <T> UntargettedBindingImpl<T> invalidBinding( + InjectorImpl injector, Key<T> key, Object source) { + return new UntargettedBindingImpl<T>(injector, key, source); + } + + protected void putBinding(BindingImpl<?> binding) { + Key<?> key = binding.getKey(); + + Class<?> rawType = key.getTypeLiteral().getRawType(); + if (FORBIDDEN_TYPES.contains(rawType)) { + errors.cannotBindToGuiceType(rawType.getSimpleName()); + return; + } + + BindingImpl<?> original = injector.getExistingBinding(key); + if (original != null) { + // If it failed because of an explicit duplicate binding... + if (injector.state.getExplicitBinding(key) != null) { + try { + if(!isOkayDuplicate(original, binding, injector.state)) { + errors.bindingAlreadySet(key, original.getSource()); + return; + } + } catch(Throwable t) { + errors.errorCheckingDuplicateBinding(key, original.getSource(), t); + return; + } + } else { + // Otherwise, it failed because of a duplicate JIT binding + // in the parent + errors.jitBindingAlreadySet(key); + return; + } + } + + // prevent the parent from creating a JIT binding for this key + injector.state.parent().blacklist(key, binding.getSource()); + injector.state.putBinding(key, binding); + } + + /** + * We tolerate duplicate bindings if one exposes the other or if the two bindings + * are considered duplicates (see {@link Bindings#areDuplicates(BindingImpl, BindingImpl)}. + * + * @param original the binding in the parent injector (candidate for an exposing binding) + * @param binding the binding to check (candidate for the exposed binding) + */ + private boolean isOkayDuplicate(BindingImpl<?> original, BindingImpl<?> binding, State state) { + if (original instanceof ExposedBindingImpl) { + ExposedBindingImpl exposed = (ExposedBindingImpl) original; + InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector(); + return (exposedFrom == binding.getInjector()); + } else { + original = (BindingImpl<?>)state.getExplicitBindingsThisLevel().get(binding.getKey()); + // If no original at this level, the original was on a parent, and we don't + // allow deduplication between parents & children. + if(original == null) { + return false; + } else { + return original.equals(binding); + } + } + } + + private <T> void validateKey(Object source, Key<T> key) { + Annotations.checkForMisplacedScopeAnnotations( + key.getTypeLiteral().getRawType(), source, errors); + } + + /** + * Processor for visiting bindings. Each overriden method that wants to + * actually process the binding should call prepareBinding first. + */ + abstract class Processor<T, V> extends DefaultBindingTargetVisitor<T, V> { + final Object source; + final Key<T> key; + final Class<? super T> rawType; + Scoping scoping; + + Processor(BindingImpl<T> binding) { + source = binding.getSource(); + key = binding.getKey(); + rawType = key.getTypeLiteral().getRawType(); + scoping = binding.getScoping(); + } + + protected void prepareBinding() { + validateKey(source, key); + scoping = Scoping.makeInjectable(scoping, injector, errors); + } + + protected void scheduleInitialization(final BindingImpl<?> binding) { + bindingData.addUninitializedBinding(new Runnable() { + public void run() { + try { + binding.getInjector().initializeBinding(binding, errors.withSource(source)); + } catch (ErrorsException e) { + errors.merge(e.getErrors()); + } + } + }); + } + } +} diff --git a/core/src/com/google/inject/internal/CreationListener.java b/core/src/com/google/inject/internal/CreationListener.java index 533b14f0..1779204c 100644 --- a/core/src/com/google/inject/internal/CreationListener.java +++ b/core/src/com/google/inject/internal/CreationListener.java @@ -1,24 +1,24 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-/** Something that is notified upon creation. */
-interface CreationListener {
-
- /** Notifies that creation should happen. */
- void notify(Errors errors);
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +/** Something that is notified upon creation. */ +interface CreationListener { + + /** Notifies that creation should happen. */ + void notify(Errors errors); }
\ No newline at end of file diff --git a/core/src/com/google/inject/internal/Exceptions.java b/core/src/com/google/inject/internal/Exceptions.java index dc6a7232..f9d1e21f 100644 --- a/core/src/com/google/inject/internal/Exceptions.java +++ b/core/src/com/google/inject/internal/Exceptions.java @@ -1,62 +1,62 @@ -/**
- * Copyright (C) 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-
-/**
- * Rethrows user-code exceptions in wrapped exceptions so that Errors can target the correct
- * exception.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-class Exceptions {
-
- /**
- * Rethrows the exception (or it's cause, if it has one) directly if possible.
- * If it was a checked exception, this wraps the exception in a stack trace
- * with no frames, so that the exception is shown immediately with no frames
- * above it.
- */
- public static RuntimeException rethrowCause(Throwable throwable) {
- Throwable cause = throwable;
- if(cause.getCause() != null) {
- cause = cause.getCause();
- }
- return rethrow(cause);
- }
-
- /** Rethrows the exception. */
- public static RuntimeException rethrow(Throwable throwable) {
- if(throwable instanceof RuntimeException) {
- throw (RuntimeException)throwable;
- } else if(throwable instanceof Error) {
- throw (Error)throwable;
- } else {
- throw new UnhandledCheckedUserException(throwable);
- }
- }
-
- /**
- * A marker exception class that we look for in order to unwrap the exception
- * into the user exception, to provide a cleaner stack trace.
- */
- static class UnhandledCheckedUserException extends RuntimeException {
- public UnhandledCheckedUserException(Throwable cause) {
- super(cause);
- }
- }
-}
+/** + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + + +/** + * Rethrows user-code exceptions in wrapped exceptions so that Errors can target the correct + * exception. + * + * @author sameb@google.com (Sam Berlin) + */ +class Exceptions { + + /** + * Rethrows the exception (or it's cause, if it has one) directly if possible. + * If it was a checked exception, this wraps the exception in a stack trace + * with no frames, so that the exception is shown immediately with no frames + * above it. + */ + public static RuntimeException rethrowCause(Throwable throwable) { + Throwable cause = throwable; + if(cause.getCause() != null) { + cause = cause.getCause(); + } + return rethrow(cause); + } + + /** Rethrows the exception. */ + public static RuntimeException rethrow(Throwable throwable) { + if(throwable instanceof RuntimeException) { + throw (RuntimeException)throwable; + } else if(throwable instanceof Error) { + throw (Error)throwable; + } else { + throw new UnhandledCheckedUserException(throwable); + } + } + + /** + * A marker exception class that we look for in order to unwrap the exception + * into the user exception, to provide a cleaner stack trace. + */ + static class UnhandledCheckedUserException extends RuntimeException { + public UnhandledCheckedUserException(Throwable cause) { + super(cause); + } + } +} diff --git a/core/src/com/google/inject/internal/InjectorOptionsProcessor.java b/core/src/com/google/inject/internal/InjectorOptionsProcessor.java index 34abc5c9..4434dce3 100644 --- a/core/src/com/google/inject/internal/InjectorOptionsProcessor.java +++ b/core/src/com/google/inject/internal/InjectorOptionsProcessor.java @@ -1,69 +1,69 @@ -/*
- * Copyright (C) 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-
-import com.google.inject.Stage;
-import com.google.inject.internal.InjectorImpl.InjectorOptions;
-import com.google.inject.spi.DisableCircularProxiesOption;
-import com.google.inject.spi.RequireExplicitBindingsOption;
-
-/**
- * A processor to gather injector options.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-class InjectorOptionsProcessor extends AbstractProcessor {
-
- private boolean disableCircularProxies = false;
- private boolean jitDisabled = false;
-
- InjectorOptionsProcessor(Errors errors) {
- super(errors);
- }
-
- @Override
- public Boolean visit(DisableCircularProxiesOption option) {
- disableCircularProxies = true;
- return true;
- }
-
- @Override
- public Boolean visit(RequireExplicitBindingsOption option) {
- jitDisabled = true;
- return true;
- }
-
- InjectorOptions getOptions(Stage stage, InjectorOptions parentOptions) {
- checkNotNull(stage, "stage must be set");
- if(parentOptions == null) {
- return new InjectorOptions(
- stage,
- jitDisabled,
- disableCircularProxies);
- } else {
- checkState(stage == parentOptions.stage, "child & parent stage don't match");
- return new InjectorOptions(
- stage,
- jitDisabled || parentOptions.jitDisabled,
- disableCircularProxies || parentOptions.disableCircularProxies);
- }
- }
-
-}
+/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + +import com.google.inject.Stage; +import com.google.inject.internal.InjectorImpl.InjectorOptions; +import com.google.inject.spi.DisableCircularProxiesOption; +import com.google.inject.spi.RequireExplicitBindingsOption; + +/** + * A processor to gather injector options. + * + * @author sameb@google.com (Sam Berlin) + */ +class InjectorOptionsProcessor extends AbstractProcessor { + + private boolean disableCircularProxies = false; + private boolean jitDisabled = false; + + InjectorOptionsProcessor(Errors errors) { + super(errors); + } + + @Override + public Boolean visit(DisableCircularProxiesOption option) { + disableCircularProxies = true; + return true; + } + + @Override + public Boolean visit(RequireExplicitBindingsOption option) { + jitDisabled = true; + return true; + } + + InjectorOptions getOptions(Stage stage, InjectorOptions parentOptions) { + checkNotNull(stage, "stage must be set"); + if(parentOptions == null) { + return new InjectorOptions( + stage, + jitDisabled, + disableCircularProxies); + } else { + checkState(stage == parentOptions.stage, "child & parent stage don't match"); + return new InjectorOptions( + stage, + jitDisabled || parentOptions.jitDisabled, + disableCircularProxies || parentOptions.disableCircularProxies); + } + } + +} diff --git a/core/src/com/google/inject/internal/ProcessedBindingData.java b/core/src/com/google/inject/internal/ProcessedBindingData.java index 4ab420d1..3787f1a9 100644 --- a/core/src/com/google/inject/internal/ProcessedBindingData.java +++ b/core/src/com/google/inject/internal/ProcessedBindingData.java @@ -1,54 +1,54 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import java.util.List;
-
-import com.google.common.collect.Lists;
-
-/**
- * Keeps track of creation listeners & uninitialized bindings,
- * so they can be processed after bindings are recorded.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-class ProcessedBindingData {
-
- private final List<CreationListener> creationListeners = Lists.newArrayList();
- private final List<Runnable> uninitializedBindings = Lists.newArrayList();
-
- void addCreationListener(CreationListener listener) {
- creationListeners.add(listener);
- }
-
- void addUninitializedBinding(Runnable runnable) {
- uninitializedBindings.add(runnable);
- }
-
- void initializeBindings() {
- for (Runnable initializer : uninitializedBindings) {
- initializer.run();
- }
- }
-
- void runCreationListeners(Errors errors) {
- for (CreationListener creationListener : creationListeners) {
- creationListener.notify(errors);
- }
- }
-
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import java.util.List; + +import com.google.common.collect.Lists; + +/** + * Keeps track of creation listeners & uninitialized bindings, + * so they can be processed after bindings are recorded. + * + * @author sameb@google.com (Sam Berlin) + */ +class ProcessedBindingData { + + private final List<CreationListener> creationListeners = Lists.newArrayList(); + private final List<Runnable> uninitializedBindings = Lists.newArrayList(); + + void addCreationListener(CreationListener listener) { + creationListeners.add(listener); + } + + void addUninitializedBinding(Runnable runnable) { + uninitializedBindings.add(runnable); + } + + void initializeBindings() { + for (Runnable initializer : uninitializedBindings) { + initializer.run(); + } + } + + void runCreationListeners(Errors errors) { + for (CreationListener creationListener : creationListeners) { + creationListener.notify(errors); + } + } + +} diff --git a/core/src/com/google/inject/internal/ProviderInternalFactory.java b/core/src/com/google/inject/internal/ProviderInternalFactory.java index db0ca192..8902cad5 100644 --- a/core/src/com/google/inject/internal/ProviderInternalFactory.java +++ b/core/src/com/google/inject/internal/ProviderInternalFactory.java @@ -1,91 +1,91 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-package com.google.inject.internal;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import javax.inject.Provider;
-import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback;
-
-import com.google.inject.spi.Dependency;
-
-/**
- * Base class for InternalFactories that are used by Providers, to handle
- * circular dependencies.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-abstract class ProviderInternalFactory<T> implements InternalFactory<T> {
-
- private final ProvisionListenerStackCallback<T> provisionCallback;
- private final boolean allowProxy;
- protected final Object source;
-
- ProviderInternalFactory(Object source, boolean allowProxy,
- ProvisionListenerStackCallback<T> provisionCallback) {
- this.provisionCallback = checkNotNull(provisionCallback, "provisionCallback");
- this.source = checkNotNull(source, "source");
- this.allowProxy = allowProxy;
- }
-
- protected T circularGet(final Provider<? extends T> provider, final Errors errors,
- InternalContext context, final Dependency<?> dependency, boolean linked)
- throws ErrorsException {
- Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType();
- final ConstructionContext<T> constructionContext = context.getConstructionContext(this);
-
- // We have a circular reference between constructors. Return a proxy.
- if (constructionContext.isConstructing()) {
- if (!allowProxy) {
- throw errors.circularProxiesDisabled(expectedType).toException();
- } else {
- // TODO: if we can't proxy this object, can we proxy the other object?
- @SuppressWarnings("unchecked")
- T proxyType = (T) constructionContext.createProxy(errors, expectedType);
- return proxyType;
- }
- }
-
- // Optimization: Don't go through the callback stack if no one's listening.
- if (!provisionCallback.hasListeners()) {
- return provision(provider, errors, dependency, constructionContext);
- } else {
- return provisionCallback.provision(errors, context, new ProvisionCallback<T>() {
- public T call() throws ErrorsException {
- return provision(provider, errors, dependency, constructionContext);
- }
- });
- }
- }
-
- /**
- * Provisions a new instance. Subclasses should override this to catch
- * exceptions & rethrow as ErrorsExceptions.
- */
- protected T provision(Provider<? extends T> provider, Errors errors, Dependency<?> dependency,
- ConstructionContext<T> constructionContext) throws ErrorsException {
- constructionContext.startConstruction();
- try {
- T t = errors.checkForNull(provider.get(), source, dependency);
- constructionContext.setProxyDelegates(t);
- return t;
- } finally {
- constructionContext.finishConstruction();
- }
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.google.inject.internal; + +import static com.google.common.base.Preconditions.checkNotNull; + +import javax.inject.Provider; +import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback; + +import com.google.inject.spi.Dependency; + +/** + * Base class for InternalFactories that are used by Providers, to handle + * circular dependencies. + * + * @author sameb@google.com (Sam Berlin) + */ +abstract class ProviderInternalFactory<T> implements InternalFactory<T> { + + private final ProvisionListenerStackCallback<T> provisionCallback; + private final boolean allowProxy; + protected final Object source; + + ProviderInternalFactory(Object source, boolean allowProxy, + ProvisionListenerStackCallback<T> provisionCallback) { + this.provisionCallback = checkNotNull(provisionCallback, "provisionCallback"); + this.source = checkNotNull(source, "source"); + this.allowProxy = allowProxy; + } + + protected T circularGet(final Provider<? extends T> provider, final Errors errors, + InternalContext context, final Dependency<?> dependency, boolean linked) + throws ErrorsException { + Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType(); + final ConstructionContext<T> constructionContext = context.getConstructionContext(this); + + // We have a circular reference between constructors. Return a proxy. + if (constructionContext.isConstructing()) { + if (!allowProxy) { + throw errors.circularProxiesDisabled(expectedType).toException(); + } else { + // TODO: if we can't proxy this object, can we proxy the other object? + @SuppressWarnings("unchecked") + T proxyType = (T) constructionContext.createProxy(errors, expectedType); + return proxyType; + } + } + + // Optimization: Don't go through the callback stack if no one's listening. + if (!provisionCallback.hasListeners()) { + return provision(provider, errors, dependency, constructionContext); + } else { + return provisionCallback.provision(errors, context, new ProvisionCallback<T>() { + public T call() throws ErrorsException { + return provision(provider, errors, dependency, constructionContext); + } + }); + } + } + + /** + * Provisions a new instance. Subclasses should override this to catch + * exceptions & rethrow as ErrorsExceptions. + */ + protected T provision(Provider<? extends T> provider, Errors errors, Dependency<?> dependency, + ConstructionContext<T> constructionContext) throws ErrorsException { + constructionContext.startConstruction(); + try { + T t = errors.checkForNull(provider.get(), source, dependency); + constructionContext.setProxyDelegates(t); + return t; + } finally { + constructionContext.finishConstruction(); + } + } +} diff --git a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java index 9694b246..b27da9fa 100644 --- a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java +++ b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java @@ -1,89 +1,89 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import java.util.List;
-import java.util.Map;
-
-import com.google.inject.Key;
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
-import com.google.common.collect.MapMaker;
-import com.google.inject.spi.ProvisionListener;
-import com.google.inject.spi.ProvisionListenerBinding;
-
-/**
- * {@link ProvisionListenerStackCallback} for each key.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-final class ProvisionListenerCallbackStore {
- private final ImmutableList<ProvisionListenerBinding> listenerBindings;
-
- private final Map<Key<?>, ProvisionListenerStackCallback<?>> cache
- = new MapMaker().makeComputingMap(
- new Function<Key<?>, ProvisionListenerStackCallback<?>>() {
- public ProvisionListenerStackCallback<?> apply(Key<?> key) {
- return create(key);
- }
- });
-
- ProvisionListenerCallbackStore(List<ProvisionListenerBinding> listenerBindings) {
- this.listenerBindings = ImmutableList.copyOf(listenerBindings);
- }
-
- /** Returns a new {@link ProvisionListenerStackCallback} for the key.
- */
- @SuppressWarnings("unchecked") // the ProvisionListenerStackCallback type always agrees with the passed type
- public <T> ProvisionListenerStackCallback<T> get(Key<T> key) {
- return (ProvisionListenerStackCallback<T>) cache.get(key);
- }
-
- /**
- * Purges a key from the cache. Use this only if the type is not actually valid for
- * binding and needs to be purged. (See issue 319 and
- * ImplicitBindingTest#testCircularJitBindingsLeaveNoResidue and
- * #testInstancesRequestingProvidersForThemselvesWithChildInjectors for examples of when this is
- * necessary.)
- *
- * Returns true if the type was stored in the cache, false otherwise.
- */
- boolean remove(Key<?> type) {
- return cache.remove(type) != null;
- }
-
- /**
- * Creates a new {@link ProvisionListenerStackCallback} with the correct listeners
- * for the key.
- */
- private <T> ProvisionListenerStackCallback<T> create(Key<T> key) {
- List<ProvisionListener> listeners = null;
- for (ProvisionListenerBinding binding : listenerBindings) {
- if (binding.getKeyMatcher().matches(key)) {
- if (listeners == null) {
- listeners = Lists.newArrayList();
- }
- listeners.addAll(binding.getListeners());
- }
- }
- if (listeners == null) {
- listeners = ImmutableList.of();
- }
- return new ProvisionListenerStackCallback<T>(key, listeners);
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import java.util.List; +import java.util.Map; + +import com.google.inject.Key; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.collect.MapMaker; +import com.google.inject.spi.ProvisionListener; +import com.google.inject.spi.ProvisionListenerBinding; + +/** + * {@link ProvisionListenerStackCallback} for each key. + * + * @author sameb@google.com (Sam Berlin) + */ +final class ProvisionListenerCallbackStore { + private final ImmutableList<ProvisionListenerBinding> listenerBindings; + + private final Map<Key<?>, ProvisionListenerStackCallback<?>> cache + = new MapMaker().makeComputingMap( + new Function<Key<?>, ProvisionListenerStackCallback<?>>() { + public ProvisionListenerStackCallback<?> apply(Key<?> key) { + return create(key); + } + }); + + ProvisionListenerCallbackStore(List<ProvisionListenerBinding> listenerBindings) { + this.listenerBindings = ImmutableList.copyOf(listenerBindings); + } + + /** Returns a new {@link ProvisionListenerStackCallback} for the key. + */ + @SuppressWarnings("unchecked") // the ProvisionListenerStackCallback type always agrees with the passed type + public <T> ProvisionListenerStackCallback<T> get(Key<T> key) { + return (ProvisionListenerStackCallback<T>) cache.get(key); + } + + /** + * Purges a key from the cache. Use this only if the type is not actually valid for + * binding and needs to be purged. (See issue 319 and + * ImplicitBindingTest#testCircularJitBindingsLeaveNoResidue and + * #testInstancesRequestingProvidersForThemselvesWithChildInjectors for examples of when this is + * necessary.) + * + * Returns true if the type was stored in the cache, false otherwise. + */ + boolean remove(Key<?> type) { + return cache.remove(type) != null; + } + + /** + * Creates a new {@link ProvisionListenerStackCallback} with the correct listeners + * for the key. + */ + private <T> ProvisionListenerStackCallback<T> create(Key<T> key) { + List<ProvisionListener> listeners = null; + for (ProvisionListenerBinding binding : listenerBindings) { + if (binding.getKeyMatcher().matches(key)) { + if (listeners == null) { + listeners = Lists.newArrayList(); + } + listeners.addAll(binding.getListeners()); + } + } + if (listeners == null) { + listeners = ImmutableList.of(); + } + return new ProvisionListenerStackCallback<T>(key, listeners); + } +} diff --git a/core/src/com/google/inject/internal/ProvisionListenerStackCallback.java b/core/src/com/google/inject/internal/ProvisionListenerStackCallback.java index 6674fe5f..a865a0a8 100644 --- a/core/src/com/google/inject/internal/ProvisionListenerStackCallback.java +++ b/core/src/com/google/inject/internal/ProvisionListenerStackCallback.java @@ -1,133 +1,133 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import java.util.List;
-
-import com.google.inject.Key;
-import com.google.inject.ProvisionException;
-import com.google.inject.spi.ProvisionListener;
-import com.google.inject.spi.DependencyAndSource;
-
-/**
- * Intercepts provisions with a stack of listeners.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-final class ProvisionListenerStackCallback<T> {
-
- private static final ProvisionListener EMPTY_LISTENER[] = new ProvisionListener[0];
- private final ProvisionListener[] listeners;
- private final Key<T> key;
-
- public ProvisionListenerStackCallback(Key<T> key, List<ProvisionListener> listeners) {
- this.key = key;
- if (listeners.isEmpty()) {
- this.listeners = EMPTY_LISTENER;
- } else {
- this.listeners = listeners.toArray(new ProvisionListener[listeners.size()]);
- }
- }
-
- public boolean hasListeners() {
- return listeners.length > 0;
- }
-
- public T provision(Errors errors, InternalContext context, ProvisionCallback<T> callable)
- throws ErrorsException {
- Provision provision = new Provision(errors, context, callable);
- RuntimeException caught = null;
- try {
- provision.provision();
- } catch(RuntimeException t) {
- caught = t;
- }
-
- if (provision.exceptionDuringProvision != null) {
- throw provision.exceptionDuringProvision;
- } else if (caught != null) {
- Object listener = provision.erredListener != null ?
- provision.erredListener.getClass() : "(unknown)";
- throw errors
- .errorInUserCode(caught, "Error notifying ProvisionListener %s of %s.%n"
- + " Reason: %s", listener, key, caught)
- .toException();
- } else {
- return provision.result;
- }
- }
-
- // TODO(sameb): Can this be more InternalFactory-like?
- public interface ProvisionCallback<T> {
- public T call() throws ErrorsException;
- }
-
- private class Provision extends ProvisionListener.ProvisionInvocation<T> {
-
- final Errors errors;
- final InternalContext context;
- final ProvisionCallback<T> callable;
- int index = -1;
- T result;
- ErrorsException exceptionDuringProvision;
- ProvisionListener erredListener;
-
- public Provision(Errors errors, InternalContext context, ProvisionCallback<T> callable) {
- this.callable = callable;
- this.context = context;
- this.errors = errors;
- }
-
- @Override
- public T provision() {
- index++;
- if (index == listeners.length) {
- try {
- result = callable.call();
- } catch(ErrorsException ee) {
- exceptionDuringProvision = ee;
- throw new ProvisionException(errors.merge(ee.getErrors()).getMessages());
- }
- } else if (index < listeners.length) {
- int currentIdx = index;
- try {
- listeners[index].onProvision(this);
- } catch(RuntimeException re) {
- erredListener = listeners[currentIdx];
- throw re;
- }
- if (currentIdx == index) {
- // Our listener didn't provision -- do it for them.
- provision();
- }
- } else {
- throw new IllegalStateException("Already provisioned in this listener.");
- }
- return result;
- }
-
- @Override
- public Key<T> getKey() {
- return key;
- }
-
- @Override
- public List<DependencyAndSource> getDependencyChain() {
- return context.getDependencyChain();
- }
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import java.util.List; + +import com.google.inject.Key; +import com.google.inject.ProvisionException; +import com.google.inject.spi.ProvisionListener; +import com.google.inject.spi.DependencyAndSource; + +/** + * Intercepts provisions with a stack of listeners. + * + * @author sameb@google.com (Sam Berlin) + */ +final class ProvisionListenerStackCallback<T> { + + private static final ProvisionListener EMPTY_LISTENER[] = new ProvisionListener[0]; + private final ProvisionListener[] listeners; + private final Key<T> key; + + public ProvisionListenerStackCallback(Key<T> key, List<ProvisionListener> listeners) { + this.key = key; + if (listeners.isEmpty()) { + this.listeners = EMPTY_LISTENER; + } else { + this.listeners = listeners.toArray(new ProvisionListener[listeners.size()]); + } + } + + public boolean hasListeners() { + return listeners.length > 0; + } + + public T provision(Errors errors, InternalContext context, ProvisionCallback<T> callable) + throws ErrorsException { + Provision provision = new Provision(errors, context, callable); + RuntimeException caught = null; + try { + provision.provision(); + } catch(RuntimeException t) { + caught = t; + } + + if (provision.exceptionDuringProvision != null) { + throw provision.exceptionDuringProvision; + } else if (caught != null) { + Object listener = provision.erredListener != null ? + provision.erredListener.getClass() : "(unknown)"; + throw errors + .errorInUserCode(caught, "Error notifying ProvisionListener %s of %s.%n" + + " Reason: %s", listener, key, caught) + .toException(); + } else { + return provision.result; + } + } + + // TODO(sameb): Can this be more InternalFactory-like? + public interface ProvisionCallback<T> { + public T call() throws ErrorsException; + } + + private class Provision extends ProvisionListener.ProvisionInvocation<T> { + + final Errors errors; + final InternalContext context; + final ProvisionCallback<T> callable; + int index = -1; + T result; + ErrorsException exceptionDuringProvision; + ProvisionListener erredListener; + + public Provision(Errors errors, InternalContext context, ProvisionCallback<T> callable) { + this.callable = callable; + this.context = context; + this.errors = errors; + } + + @Override + public T provision() { + index++; + if (index == listeners.length) { + try { + result = callable.call(); + } catch(ErrorsException ee) { + exceptionDuringProvision = ee; + throw new ProvisionException(errors.merge(ee.getErrors()).getMessages()); + } + } else if (index < listeners.length) { + int currentIdx = index; + try { + listeners[index].onProvision(this); + } catch(RuntimeException re) { + erredListener = listeners[currentIdx]; + throw re; + } + if (currentIdx == index) { + // Our listener didn't provision -- do it for them. + provision(); + } + } else { + throw new IllegalStateException("Already provisioned in this listener."); + } + return result; + } + + @Override + public Key<T> getKey() { + return key; + } + + @Override + public List<DependencyAndSource> getDependencyChain() { + return context.getDependencyChain(); + } + } +} diff --git a/core/src/com/google/inject/internal/UntargettedBindingProcessor.java b/core/src/com/google/inject/internal/UntargettedBindingProcessor.java index d0561271..8ca2ecc8 100644 --- a/core/src/com/google/inject/internal/UntargettedBindingProcessor.java +++ b/core/src/com/google/inject/internal/UntargettedBindingProcessor.java @@ -1,69 +1,69 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.internal;
-
-import com.google.inject.Binding;
-import com.google.inject.spi.UntargettedBinding;
-
-/**
- * Processes just UntargettedBindings.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-class UntargettedBindingProcessor extends AbstractBindingProcessor {
-
- UntargettedBindingProcessor(Errors errors, ProcessedBindingData bindingData) {
- super(errors, bindingData);
- }
-
- @Override
- public <T> Boolean visit(Binding<T> binding) {
- return binding.acceptTargetVisitor(new Processor<T, Boolean>((BindingImpl<T>)binding) {
- public Boolean visit(UntargettedBinding<? extends T> untargetted) {
- prepareBinding();
-
- // Error: Missing implementation.
- // Example: bind(Date.class).annotatedWith(Red.class);
- // We can't assume abstract types aren't injectable. They may have an
- // @ImplementedBy annotation or something.
- if (key.getAnnotationType() != null) {
- errors.missingImplementation(key);
- putBinding(invalidBinding(injector, key, source));
- return true;
- }
-
- // This cast is safe after the preceeding check.
- try {
- BindingImpl<T> binding = injector.createUninitializedBinding(
- key, scoping, source, errors, false);
- scheduleInitialization(binding);
- putBinding(binding);
- } catch (ErrorsException e) {
- errors.merge(e.getErrors());
- putBinding(invalidBinding(injector, key, source));
- }
-
- return true;
- }
-
- @Override
- protected Boolean visitOther(Binding<? extends T> binding) {
- return false;
- }
- });
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.internal; + +import com.google.inject.Binding; +import com.google.inject.spi.UntargettedBinding; + +/** + * Processes just UntargettedBindings. + * + * @author sameb@google.com (Sam Berlin) + */ +class UntargettedBindingProcessor extends AbstractBindingProcessor { + + UntargettedBindingProcessor(Errors errors, ProcessedBindingData bindingData) { + super(errors, bindingData); + } + + @Override + public <T> Boolean visit(Binding<T> binding) { + return binding.acceptTargetVisitor(new Processor<T, Boolean>((BindingImpl<T>)binding) { + public Boolean visit(UntargettedBinding<? extends T> untargetted) { + prepareBinding(); + + // Error: Missing implementation. + // Example: bind(Date.class).annotatedWith(Red.class); + // We can't assume abstract types aren't injectable. They may have an + // @ImplementedBy annotation or something. + if (key.getAnnotationType() != null) { + errors.missingImplementation(key); + putBinding(invalidBinding(injector, key, source)); + return true; + } + + // This cast is safe after the preceeding check. + try { + BindingImpl<T> binding = injector.createUninitializedBinding( + key, scoping, source, errors, false); + scheduleInitialization(binding); + putBinding(binding); + } catch (ErrorsException e) { + errors.merge(e.getErrors()); + putBinding(invalidBinding(injector, key, source)); + } + + return true; + } + + @Override + protected Boolean visitOther(Binding<? extends T> binding) { + return false; + } + }); + } +} diff --git a/core/src/com/google/inject/spi/DependencyAndSource.java b/core/src/com/google/inject/spi/DependencyAndSource.java index 23f9d0d4..c4b22876 100644 --- a/core/src/com/google/inject/spi/DependencyAndSource.java +++ b/core/src/com/google/inject/spi/DependencyAndSource.java @@ -1,76 +1,76 @@ -/*
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.google.inject.spi;
-
-import java.lang.reflect.Member;
-
-import com.google.inject.Binding;
-import com.google.inject.internal.util.StackTraceElements;
-
-/**
- * A combination of a {@link Dependency} and the {@link Binding#getSource()
- * source} where the dependency was bound.
- *
- * @author sameb@google.com (Sam Berlin)
- */
-public final class DependencyAndSource {
- private final Dependency<?> dependency;
- private final Object source;
-
- public DependencyAndSource(Dependency<?> dependency, Object source) {
- this.dependency = dependency;
- this.source = source;
- }
-
- /**
- * Returns the Dependency, if one exists. For anything that can be referenced
- * by {@link Injector#getBinding}, a dependency exists. A dependency will not
- * exist (and this will return null) for types initialized with
- * {@link Binder#requestInjection} or {@link Injector#injectMembers(Object),
- * nor will it exist for objects injected into Providers bound with
- * LinkedBindingBuilder#toProvider(Provider).
- */
- public Dependency<?> getDependency() {
- return dependency;
- }
-
- /**
- * Returns a string describing where this dependency was bound. If the binding
- * was just-in-time, there is no valid binding source, so this describes the
- * class in question.
- */
- public String getBindingSource() {
- if (source instanceof Class) {
- return StackTraceElements.forType((Class) source).toString();
- } else if (source instanceof Member) {
- return StackTraceElements.forMember((Member) source).toString();
- } else {
- return source.toString();
- }
- }
-
- @Override
- public String toString() {
- Dependency<?> dep = getDependency();
- Object source = getBindingSource();
- if (dep != null) {
- return "Dependency: " + dep + ", source: " + source;
- } else {
- return "Source: " + source;
- }
- }
+/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.inject.spi; + +import java.lang.reflect.Member; + +import com.google.inject.Binding; +import com.google.inject.internal.util.StackTraceElements; + +/** + * A combination of a {@link Dependency} and the {@link Binding#getSource() + * source} where the dependency was bound. + * + * @author sameb@google.com (Sam Berlin) + */ +public final class DependencyAndSource { + private final Dependency<?> dependency; + private final Object source; + + public DependencyAndSource(Dependency<?> dependency, Object source) { + this.dependency = dependency; + this.source = source; + } + + /** + * Returns the Dependency, if one exists. For anything that can be referenced + * by {@link Injector#getBinding}, a dependency exists. A dependency will not + * exist (and this will return null) for types initialized with + * {@link Binder#requestInjection} or {@link Injector#injectMembers(Object), + * nor will it exist for objects injected into Providers bound with + * LinkedBindingBuilder#toProvider(Provider). + */ + public Dependency<?> getDependency() { + return dependency; + } + + /** + * Returns a string describing where this dependency was bound. If the binding + * was just-in-time, there is no valid binding source, so this describes the + * class in question. + */ + public String getBindingSource() { + if (source instanceof Class) { + return StackTraceElements.forType((Class) source).toString(); + } else if (source instanceof Member) { + return StackTraceElements.forMember((Member) source).toString(); + } else { + return source.toString(); + } + } + + @Override + public String toString() { + Dependency<?> dep = getDependency(); + Object source = getBindingSource(); + if (dep != null) { + return "Dependency: " + dep + ", source: " + source; + } else { + return "Source: " + source; + } + } }
\ No newline at end of file diff --git a/core/src/com/google/inject/spi/DisableCircularProxiesOption.java b/core/src/com/google/inject/spi/DisableCircularProxiesOption.java index e45c7c04..d228efbe 100644 --- a/core/src/com/google/inject/spi/DisableCircularProxiesOption.java +++ b/core/src/com/google/inject/spi/DisableCircularProxiesOption.java @@ -1,46 +1,46 @@ -/**
- * Copyright (C) 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.spi;
-
-import com.google.inject.Binder;
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * A request to disable circular proxies.
- *
- * @author sameb@google.com (Sam Berlin)
- * @since 3.0
- */
-public final class DisableCircularProxiesOption implements Element {
- private final Object source;
-
- DisableCircularProxiesOption(Object source) {
- this.source = checkNotNull(source, "source");
- }
-
- public Object getSource() {
- return source;
- }
-
- public void applyTo(Binder binder) {
- binder.withSource(getSource()).disableCircularProxies();
- }
-
- public <T> T acceptVisitor(ElementVisitor<T> visitor) {
- return visitor.visit(this);
- }
-}
+/** + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.spi; + +import com.google.inject.Binder; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A request to disable circular proxies. + * + * @author sameb@google.com (Sam Berlin) + * @since 3.0 + */ +public final class DisableCircularProxiesOption implements Element { + private final Object source; + + DisableCircularProxiesOption(Object source) { + this.source = checkNotNull(source, "source"); + } + + public Object getSource() { + return source; + } + + public void applyTo(Binder binder) { + binder.withSource(getSource()).disableCircularProxies(); + } + + public <T> T acceptVisitor(ElementVisitor<T> visitor) { + return visitor.visit(this); + } +} diff --git a/core/src/com/google/inject/spi/ProvisionListener.java b/core/src/com/google/inject/spi/ProvisionListener.java index 82642f38..5fde5881 100644 --- a/core/src/com/google/inject/spi/ProvisionListener.java +++ b/core/src/com/google/inject/spi/ProvisionListener.java @@ -1,62 +1,62 @@ -/*
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.google.inject.spi;
-
-import java.util.List;
-
-import com.google.inject.Key;
-import com.google.inject.Provider;
-import com.google.inject.Scope;
-
-/**
- * Listens for provisioning of objects. Useful for gathering timing information
- * about provisioning, post-provision initialization, and more.
- *
- * @author sameb@google.com (Sam Berlin)
- * @since 4.0
- */
-public interface ProvisionListener {
-
- /**
- * Invoked by Guice when an object requires provisioning. Provisioning occurs
- * when Guice locates and injects the dependencies for a binding. For types
- * bound to a Provider, provisioning encapsulates the {@link Provider#get}
- * method. For other types, provisioning encapsulates the construction of the
- * object. If a type is bound within a {@link Scope}, provisioning depends on
- * the scope. Types bound in Singleton scope will only be provisioned once.
- * Types bound in no scope will be provisioned every time they are injected.
- * Other scopes define their own behavior for provisioning.
- * <p>
- * To perform the provision, call {@link ProvisionInvocation#provision()}.
- * If you do not explicitly call provision, it will be automatically done after
- * this method returns. It is an error to call provision more than once.
- */
- <T> void onProvision(ProvisionInvocation<T> provision);
-
- /** Encapsulates a single act of provisioning.*/
- public abstract static class ProvisionInvocation<T> {
-
- /** Returns the Key which will be provisioned. */
- public abstract Key<T> getKey();
-
- /** Performs the provision, returning the object provisioned. */
- public abstract T provision();
-
- /** Returns the dependency chain that led to this object being provisioned. */
- public abstract List<DependencyAndSource> getDependencyChain();
- }
-}
+/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.inject.spi; + +import java.util.List; + +import com.google.inject.Key; +import com.google.inject.Provider; +import com.google.inject.Scope; + +/** + * Listens for provisioning of objects. Useful for gathering timing information + * about provisioning, post-provision initialization, and more. + * + * @author sameb@google.com (Sam Berlin) + * @since 4.0 + */ +public interface ProvisionListener { + + /** + * Invoked by Guice when an object requires provisioning. Provisioning occurs + * when Guice locates and injects the dependencies for a binding. For types + * bound to a Provider, provisioning encapsulates the {@link Provider#get} + * method. For other types, provisioning encapsulates the construction of the + * object. If a type is bound within a {@link Scope}, provisioning depends on + * the scope. Types bound in Singleton scope will only be provisioned once. + * Types bound in no scope will be provisioned every time they are injected. + * Other scopes define their own behavior for provisioning. + * <p> + * To perform the provision, call {@link ProvisionInvocation#provision()}. + * If you do not explicitly call provision, it will be automatically done after + * this method returns. It is an error to call provision more than once. + */ + <T> void onProvision(ProvisionInvocation<T> provision); + + /** Encapsulates a single act of provisioning.*/ + public abstract static class ProvisionInvocation<T> { + + /** Returns the Key which will be provisioned. */ + public abstract Key<T> getKey(); + + /** Performs the provision, returning the object provisioned. */ + public abstract T provision(); + + /** Returns the dependency chain that led to this object being provisioned. */ + public abstract List<DependencyAndSource> getDependencyChain(); + } +} diff --git a/core/src/com/google/inject/spi/ProvisionListenerBinding.java b/core/src/com/google/inject/spi/ProvisionListenerBinding.java index b75ac154..3d40232c 100644 --- a/core/src/com/google/inject/spi/ProvisionListenerBinding.java +++ b/core/src/com/google/inject/spi/ProvisionListenerBinding.java @@ -1,69 +1,69 @@ -/**
- * Copyright (C) 2011 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.spi;
-
-import java.util.List;
-
-import com.google.inject.Binder;
-import com.google.inject.Key;
-import com.google.common.collect.ImmutableList;
-import com.google.inject.matcher.Matcher;
-
-/**
- * Binds keys (picked using a Matcher) to a provision listener. Listeners are created explicitly in
- * a module using {@link Binder#bindListener(Matcher, ProvisionListener)} statements:
- *
- * @author sameb@google.com (Sam Berlin)
- * @since 4.0
- */
-public final class ProvisionListenerBinding implements Element {
-
- private final Object source;
- private final Matcher<? super Key<?>> keyMatcher;
- private final List<ProvisionListener> listeners;
-
- ProvisionListenerBinding(Object source,
- Matcher<? super Key<?>> typeMatcher,
- ProvisionListener[] listeners) {
- this.source = source;
- this.keyMatcher = typeMatcher;
- this.listeners = ImmutableList.of(listeners);
- }
-
- /** Returns the registered listeners. */
- public List<ProvisionListener> getListeners() {
- return listeners;
- }
-
- /** Returns the key matcher which chooses which keys the listener should be notified of. */
- public Matcher<? super Key<?>> getKeyMatcher() {
- return keyMatcher;
- }
-
- public Object getSource() {
- return source;
- }
-
- public <R> R acceptVisitor(ElementVisitor<R> visitor) {
- return visitor.visit(this);
- }
-
- public void applyTo(Binder binder) {
- binder.withSource(getSource()).bindListener(keyMatcher,
- listeners.toArray(new ProvisionListener[listeners.size()]));
- }
-}
+/** + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.spi; + +import java.util.List; + +import com.google.inject.Binder; +import com.google.inject.Key; +import com.google.common.collect.ImmutableList; +import com.google.inject.matcher.Matcher; + +/** + * Binds keys (picked using a Matcher) to a provision listener. Listeners are created explicitly in + * a module using {@link Binder#bindListener(Matcher, ProvisionListener)} statements: + * + * @author sameb@google.com (Sam Berlin) + * @since 4.0 + */ +public final class ProvisionListenerBinding implements Element { + + private final Object source; + private final Matcher<? super Key<?>> keyMatcher; + private final List<ProvisionListener> listeners; + + ProvisionListenerBinding(Object source, + Matcher<? super Key<?>> typeMatcher, + ProvisionListener[] listeners) { + this.source = source; + this.keyMatcher = typeMatcher; + this.listeners = ImmutableList.of(listeners); + } + + /** Returns the registered listeners. */ + public List<ProvisionListener> getListeners() { + return listeners; + } + + /** Returns the key matcher which chooses which keys the listener should be notified of. */ + public Matcher<? super Key<?>> getKeyMatcher() { + return keyMatcher; + } + + public Object getSource() { + return source; + } + + public <R> R acceptVisitor(ElementVisitor<R> visitor) { + return visitor.visit(this); + } + + public void applyTo(Binder binder) { + binder.withSource(getSource()).bindListener(keyMatcher, + listeners.toArray(new ProvisionListener[listeners.size()])); + } +} diff --git a/core/src/com/google/inject/spi/RequireExplicitBindingsOption.java b/core/src/com/google/inject/spi/RequireExplicitBindingsOption.java index b2a259de..93123bb0 100644 --- a/core/src/com/google/inject/spi/RequireExplicitBindingsOption.java +++ b/core/src/com/google/inject/spi/RequireExplicitBindingsOption.java @@ -1,46 +1,46 @@ -/**
- * Copyright (C) 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject.spi;
-
-import com.google.inject.Binder;
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * A request to require explicit bindings.
- *
- * @author sameb@google.com (Sam Berlin)
- * @since 3.0
- */
-public final class RequireExplicitBindingsOption implements Element {
- private final Object source;
-
- RequireExplicitBindingsOption(Object source) {
- this.source = checkNotNull(source, "source");
- }
-
- public Object getSource() {
- return source;
- }
-
- public void applyTo(Binder binder) {
- binder.withSource(getSource()).requireExplicitBindings();
- }
-
- public <T> T acceptVisitor(ElementVisitor<T> visitor) {
- return visitor.visit(this);
- }
-}
+/** + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.inject.spi; + +import com.google.inject.Binder; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A request to require explicit bindings. + * + * @author sameb@google.com (Sam Berlin) + * @since 3.0 + */ +public final class RequireExplicitBindingsOption implements Element { + private final Object source; + + RequireExplicitBindingsOption(Object source) { + this.source = checkNotNull(source, "source"); + } + + public Object getSource() { + return source; + } + + public void applyTo(Binder binder) { + binder.withSource(getSource()).requireExplicitBindings(); + } + + public <T> T acceptVisitor(ElementVisitor<T> visitor) { + return visitor.visit(this); + } +} diff --git a/core/src/com/google/inject/spi/Toolable.java b/core/src/com/google/inject/spi/Toolable.java index 42897ad6..e646b51d 100644 --- a/core/src/com/google/inject/spi/Toolable.java +++ b/core/src/com/google/inject/spi/Toolable.java @@ -1,28 +1,28 @@ -package com.google.inject.spi;
-
-import com.google.inject.Injector;
-import com.google.inject.Stage;
-import java.lang.annotation.Documented;
-import static java.lang.annotation.ElementType.METHOD;
-import java.lang.annotation.Retention;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import java.lang.annotation.Target;
-
-
-/**
- * Instructs an {@link Injector} running in {@link Stage#TOOL} that a method should be injected.
- * This is typically useful for for extensions to Guice that perform additional validation in an
- * injected method or field. This only applies to objects that are already constructed when
- * bindings are created (ie., something bound using {@link
- * com.google.inject.binder.LinkedBindingBuilder#toProvider toProvider}, {@link
- * com.google.inject.binder.LinkedBindingBuilder#toInstance toInstance}, or {@link
- * com.google.inject.Binder#requestInjection requestInjection}.
- *
- * @author sberlin@gmail.com (Sam Berlin)
- * @since 3.0
- */
-@Target({ METHOD })
-@Retention(RUNTIME)
-@Documented
-public @interface Toolable {
-}
+package com.google.inject.spi; + +import com.google.inject.Injector; +import com.google.inject.Stage; +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + + +/** + * Instructs an {@link Injector} running in {@link Stage#TOOL} that a method should be injected. + * This is typically useful for for extensions to Guice that perform additional validation in an + * injected method or field. This only applies to objects that are already constructed when + * bindings are created (ie., something bound using {@link + * com.google.inject.binder.LinkedBindingBuilder#toProvider toProvider}, {@link + * com.google.inject.binder.LinkedBindingBuilder#toInstance toInstance}, or {@link + * com.google.inject.Binder#requestInjection requestInjection}. + * + * @author sberlin@gmail.com (Sam Berlin) + * @since 3.0 + */ +@Target({ METHOD }) +@Retention(RUNTIME) +@Documented +public @interface Toolable { +} |