crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
kevinb9n | e148102 | 2007-04-29 18:34:59 +0000 | [diff] [blame^] | 19 | import java.lang.annotation.Documented; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 20 | import static java.lang.annotation.ElementType.CONSTRUCTOR; |
| 21 | import static java.lang.annotation.ElementType.FIELD; |
| 22 | import static java.lang.annotation.ElementType.METHOD; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 23 | import java.lang.annotation.Retention; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 24 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 25 | import java.lang.annotation.Target; |
| 26 | |
| 27 | /** |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 28 | * Annotates members of your implementation class (constructors, methods |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 29 | * and fields) into which the {@link Injector} should inject values. |
| 30 | * The Injector fulfills injection requests for: |
kevinb9n | 2a7178f | 2007-02-20 07:32:27 +0000 | [diff] [blame] | 31 | * |
| 32 | * <ul> |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 33 | * <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 |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 35 | * constructor taking no parameters. The Injector then proceeds to perform |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 36 | * method and field injections. |
| 37 | * |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 38 | * <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)}. |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 41 | * In this case all constructors are, of course, ignored. |
kevinb9n | 2a7178f | 2007-02-20 07:32:27 +0000 | [diff] [blame] | 42 | * |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 43 | * <li>Static fields and methods of classes which any {@link Module} has |
| 44 | * specifically requested static injection for, using |
| 45 | * {@link Binder#requestStaticInjection}. |
kevinb9n | 2a7178f | 2007-02-20 07:32:27 +0000 | [diff] [blame] | 46 | * </ul> |
| 47 | * |
| 48 | * In all cases, a member can be injected regardless of its Java access |
| 49 | * specifier (private, default, protected, public). |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 50 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 51 | * @author crazybob@google.com (Bob Lee) |
| 52 | */ |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 53 | @Target({ METHOD, CONSTRUCTOR, FIELD }) |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 54 | @Retention(RUNTIME) |
kevinb9n | e148102 | 2007-04-29 18:34:59 +0000 | [diff] [blame^] | 55 | @Documented |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 56 | public @interface Inject { |
| 57 | |
| 58 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 59 | * If true, and the appropriate binding is not found, |
| 60 | * the Injector will skip injection of this method or field rather than |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 61 | * produce an error. When applied to a field, any default value already |
kevinb9n | 2a7178f | 2007-02-20 07:32:27 +0000 | [diff] [blame] | 62 | * assigned to the field will remain (guice will not actively null out the |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 63 | * 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 |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 65 | * constructor, an error will result upon Injector creation. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 66 | */ |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 67 | boolean optional() default false; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 68 | } |