KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > coerce > ClassHandler


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util.coerce;
11
12 import org.jboss.util.CoercionException;
13 import org.jboss.util.NotCoercibleException;
14
15 /**
16  * A <tt>java.lang.Class</tt> coercion handler.
17  *
18  * @version <tt>$Revision: 1.1 $</tt>
19  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
20  */

21 public class ClassHandler
22    extends BoundCoercionHandler
23 {
24    /**
25     * Get the target class type for this <tt>CoercionHandler</tt>.
26     *
27     * @return Class type.
28     */

29    public Class JavaDoc getType() {
30       return Class JavaDoc.class;
31    }
32
33    /**
34     * Coerces the given value into the given type (which should be
35     * <tt>Class</tt>).
36     *
37     * <p>This currently only support coercion from a <tt>String</tt>.
38     *
39     * @param value Value to coerce.
40     * @param type <tt>java.lang.Class</tt>.
41     * @return Value coerced into a <tt>Class</tt>.
42     *
43     * @throws CoercionException Failed to coerce.
44     */

45    public Object JavaDoc coerce(Object JavaDoc value, Class JavaDoc type) throws CoercionException {
46       if (value.getClass().equals(String JavaDoc.class)) {
47          return coerce((String JavaDoc)value);
48       }
49       
50       throw new NotCoercibleException(value);
51    }
52
53    /**
54     * Coerces the given String into a <tt>Class</tt> by doing a
55     * <code>Class.forName()</code>.
56     *
57     * @param value String value to convert to a <tt>Class</tt>.
58     * @return <tt>Class</tt> value.
59     *
60     * @throws NotCoercibleException Class not found.
61     */

62    public Object JavaDoc coerce(String JavaDoc value) {
63       try {
64          return Class.forName(value);
65       }
66       catch (ClassNotFoundException JavaDoc e) {
67          throw new NotCoercibleException(value, e);
68       }
69    }
70 }
71
72
Popular Tags