1477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit/** 2477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * Copyright (C) 2008 Google Inc. 3477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * 4477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * Licensed under the Apache License, Version 2.0 (the "License"); 5477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * you may not use this file except in compliance with the License. 6477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * You may obtain a copy of the License at 7477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * 8477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * http://www.apache.org/licenses/LICENSE-2.0 9477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * 10477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * Unless required by applicable law or agreed to in writing, software 11477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * distributed under the License is distributed on an "AS IS" BASIS, 12477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * See the License for the specific language governing permissions and 14477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * limitations under the License. 15477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit */ 16477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 17477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkitpackage com.google.inject.spi; 18477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 19b7a02b02d81c830d148355c90bc309bcd66fb592sberlinimport static com.google.common.base.Preconditions.checkNotNull; 20b7a02b02d81c830d148355c90bc309bcd66fb592sberlin 2103b81a6b931a06c7697e422b218e3734a7f262cclimpbizkitimport com.google.inject.Binder; 229a227bef3b82a045323ef2cf38ec60b2e42cf0felimpbizkit@gmail.comimport com.google.inject.ConfigurationException; 23b7a02b02d81c830d148355c90bc309bcd66fb592sberlin 24c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkitimport java.util.Set; 25477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 26477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit/** 27c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * A request to inject the static fields and methods of a type. Requests are created 2800ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkit * explicitly in a module using {@link com.google.inject.Binder#requestStaticInjection(Class[]) 2900ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkit * requestStaticInjection()} statements: 3000ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkit * <pre> 3100ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkit * requestStaticInjection(MyLegacyService.class);</pre> 3200ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkit * 33477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit * @author jessewilson@google.com (Jesse Wilson) 34c489adf4671b41765698d167e13960d998190c5elimpbizkit * @since 2.0 35477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit */ 3600ca9f749aa80383ee7d85ad25e1535a79fe5718limpbizkitpublic final class StaticInjectionRequest implements Element { 37477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit private final Object source; 38b3a8f0bda3259af41e639f1872c42377989704d9limpbizkit private final Class<?> type; 39477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 40b3a8f0bda3259af41e639f1872c42377989704d9limpbizkit StaticInjectionRequest(Object source, Class<?> type) { 41477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit this.source = checkNotNull(source, "source"); 42b3a8f0bda3259af41e639f1872c42377989704d9limpbizkit this.type = checkNotNull(type, "type"); 43477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit } 44477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 45477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit public Object getSource() { 46477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit return source; 47477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit } 48477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 49b3a8f0bda3259af41e639f1872c42377989704d9limpbizkit public Class<?> getType() { 50b3a8f0bda3259af41e639f1872c42377989704d9limpbizkit return type; 51477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit } 52477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit 53c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit /** 54c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * Returns the static methods and fields of {@code type} that will be injected to fulfill this 55c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * request. 56c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * 57c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * @return a possibly empty set of injection points. The set has a specified iteration order. All 58c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * fields are returned and then all methods. Within the fields, supertype fields are returned 59c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * before subtype fields. Similarly, supertype methods are returned before subtype methods. 60c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as 61c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * a field with multiple binding annotations. The exception's {@link 62c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} 63c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit * of the valid injection points. 64c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit */ 65c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit public Set<InjectionPoint> getInjectionPoints() throws ConfigurationException { 66c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit return InjectionPoint.forStaticMethodsAndFields(type); 67c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit } 68c45600e563daf23ed3bf4a2881cdf1b7771e9ddelimpbizkit 6903b81a6b931a06c7697e422b218e3734a7f262cclimpbizkit public void applyTo(Binder binder) { 7003b81a6b931a06c7697e422b218e3734a7f262cclimpbizkit binder.withSource(getSource()).requestStaticInjection(type); 7103b81a6b931a06c7697e422b218e3734a7f262cclimpbizkit } 7203b81a6b931a06c7697e422b218e3734a7f262cclimpbizkit 73afa4b5dd2056a8427657186d1804313c3f34fbfelimpbizkit public <T> T acceptVisitor(ElementVisitor<T> visitor) { 7403b81a6b931a06c7697e422b218e3734a7f262cclimpbizkit return visitor.visit(this); 75477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit } 76477f9f9ce3e1077866b579e99cd33ab824f1ee69limpbizkit} 77