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 Jon Tirsen *
009: *****************************************************************************/package org.picocontainer.parameters;
010:
011: import org.picocontainer.ComponentAdapter;
012: import org.picocontainer.Parameter;
013: import org.picocontainer.PicoContainer;
014: import org.picocontainer.PicoException;
015: import org.picocontainer.PicoCompositionException;
016: import org.picocontainer.PicoVisitor;
017: import org.picocontainer.NameBinding;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.Field;
021: import java.lang.annotation.Annotation;
022:
023: /**
024: * A ConstantParameter should be used to pass in "constant" arguments to constructors. This
025: * includes {@link String}s,{@link Integer}s or any other object that is not registered in
026: * the container.
027: *
028: * @author Jon Tirsén
029: * @author Aslak Hellesøy
030: * @author Jörg Schaible
031: * @author Thomas Heller
032: */
033: public class ConstantParameter implements Parameter, Serializable {
034:
035: private final Object value;
036:
037: public ConstantParameter(Object value) {
038: this .value = value;
039: }
040:
041: public Object resolveInstance(PicoContainer container,
042: ComponentAdapter adapter, Class expectedType,
043: NameBinding expectedNameBinding, boolean useNames,
044: Annotation binding) {
045: return value;
046: }
047:
048: public boolean isResolvable(PicoContainer container,
049: ComponentAdapter adapter, Class expectedType,
050: NameBinding expectedNameBinding, boolean useNames,
051: Annotation binding) {
052: try {
053: verify(container, adapter, expectedType,
054: expectedNameBinding, useNames, binding);
055: return true;
056: } catch (final PicoCompositionException e) {
057: return false;
058: }
059: }
060:
061: /**
062: * {@inheritDoc}
063: *
064: * @see Parameter#verify(PicoContainer, ComponentAdapter, Class, NameBinding ,boolean, Annotation)
065: */
066: public void verify(PicoContainer container,
067: ComponentAdapter adapter, Class expectedType,
068: NameBinding expectedNameBinding, boolean useNames,
069: Annotation binding) throws PicoException {
070: if (!checkPrimitive(expectedType)
071: && !expectedType.isInstance(value)) {
072: throw new PicoCompositionException(expectedType.getClass()
073: .getName()
074: + " is not assignable from "
075: + (value != null ? value.getClass().getName()
076: : "null"));
077: }
078: }
079:
080: /**
081: * Visit the current {@link Parameter}.
082: *
083: * @see org.picocontainer.Parameter#accept(org.picocontainer.PicoVisitor)
084: */
085: public void accept(final PicoVisitor visitor) {
086: visitor.visitParameter(this );
087: }
088:
089: private boolean checkPrimitive(Class expectedType) {
090: try {
091: if (expectedType.isPrimitive()) {
092: final Field field = value.getClass().getField("TYPE");
093: final Class type = (Class) field.get(value);
094: return expectedType.isAssignableFrom(type);
095: }
096: } catch (NoSuchFieldException e) {
097: //ignore
098: } catch (IllegalAccessException e) {
099: //ignore
100: }
101: return false;
102: }
103:
104: }
|