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