Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2014 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 package dagger.internal;
     17 
     18 import dagger.MembersInjector;
     19 import javax.inject.Inject;
     20 
     21 /**
     22  * Basic {@link MembersInjector} implementations used by the framework.
     23  *
     24  * @author Gregory Kick
     25  * @since 2.0
     26  */
     27 public final class MembersInjectors {
     28   /**
     29    * Returns a {@link MembersInjector} implementation that injects no members
     30    *
     31    * <p>Note that there is no verification that the type being injected does not have {@link Inject}
     32    * members, so care should be taken to ensure appropriate use.
     33    */
     34   @SuppressWarnings("unchecked")
     35   public static <T> MembersInjector<T> noOp() {
     36     return (MembersInjector<T>) NoOpMembersInjector.INSTANCE;
     37   }
     38 
     39   private static enum NoOpMembersInjector implements MembersInjector<Object> {
     40     INSTANCE;
     41 
     42     @Override public void injectMembers(Object instance) {
     43       if (instance == null) {
     44         throw new NullPointerException();
     45       }
     46     }
     47   }
     48 
     49   /**
     50    * Returns a {@link MembersInjector} that delegates to the {@link MembersInjector} of its
     51    * supertype.  This is useful for cases where a type is known not to have its own {@link Inject}
     52    * members, but must still inject members on its supertype(s).
     53    *
     54    * <p>Note that there is no verification that the type being injected does not have {@link Inject}
     55    * members, so care should be taken to ensure appropriate use.
     56    */
     57   @SuppressWarnings("unchecked")
     58   public static <T> MembersInjector<T> delegatingTo(MembersInjector<? super T> delegate) {
     59     return (MembersInjector<T>) delegate;
     60   }
     61 
     62   private MembersInjectors() {}
     63 }
     64