blob: d0535f2d8aa32a59bcc35d32fe6b64f851513827 [file] [log] [blame]
crazyboblee66b415a2006-08-25 02:01:19 +00001/**
2 * Copyright (C) 2006 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.inject;
18
kevinb9ne1481022007-04-29 18:34:59 +000019import java.lang.annotation.Documented;
kevinb9na99dca72007-02-11 04:48:57 +000020import static java.lang.annotation.ElementType.CONSTRUCTOR;
21import static java.lang.annotation.ElementType.FIELD;
22import static java.lang.annotation.ElementType.METHOD;
crazyboblee66b415a2006-08-25 02:01:19 +000023import java.lang.annotation.Retention;
kevinb9na99dca72007-02-11 04:48:57 +000024import static java.lang.annotation.RetentionPolicy.RUNTIME;
crazyboblee66b415a2006-08-25 02:01:19 +000025import java.lang.annotation.Target;
26
27/**
crazyboblee0bfdbc62007-02-20 21:47:19 +000028 * Annotates members of your implementation class (constructors, methods
crazyboblee61257a82007-03-03 00:23:40 +000029 * and fields) into which the {@link Injector} should inject values.
30 * The Injector fulfills injection requests for:
kevinb9n2a7178f2007-02-20 07:32:27 +000031 *
32 * <ul>
crazyboblee0bfdbc62007-02-20 21:47:19 +000033 * <li>Every instance it constructs. The class being constructed must have
34 * exactly one of its constructors marked with {@code @Inject} or must have a
kevinb9na2915a92007-02-28 06:20:30 +000035 * constructor taking no parameters. The Injector then proceeds to perform
crazyboblee0bfdbc62007-02-20 21:47:19 +000036 * method and field injections.
37 *
crazyboblee61257a82007-03-03 00:23:40 +000038 * <li>Pre-constructed instances passed to {@link Injector#injectMembers},
39 * {@link com.google.inject.binder.LinkedBindingBuilder#toInstance(Object)} and
40 * {@link com.google.inject.binder.LinkedBindingBuilder#toProvider(Provider)}.
kevinb9na2915a92007-02-28 06:20:30 +000041 * In this case all constructors are, of course, ignored.
kevinb9n2a7178f2007-02-20 07:32:27 +000042 *
crazyboblee0bfdbc62007-02-20 21:47:19 +000043 * <li>Static fields and methods of classes which any {@link Module} has
44 * specifically requested static injection for, using
45 * {@link Binder#requestStaticInjection}.
kevinb9n2a7178f2007-02-20 07:32:27 +000046 * </ul>
47 *
48 * In all cases, a member can be injected regardless of its Java access
49 * specifier (private, default, protected, public).
crazyboblee4602a6f2007-02-15 02:45:18 +000050 *
crazyboblee66b415a2006-08-25 02:01:19 +000051 * @author crazybob@google.com (Bob Lee)
52 */
crazyboblee278ee4d2007-02-15 19:23:13 +000053@Target({ METHOD, CONSTRUCTOR, FIELD })
crazyboblee66b415a2006-08-25 02:01:19 +000054@Retention(RUNTIME)
kevinb9ne1481022007-04-29 18:34:59 +000055@Documented
crazyboblee66b415a2006-08-25 02:01:19 +000056public @interface Inject {
57
58 /**
kevinb9na2915a92007-02-28 06:20:30 +000059 * If true, and the appropriate binding is not found,
60 * the Injector will skip injection of this method or field rather than
crazyboblee0bfdbc62007-02-20 21:47:19 +000061 * produce an error. When applied to a field, any default value already
kevinb9n2a7178f2007-02-20 07:32:27 +000062 * assigned to the field will remain (guice will not actively null out the
crazyboblee0bfdbc62007-02-20 21:47:19 +000063 * field). When applied to a method, the method will only be invoked if
64 * bindings for <i>all</i> parameters are found. When applied to a
kevinb9na2915a92007-02-28 06:20:30 +000065 * constructor, an error will result upon Injector creation.
crazyboblee66b415a2006-08-25 02:01:19 +000066 */
crazyboblee4602a6f2007-02-15 02:45:18 +000067 boolean optional() default false;
crazyboblee66b415a2006-08-25 02:01:19 +000068}