KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > ConstantParameter


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by Jon Tirsen *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import org.picocontainer.ComponentAdapter;
14 import org.picocontainer.Parameter;
15 import org.picocontainer.PicoContainer;
16 import org.picocontainer.PicoException;
17 import org.picocontainer.PicoIntrospectionException;
18 import org.picocontainer.PicoVisitor;
19
20 import java.io.Serializable JavaDoc;
21 import java.lang.reflect.Field JavaDoc;
22
23
24 /**
25  * A ConstantParameter should be used to pass in "constant" arguments to constructors. This
26  * includes {@link String}s,{@link Integer}s or any other object that is not registered in
27  * the container.
28  *
29  * @author Jon Tirsén
30  * @author Aslak Hellesøy
31  * @author Jörg Schaible
32  * @author Thomas Heller
33  * @version $Revision: 1801 $
34  */

35 public class ConstantParameter
36         implements Parameter, Serializable JavaDoc {
37
38     private final Object JavaDoc value;
39
40     public ConstantParameter(Object JavaDoc value) {
41         this.value = value;
42     }
43
44     public Object JavaDoc resolveInstance(PicoContainer container, ComponentAdapter adapter, Class JavaDoc expectedType) {
45         return value;
46     }
47
48     public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class JavaDoc expectedType) {
49         try {
50             verify(container, adapter, expectedType);
51             return true;
52         } catch(final PicoIntrospectionException e) {
53             return false;
54         }
55     }
56     
57     /**
58      * {@inheritDoc}
59      *
60      * @see org.picocontainer.Parameter#verify(org.picocontainer.PicoContainer,
61      * org.picocontainer.ComponentAdapter, java.lang.Class)
62      */

63     public void verify(PicoContainer container, ComponentAdapter adapter, Class JavaDoc expectedType) throws PicoException {
64         if (!checkPrimitive(expectedType) && !expectedType.isInstance(value)) {
65             throw new PicoIntrospectionException(expectedType.getClass().getName()
66                     + " is not assignable from "
67                     + value.getClass().getName());
68         }
69     }
70
71     /**
72      * Visit the current {@link Parameter}.
73      *
74      * @see org.picocontainer.Parameter#accept(org.picocontainer.PicoVisitor)
75      */

76     public void accept(final PicoVisitor visitor) {
77         visitor.visitParameter(this);
78     }
79
80     private boolean checkPrimitive(Class JavaDoc expectedType) {
81         try {
82             if (expectedType.isPrimitive()) {
83                 final Field JavaDoc field = value.getClass().getField("TYPE");
84                 final Class JavaDoc type = (Class JavaDoc) field.get(value);
85                 return expectedType.isAssignableFrom(type);
86             }
87         } catch (NoSuchFieldException JavaDoc e) {
88         } catch (IllegalAccessException JavaDoc e) {
89         }
90         return false;
91     }
92
93 }
94
Popular Tags