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