1 /* 2 * Copyright (C) 2008 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.spi; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.inject.Binder; 22 import com.google.inject.ConfigurationException; 23 import com.google.inject.TypeLiteral; 24 import java.util.Set; 25 26 /** 27 * A request to inject the instance fields and methods of an instance. Requests are created 28 * explicitly in a module using {@link com.google.inject.Binder#requestInjection(Object) 29 * requestInjection()} statements: 30 * 31 * <pre> 32 * requestInjection(serviceInstance);</pre> 33 * 34 * @author mikeward@google.com (Mike Ward) 35 * @since 2.0 36 */ 37 public final class InjectionRequest<T> implements Element { 38 39 private final Object source; 40 private final TypeLiteral<T> type; 41 private final T instance; 42 InjectionRequest(Object source, TypeLiteral<T> type, T instance)43 public InjectionRequest(Object source, TypeLiteral<T> type, T instance) { 44 this.source = checkNotNull(source, "source"); 45 this.type = checkNotNull(type, "type"); 46 this.instance = checkNotNull(instance, "instance"); 47 } 48 49 @Override getSource()50 public Object getSource() { 51 return source; 52 } 53 getInstance()54 public T getInstance() { 55 return instance; 56 } 57 getType()58 public TypeLiteral<T> getType() { 59 return type; 60 } 61 62 /** 63 * Returns the instance methods and fields of {@code instance} that will be injected to fulfill 64 * this request. 65 * 66 * @return a possibly empty set of injection points. The set has a specified iteration order. All 67 * fields are returned and then all methods. Within the fields, supertype fields are returned 68 * before subtype fields. Similarly, supertype methods are returned before subtype methods. 69 * @throws ConfigurationException if there is a malformed injection point on the class of {@code 70 * instance}, such as a field with multiple binding annotations. The exception's {@link 71 * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} of 72 * the valid injection points. 73 */ getInjectionPoints()74 public Set<InjectionPoint> getInjectionPoints() throws ConfigurationException { 75 return InjectionPoint.forInstanceMethodsAndFields(instance.getClass()); 76 } 77 78 @Override acceptVisitor(ElementVisitor<R> visitor)79 public <R> R acceptVisitor(ElementVisitor<R> visitor) { 80 return visitor.visit(this); 81 } 82 83 @Override applyTo(Binder binder)84 public void applyTo(Binder binder) { 85 binder.withSource(getSource()).requestInjection(type, instance); 86 } 87 } 88