001 /*******************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved.
003 * ---------------------------------------------------------------------------
004 * The software in this package is published under the terms of the BSD style
005 * license a copy of which has been included with this distribution in the
006 * LICENSE.txt file.
007 ******************************************************************************/
008 package org.picocontainer.parameters;
009
010 import java.io.Serializable;
011 import java.lang.annotation.Annotation;
012 import java.lang.reflect.Constructor;
013 import java.lang.reflect.Type;
014
015 import org.picocontainer.ComponentAdapter;
016 import org.picocontainer.NameBinding;
017 import org.picocontainer.Parameter;
018 import org.picocontainer.PicoContainer;
019 import org.picocontainer.PicoVisitor;
020
021 /**
022 * Part of the replacement construct for Parameter.ZERO
023 * @since PicoContainer 2.8
024 */
025 @SuppressWarnings("serial")
026 public final class DefaultConstructorParameter extends AbstractParameter implements Parameter, Serializable {
027
028 /**
029 * The one and only instance
030 */
031 public static final DefaultConstructorParameter INSTANCE = new DefaultConstructorParameter();
032
033 /**
034 * No instantiation
035 */
036 public void accept(PicoVisitor visitor) {
037 visitor.visitParameter(this);
038 }
039
040 public Resolver resolve(PicoContainer container,
041 ComponentAdapter<?> forAdapter, ComponentAdapter<?> injecteeAdapter, Type expectedType,
042 NameBinding expectedNameBinding, boolean useNames,
043 Annotation binding) {
044 return new Parameter.NotResolved();
045 }
046
047 public void verify(PicoContainer container,
048 ComponentAdapter<?> adapter, Type expectedType,
049 NameBinding expectedNameBinding, boolean useNames,
050 Annotation binding) {
051
052 if (!(expectedType instanceof Class)) {
053 throw new ClassCastException("Unable to use except for class types. Offending type: " + expectedType);
054 }
055
056 Class<?> type = (Class<?>)expectedType;
057 try {
058 Constructor constructor = type.getConstructor();
059 } catch (NoSuchMethodException e) {
060 throw new IllegalArgumentException("No default constructor for type " + expectedType,e );
061 }
062 }
063
064 @Override
065 public String toString() {
066 return "Force Default Constructor Parameter";
067 }
068
069 /**
070 * Returns true if the object object is a DEFAULT_CONSTRUCTOR object.
071 * {@inheritDoc}
072 * @see java.lang.Object#equals(java.lang.Object)
073 */
074 @Override
075 public boolean equals(Object other) {
076 if (other == null) {
077 return false;
078 }
079
080 return (other.getClass().getName()).equals(getClass().getName());
081 }
082 }