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 19 import static java.lang.annotation.ElementType.CONSTRUCTOR; 20 import static java.lang.annotation.ElementType.FIELD; 21 import static java.lang.annotation.ElementType.METHOD; 22 import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 24 import java.lang.annotation.Documented; 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.Target; 27 28 /** 29 * Annotates members of your implementation class (constructors, methods 30 * and fields) into which the {@link Injector} should inject values. 31 * The Injector fulfills injection requests for: 32 * 33 * <ul> 34 * <li>Every instance it constructs. The class being constructed must have 35 * exactly one of its constructors marked with {@code @Inject} or must have a 36 * constructor taking no parameters. The Injector then proceeds to perform 37 * field and method injections. 38 * 39 * <li>Pre-constructed instances passed to {@link Injector#injectMembers}, 40 * {@link com.google.inject.binder.LinkedBindingBuilder#toInstance(Object)} and 41 * {@link com.google.inject.binder.LinkedBindingBuilder#toProvider(javax.inject.Provider)}. 42 * In this case all constructors are, of course, ignored. 43 * 44 * <li>Static fields and methods of classes which any {@link Module} has 45 * specifically requested static injection for, using 46 * {@link Binder#requestStaticInjection}. 47 * </ul> 48 * 49 * In all cases, a member can be injected regardless of its Java access 50 * specifier (private, default, protected, public). 51 * 52 * @author crazybob@google.com (Bob Lee) 53 */ 54 @Target({ METHOD, CONSTRUCTOR, FIELD }) 55 @Retention(RUNTIME) 56 @Documented 57 public @interface Inject { 58 59 /** 60 * If true, and the appropriate binding is not found, 61 * the Injector will skip injection of this method or field rather than 62 * produce an error. When applied to a field, any default value already 63 * assigned to the field will remain (guice will not actively null out the 64 * field). When applied to a method, the method will only be invoked if 65 * bindings for <i>all</i> parameters are found. When applied to a 66 * constructor, an error will result upon Injector creation. 67 */ optional()68 boolean optional() default false; 69 } 70