01: //$Id: ClassType.java 4582 2004-09-25 11:22:20Z oneovthafew $
02: package org.hibernate.type;
03:
04: import java.sql.PreparedStatement;
05: import java.sql.ResultSet;
06: import java.sql.SQLException;
07:
08: import org.hibernate.Hibernate;
09: import org.hibernate.HibernateException;
10: import org.hibernate.util.ReflectHelper;
11:
12: /**
13: * <tt>class</tt>: A type that maps an SQL VARCHAR to a Java Class.
14: * @author Gavin King
15: */
16: public class ClassType extends ImmutableType {
17:
18: public Object get(ResultSet rs, String name)
19: throws HibernateException, SQLException {
20: String str = (String) Hibernate.STRING.get(rs, name);
21: if (str == null) {
22: return null;
23: } else {
24: try {
25: return ReflectHelper.classForName(str);
26: } catch (ClassNotFoundException cnfe) {
27: throw new HibernateException("Class not found: " + str);
28: }
29: }
30: }
31:
32: public void set(PreparedStatement st, Object value, int index)
33: throws HibernateException, SQLException {
34: //TODO: would be nice to handle proxy classes elegantly!
35: Hibernate.STRING.set(st, ((Class) value).getName(), index);
36: }
37:
38: public int sqlType() {
39: return Hibernate.STRING.sqlType();
40: }
41:
42: public String toString(Object value) throws HibernateException {
43: return ((Class) value).getName();
44: }
45:
46: public Class getReturnedClass() {
47: return Class.class;
48: }
49:
50: public String getName() {
51: return "class";
52: }
53:
54: public Object fromStringValue(String xml) throws HibernateException {
55: try {
56: return ReflectHelper.classForName(xml);
57: } catch (ClassNotFoundException cnfe) {
58: throw new HibernateException("could not parse xml", cnfe);
59: }
60: }
61:
62: }
|