001 /***************************************************************************** 002 * Copyright (c) PicoContainer Organization. All rights reserved. * 003 * ------------------------------------------------------------------------- * 004 * The software in this package is published under the terms of the BSD * 005 * style license a copy of which has been included with this distribution in * 006 * the LICENSE.txt file. * 007 * * 008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant * 009 *****************************************************************************/ 010 package org.picocontainer.defaults; 011 012 import org.picocontainer.PicoIntrospectionException; 013 014 import java.util.Arrays; 015 016 /** 017 * Exception that is thrown as part of the introspection. Raised if a PicoContainer cannot resolve a 018 * type dependency because the registered {@link org.picocontainer.ComponentAdapter}s are not 019 * distinct. 020 * 021 * @author Paul Hammant 022 * @author Aslak Hellesøy 023 * @author Jon Tirsén 024 * @since 1.0 025 */ 026 public class AmbiguousComponentResolutionException extends PicoIntrospectionException { 027 private Class component; 028 private Class ambiguousDependency; 029 private final Object[] ambiguousComponentKeys; 030 031 032 /** 033 * Construct a new exception with the ambigous class type and the ambiguous component keys. 034 * 035 * @param ambiguousDependency the unresolved dependency type 036 * @param componentKeys the ambiguous keys. 037 */ 038 public AmbiguousComponentResolutionException(Class ambiguousDependency, Object[] componentKeys) { 039 super(""); 040 this.ambiguousDependency = ambiguousDependency; 041 this.ambiguousComponentKeys = new Class[componentKeys.length]; 042 for (int i = 0; i < componentKeys.length; i++) { 043 ambiguousComponentKeys[i] = componentKeys[i]; 044 } 045 } 046 047 /** 048 * @return Returns a string containing the unresolved class type and the ambiguous keys. 049 */ 050 public String getMessage() { 051 StringBuffer msg = new StringBuffer(); 052 msg.append(component); 053 msg.append(" has ambiguous dependency on "); 054 msg.append(ambiguousDependency); 055 msg.append(", "); 056 msg.append("resolves to multiple classes: "); 057 msg.append(Arrays.asList(getAmbiguousComponentKeys())); 058 return msg.toString(); 059 } 060 061 /** 062 * @return Returns the ambiguous component keys as array. 063 */ 064 public Object[] getAmbiguousComponentKeys() { 065 return ambiguousComponentKeys; 066 } 067 068 public void setComponent(Class component) { 069 this.component = component; 070 } 071 }