• 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 java.util.Set;
24 
25 /**
26  * A request to inject the static fields and methods of a type. Requests are created explicitly in a
27  * module using {@link com.google.inject.Binder#requestStaticInjection(Class[])
28  * requestStaticInjection()} statements:
29  *
30  * <pre>
31  *     requestStaticInjection(MyLegacyService.class);</pre>
32  *
33  * @author jessewilson@google.com (Jesse Wilson)
34  * @since 2.0
35  */
36 public final class StaticInjectionRequest implements Element {
37   private final Object source;
38   private final Class<?> type;
39 
StaticInjectionRequest(Object source, Class<?> type)40   StaticInjectionRequest(Object source, Class<?> type) {
41     this.source = checkNotNull(source, "source");
42     this.type = checkNotNull(type, "type");
43   }
44 
45   @Override
getSource()46   public Object getSource() {
47     return source;
48   }
49 
getType()50   public Class<?> getType() {
51     return type;
52   }
53 
54   /**
55    * Returns the static methods and fields of {@code type} that will be injected to fulfill this
56    * request.
57    *
58    * @return a possibly empty set of injection points. The set has a specified iteration order. All
59    *     fields are returned and then all methods. Within the fields, supertype fields are returned
60    *     before subtype fields. Similarly, supertype methods are returned before subtype methods.
61    * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as
62    *     a field with multiple binding annotations. The exception's {@link
63    *     ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} of
64    *     the valid injection points.
65    */
getInjectionPoints()66   public Set<InjectionPoint> getInjectionPoints() throws ConfigurationException {
67     return InjectionPoint.forStaticMethodsAndFields(type);
68   }
69 
70   @Override
applyTo(Binder binder)71   public void applyTo(Binder binder) {
72     binder.withSource(getSource()).requestStaticInjection(type);
73   }
74 
75   @Override
acceptVisitor(ElementVisitor<T> visitor)76   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
77     return visitor.visit(this);
78   }
79 }
80