001: /*
002: * Copyright (C) 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package com.google.inject;
018:
019: import com.google.inject.binder.AnnotatedBindingBuilder;
020: import com.google.inject.binder.ConstantBindingBuilder;
021: import com.google.inject.binder.LinkedBindingBuilder;
022: import com.google.inject.binder.AnnotatedConstantBindingBuilder;
023: import com.google.inject.matcher.Matcher;
024: import java.lang.annotation.Annotation;
025: import java.lang.reflect.Method;
026: import org.aopalliance.intercept.MethodInterceptor;
027:
028: /**
029: * Collects configuration information (primarily <i>bindings</i>) which will be
030: * used to create an {@link Injector}. Guice provides this object to your
031: * application's {@link Module}s so they may each contribute
032: * their own bindings.
033: *
034: * <p>The bindings contributed by {@code Module}s define how the {@code
035: * Injector} resolves dependencies. A {@link Key} consisting of a type
036: * and optional annotation uniquely identifies a binding within an {@code
037: * Injector}.
038: *
039: * <p>You may bind from a key to:
040: *
041: * <ul>
042: * <li>Another binding, which this binding's key is now "aliased to"
043: * <li>Another binding, which references a {@link Provider} for this key
044: * <li>A preconstructed instance
045: * <li>A preconstructed instance which should be used as the {@link Provider}
046: * for this binding
047: * </ul>
048: *
049: * <p>In addition, a binding may have an associated scope, such as
050: * {@link Scopes#SINGLETON}, and singleton bindings may specify eager or lazy
051: * initialization.
052: *
053: * <p>See the users' guide appendix, "How the Injector resolves injection
054: * requests," to better understand binding resolution.
055: *
056: * <p>After an {@code Injector} has been created, its bindings may be
057: * examined using methods like {@link Injector#getBinding(Key)}, but this
058: * read-only {@link Binding} type is not used when <i>creating</i> the
059: * bindings.
060: */
061: public interface Binder {
062:
063: /**
064: * Binds a method interceptor to methods matched by class and method
065: * matchers.
066: *
067: * @param classMatcher matches classes the interceptor should apply to. For
068: * example: {@code only(Runnable.class)}.
069: * @param methodMatcher matches methods the interceptor should apply to. For
070: * example: {@code annotatedWith(Transactional.class)}.
071: * @param interceptors to bind
072: */
073: void bindInterceptor(Matcher<? super Class<?>> classMatcher,
074: Matcher<? super Method> methodMatcher,
075: MethodInterceptor... interceptors);
076:
077: /**
078: * Binds a scope to an annotation.
079: */
080: void bindScope(Class<? extends Annotation> annotationType,
081: Scope scope);
082:
083: /**
084: * Creates a binding to a key.
085: */
086: <T> LinkedBindingBuilder<T> bind(Key<T> key);
087:
088: /**
089: * Creates a binding to a type.
090: */
091: <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral);
092:
093: /**
094: * Creates a binding to a type.
095: */
096: <T> AnnotatedBindingBuilder<T> bind(Class<T> type);
097:
098: /**
099: * Binds a constant value to an annotation.
100: */
101: AnnotatedConstantBindingBuilder bindConstant();
102:
103: /**
104: * Upon successful creation, the {@link Injector} will inject static fields
105: * and methods in the given classes.
106: *
107: * @param types for which static members will be injected
108: */
109: void requestStaticInjection(Class<?>... types);
110:
111: /**
112: * Uses the given module to configure more bindings.
113: */
114: void install(Module module);
115:
116: /**
117: * Gets the current stage.
118: */
119: Stage currentStage();
120:
121: /**
122: * Records an error message which will be presented to the user at a later
123: * time. Unlike throwing an exception, this enable us to continue
124: * configuring the Injector and discover more errors. Uses {@link
125: * String#format(String, Object[])} to insert the arguments into the
126: * message.
127: */
128: void addError(String message, Object... arguments);
129:
130: /**
131: * Records an exception, the full details of which will be logged, and the
132: * message of which will be presented to the user at a later
133: * time. If your Module calls something that you worry may fail, you should
134: * catch the exception and pass it into this.
135: */
136: void addError(Throwable t);
137: }
|