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 java.util.List; 020 import java.util.Map; 021 022 import org.apache.commons.inject.api.IBinding; 023 import org.apache.commons.inject.api.Key; 024 import org.apache.commons.inject.api.NoSuchBindingException; 025 026 public class ResolvableBindingSet extends AbstractBindingSet { 027 public ResolvableBindingSet(MutableBindingSet pMutableBindings) { 028 super(pMutableBindings.map); 029 } 030 031 public void resolve() { 032 for (Map.Entry<ReducedKey<?>, List<BindingAndKey<?>>> en : map.entrySet()) { 033 List<BindingAndKey<?>> list = en.getValue(); 034 for (BindingAndKey<?> bak : list) { 035 final IBinding<?> binding = bak.getBinding(); 036 if (binding instanceof BindingProxy) { 037 @SuppressWarnings("unchecked") 038 final BindingProxy<Object> bnd = (BindingProxy<Object>) binding; 039 if (bnd.isResolvedLater()) { 040 continue; 041 } 042 @SuppressWarnings("unchecked") 043 final IBinding<Object> realBinding = (IBinding<Object>) findRealBinding(list, bak.getKey()); 044 if (realBinding == null) { 045 throw new NoSuchBindingException("No Binding has been registered for key " 046 + Key.toString(bak.getKey()) + ". " + ((BindingProxy<?>) binding).getCause()); 047 } 048 bnd.setBinding(realBinding); 049 } 050 } 051 } 052 } 053 054 private IBinding<?> findRealBinding(List<BindingAndKey<?>> pList, 055 MappedKey<?> pKey) { 056 for (BindingAndKey<?> bak : pList) { 057 if (bak.getKey() == pKey) { 058 continue; 059 } 060 if (!isMatching(bak.getKey(), pKey)) { 061 continue; 062 } 063 return bak.getBinding(); 064 } 065 return null; 066 } 067 }