Coverage Report - org.apache.commons.inject.impl.AbstractInjector
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractInjector
66%
16/24
50%
3/6
0
 
 1  
 /**
 2  
  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  contributor license agreements.  See the NOTICE file distributed with
 4  
  this work for additional information regarding copyright ownership.
 5  
  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  (the "License"); you may not use this file except in compliance with
 7  
  the License.  You may obtain a copy of the License at
 8  
 
 9  
       http://www.apache.org/licenses/LICENSE-2.0
 10  
 
 11  
  Unless required by applicable law or agreed to in writing, software
 12  
  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  See the License for the specific language governing permissions and
 15  
  limitations under the License.
 16  
 */
 17  
 package org.apache.commons.inject.impl;
 18  
 
 19  
 import org.apache.commons.inject.api.IBinding;
 20  
 import org.apache.commons.inject.api.IInjector;
 21  
 import org.apache.commons.inject.api.IKey;
 22  
 import org.apache.commons.inject.api.Key;
 23  
 import org.apache.commons.inject.api.NoSuchBindingException;
 24  
 
 25  
 /**
 26  
  * Abstract implementation of an {@link IInjector injector}.
 27  
  */
 28  9
 public abstract class AbstractInjector implements IInjector {
 29  
         protected abstract <T> IBinding<T> getBinding(IKey<T> pKey);
 30  
         protected abstract <T> IBinding<T> requireBinding(IKey<T> pKey);
 31  
 
 32  
         @Override
 33  
         public <T> T getInstance(Class<T> pType) {
 34  1
                 final IKey<T> key = new Key<T>(pType);
 35  1
                 return getInstance(key);
 36  
         }
 37  
 
 38  
         @Override
 39  
         public <T> T getInstance(Class<T> pType, String pName) {
 40  4
                 final IKey<T> key = new Key<T>(pType, pName);
 41  4
                 return getInstance(key);
 42  
         }
 43  
 
 44  
         @Override
 45  
         public <T> T getInstance(IKey<T> pKey) {
 46  5
                 final IBinding<T> binding = getBinding(pKey);
 47  5
                 if (binding == null) {
 48  0
                         return null;
 49  
                 } else {
 50  5
                         return binding.getProvider().get();
 51  
                 }
 52  
         }
 53  
 
 54  
         @Override
 55  
         public <T> T requireInstance(Class<T> pType) throws NoSuchBindingException {
 56  6
                 final IKey<T> key = new Key<T>(pType);
 57  6
                 return requireInstance(key);
 58  
         }
 59  
 
 60  
         @Override
 61  
         public <T> T requireInstance(Class<T> pType, String pName)
 62  
                         throws NoSuchBindingException {
 63  15
                 final IKey<T> key = new Key<T>(pType, pName);
 64  15
                 return requireInstance(key);
 65  
         }
 66  
 
 67  
         @Override
 68  
         public <T> T requireInstance(IKey<T> pKey) throws NoSuchBindingException {
 69  22
                 final IBinding<T> binding = getBinding(pKey);
 70  22
                 if (binding == null) {
 71  3
                         throw new NoSuchBindingException("No binding registered for key: " + pKey);
 72  
                 } else {
 73  19
                         return binding.getProvider().get();
 74  
                 }
 75  
         }
 76  
 
 77  
         @Override
 78  
         public void injectMembers(Object pInstance) {
 79  0
                 if (pInstance == null) {
 80  0
                         throw new NullPointerException("The instance must not be null.");
 81  
                 }
 82  
                 @SuppressWarnings("unchecked")
 83  0
                 final Class<Object> cl = (Class<Object>) pInstance.getClass();
 84  0
                 final IKey<Object> key = new Key<Object>(cl);
 85  0
                 IBinding<Object> binding = requireBinding(key);
 86  0
                 binding.getPoint().injectTo(pInstance, this);
 87  0
         }
 88  
 }