• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 import java.util.Set;
26 
27 /**
28  * A request to inject the instance fields and methods of an instance. Requests are created
29  * explicitly in a module using {@link com.google.inject.Binder#requestInjection(Object)
30  * requestInjection()} statements:
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 
getSource()49   public Object getSource() {
50     return source;
51   }
52 
getInstance()53   public T getInstance() {
54     return instance;
55   }
56 
getType()57   public TypeLiteral<T> getType() {
58     return type;
59   }
60 
61   /**
62    * Returns the instance methods and fields of {@code instance} that will be injected to fulfill
63    * this request.
64    *
65    * @return a possibly empty set of injection points. The set has a specified iteration order. All
66    *      fields are returned and then all methods. Within the fields, supertype fields are returned
67    *      before subtype fields. Similarly, supertype methods are returned before subtype methods.
68    * @throws ConfigurationException if there is a malformed injection point on the class of {@code
69    *      instance}, such as a field with multiple binding annotations. The exception's {@link
70    *      ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>}
71    *      of the valid injection points.
72    */
getInjectionPoints()73   public Set<InjectionPoint> getInjectionPoints() throws ConfigurationException {
74     return InjectionPoint.forInstanceMethodsAndFields(instance.getClass());
75   }
76 
acceptVisitor(ElementVisitor<R> visitor)77   public <R> R acceptVisitor(ElementVisitor<R> visitor) {
78     return visitor.visit(this);
79   }
80 
applyTo(Binder binder)81   public void applyTo(Binder binder) {
82     binder.withSource(getSource()).requestInjection(type, instance);
83   }
84 }
85