/*************************************** * * * JBoss: The OpenSource J2EE WebOS * * * * Distributable under LGPL license. * * See terms of license at gnu.org. * * * ***************************************/ package org.jboss.util.coerce; import org.jboss.util.CoercionException; import org.jboss.util.NotCoercibleException; /** * A java.lang.Class coercion handler. * * @version $Revision: 1.1 $ * @author Jason Dillon */ public class ClassHandler extends BoundCoercionHandler { /** * Get the target class type for this CoercionHandler. * * @return Class type. */ public Class getType() { return Class.class; } /** * Coerces the given value into the given type (which should be * Class). * *
This currently only support coercion from a String.
*
* @param value Value to coerce.
* @param type java.lang.Class.
* @return Value coerced into a Class.
*
* @throws CoercionException Failed to coerce.
*/
public Object coerce(Object value, Class type) throws CoercionException {
if (value.getClass().equals(String.class)) {
return coerce((String)value);
}
throw new NotCoercibleException(value);
}
/**
* Coerces the given String into a Class by doing a
* Class.forName().
*
* @param value String value to convert to a Class.
* @return Class value.
*
* @throws NotCoercibleException Class not found.
*/
public Object coerce(String value) {
try {
return Class.forName(value);
}
catch (ClassNotFoundException e) {
throw new NotCoercibleException(value, e);
}
}
}