All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.google.inject.internal.BindingBuilder Maven / Gradle / Ivy
/**
* Copyright (C) 2008 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.Binder;
import com.google.inject.ConfigurationException;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;
import com.google.inject.internal.util.ImmutableSet;
import static com.google.inject.internal.util.Preconditions.checkNotNull;
import com.google.inject.spi.Element;
import com.google.inject.spi.InjectionPoint;
import com.google.inject.spi.Message;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Set;
/**
* Bind a non-constant key.
*
* @author [email protected] (Jesse Wilson)
*/
public class BindingBuilder extends AbstractBindingBuilder
implements AnnotatedBindingBuilder {
public BindingBuilder(Binder binder, List elements, Object source, Key key) {
super(binder, elements, source, key);
}
public BindingBuilder annotatedWith(Class extends Annotation> annotationType) {
annotatedWithInternal(annotationType);
return this;
}
public BindingBuilder annotatedWith(Annotation annotation) {
annotatedWithInternal(annotation);
return this;
}
public BindingBuilder to(Class extends T> implementation) {
return to(Key.get(implementation));
}
public BindingBuilder to(TypeLiteral extends T> implementation) {
return to(Key.get(implementation));
}
public BindingBuilder to(Key extends T> linkedKey) {
checkNotNull(linkedKey, "linkedKey");
checkNotTargetted();
BindingImpl base = getBinding();
setBinding(new LinkedBindingImpl(
base.getSource(), base.getKey(), base.getScoping(), linkedKey));
return this;
}
public void toInstance(T instance) {
checkNotTargetted();
// lookup the injection points, adding any errors to the binder's errors list
Set injectionPoints;
if (instance != null) {
try {
injectionPoints = InjectionPoint.forInstanceMethodsAndFields(instance.getClass());
} catch (ConfigurationException e) {
copyErrorsToBinder(e);
injectionPoints = e.getPartialValue();
}
} else {
binder.addError(BINDING_TO_NULL);
injectionPoints = ImmutableSet.of();
}
BindingImpl base = getBinding();
setBinding(new InstanceBindingImpl(
base.getSource(), base.getKey(), Scoping.EAGER_SINGLETON, injectionPoints, instance));
}
public BindingBuilder toProvider(Provider extends T> provider) {
checkNotNull(provider, "provider");
checkNotTargetted();
// lookup the injection points, adding any errors to the binder's errors list
Set injectionPoints;
try {
injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass());
} catch (ConfigurationException e) {
copyErrorsToBinder(e);
injectionPoints = e.getPartialValue();
}
BindingImpl base = getBinding();
setBinding(new ProviderInstanceBindingImpl(
base.getSource(), base.getKey(), base.getScoping(), injectionPoints, provider));
return this;
}
public BindingBuilder toProvider(
Class extends javax.inject.Provider extends T>> providerType) {
return toProvider(Key.get(providerType));
}
public BindingBuilder toProvider(
TypeLiteral extends javax.inject.Provider extends T>> providerType) {
return toProvider(Key.get(providerType));
}
public BindingBuilder toProvider(
Key extends javax.inject.Provider extends T>> providerKey) {
checkNotNull(providerKey, "providerKey");
checkNotTargetted();
BindingImpl base = getBinding();
setBinding(new LinkedProviderBindingImpl(
base.getSource(), base.getKey(), base.getScoping(), providerKey));
return this;
}
public ScopedBindingBuilder toConstructor(Constructor constructor) {
return toConstructor(constructor, TypeLiteral.get(constructor.getDeclaringClass()));
}
public ScopedBindingBuilder toConstructor(Constructor constructor,
TypeLiteral extends S> type) {
checkNotNull(constructor, "constructor");
checkNotNull(type, "type");
checkNotTargetted();
BindingImpl base = getBinding();
Set injectionPoints;
try {
injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
} catch (ConfigurationException e) {
copyErrorsToBinder(e);
injectionPoints = e.getPartialValue();
}
try {
@SuppressWarnings("unchecked") // safe; constructor is a subtype of toConstruct
InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type);
setBinding(new ConstructorBindingImpl(base.getKey(), base.getSource(), base.getScoping(),
constructorPoint, injectionPoints));
} catch (ConfigurationException e) {
copyErrorsToBinder(e);
}
return this;
}
@Override public String toString() {
return "BindingBuilder<" + getBinding().getKey().getTypeLiteral() + ">";
}
private void copyErrorsToBinder(ConfigurationException e) {
for (Message message : e.getErrorMessages()) {
binder.addError(message);
}
}
}