001 /** 002 Licensed to the Apache Software Foundation (ASF) under one or more 003 contributor license agreements. See the NOTICE file distributed with 004 this work for additional information regarding copyright ownership. 005 The ASF licenses this file to You under the Apache License, Version 2.0 006 (the "License"); you may not use this file except in compliance with 007 the License. You may obtain a copy of the License at 008 009 http://www.apache.org/licenses/LICENSE-2.0 010 011 Unless required by applicable law or agreed to in writing, software 012 distributed under the License is distributed on an "AS IS" BASIS, 013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 See the License for the specific language governing permissions and 015 limitations under the License. 016 */ 017 package org.apache.commons.inject.impl; 018 019 import org.apache.commons.inject.api.IBinding; 020 import org.apache.commons.inject.api.IInjector; 021 import org.apache.commons.inject.api.IKey; 022 import org.apache.commons.inject.api.Key; 023 import org.apache.commons.inject.api.NoSuchBindingException; 024 025 /** 026 * Abstract implementation of an {@link IInjector injector}. 027 */ 028 public abstract class AbstractInjector implements IInjector { 029 protected abstract <T> IBinding<T> getBinding(IKey<T> pKey); 030 protected abstract <T> IBinding<T> requireBinding(IKey<T> pKey); 031 032 @Override 033 public <T> T getInstance(Class<T> pType) { 034 final IKey<T> key = new Key<T>(pType); 035 return getInstance(key); 036 } 037 038 @Override 039 public <T> T getInstance(Class<T> pType, String pName) { 040 final IKey<T> key = new Key<T>(pType, pName); 041 return getInstance(key); 042 } 043 044 @Override 045 public <T> T getInstance(IKey<T> pKey) { 046 final IBinding<T> binding = getBinding(pKey); 047 if (binding == null) { 048 return null; 049 } else { 050 return binding.getProvider().get(); 051 } 052 } 053 054 @Override 055 public <T> T requireInstance(Class<T> pType) throws NoSuchBindingException { 056 final IKey<T> key = new Key<T>(pType); 057 return requireInstance(key); 058 } 059 060 @Override 061 public <T> T requireInstance(Class<T> pType, String pName) 062 throws NoSuchBindingException { 063 final IKey<T> key = new Key<T>(pType, pName); 064 return requireInstance(key); 065 } 066 067 @Override 068 public <T> T requireInstance(IKey<T> pKey) throws NoSuchBindingException { 069 final IBinding<T> binding = getBinding(pKey); 070 if (binding == null) { 071 throw new NoSuchBindingException("No binding registered for key: " + pKey); 072 } else { 073 return binding.getProvider().get(); 074 } 075 } 076 077 @Override 078 public void injectMembers(Object pInstance) { 079 if (pInstance == null) { 080 throw new NullPointerException("The instance must not be null."); 081 } 082 @SuppressWarnings("unchecked") 083 final Class<Object> cl = (Class<Object>) pInstance.getClass(); 084 final IKey<Object> key = new Key<Object>(cl); 085 IBinding<Object> binding = requireBinding(key); 086 binding.getPoint().injectTo(pInstance, this); 087 } 088 }