1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.usertype.spi.shared;
17
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.Properties;
26
27 import org.hibernate.HibernateException;
28 import org.hibernate.engine.spi.SharedSessionContractImplementor;
29 import org.hibernate.usertype.EnhancedUserType;
30 import org.jadira.usertype.spi.utils.reflection.TypeHelper;
31
32 public abstract class AbstractReflectionUserType<T> extends AbstractKnownClassHeuristicUserType<T> implements EnhancedUserType, Serializable {
33
34 private static final long serialVersionUID = 7943328235820102665L;
35
36 private Method identifierMethod;
37 private Method valueOfMethod;
38
39 protected void setIdentifierMethod(Method identifierMethod) {
40 this.identifierMethod = identifierMethod;
41 }
42
43 protected Method getIdentifierMethod() {
44 return identifierMethod;
45 }
46
47 protected void setValueOfMethod(Method valueOfMethod) {
48 if (!Modifier.isStatic(valueOfMethod.getModifiers())) {
49 throw new IllegalStateException("valueOfMethod must be static: " + valueOfMethod.toString());
50 }
51 this.valueOfMethod = valueOfMethod;
52 }
53
54 protected Method getValueOfMethod() {
55 return valueOfMethod;
56 }
57
58 public void setParameterValues(Properties parameters) {
59
60 if (identifierMethod == null) {
61
62 throw new IllegalStateException("No identifier method was defined for " + this.getClass().getName());
63 }
64
65 setIdentifierType(identifierMethod.getReturnType());
66
67 super.setParameterValues(parameters);
68 }
69
70 @SuppressWarnings("unchecked")
71 @Override
72 public Class<T> returnedClass() {
73 return (Class<T>) TypeHelper.getTypeArguments(AbstractReflectionUserType.class, getClass()).get(0);
74 }
75
76 @Override
77 public Object doNullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
78
79 Object identifier = getType().get(rs, names[0], session);
80
81 if (rs.wasNull()) {
82 return null;
83 }
84
85 return valueOfMethod.invoke(getMappedClass(), new Object[] { identifier });
86 }
87
88 @Override
89 public void doNullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
90
91 if (value == null) {
92 preparedStatement.setNull(index, getType().sqlType());
93 } else {
94 Object identifier = identifierMethod.invoke(value, new Object[0]);
95 getType().nullSafeSet(preparedStatement, identifier, index, session);
96 }
97 }
98
99 @Override
100 public String objectToSQLString(final Object object) {
101
102 if (object == null) {
103 return null;
104 }
105 if (identifierMethod != null) {
106 try {
107 return String.valueOf(identifierMethod.invoke(object));
108 } catch (InvocationTargetException e) {
109
110 } catch (IllegalAccessException e) {
111
112 }
113 }
114 return super.objectToSQLString(object);
115 }
116
117 @Override
118 @Deprecated
119 public String toXMLString(Object object) {
120 if (identifierMethod != null) {
121 try {
122 return String.valueOf(identifierMethod.invoke(object));
123
124 } catch (InvocationTargetException e) {
125 throw new HibernateException("Problem constructing XMLString: " + object + "'", e);
126
127 } catch (IllegalAccessException e) {
128 throw new HibernateException("Problem constructing XMLString: " + object + "'", e);
129 }
130 }
131 return super.toXMLString(object);
132 }
133 }