From 26a42cb3657fe8c010da346ccf31ffd5902df2e6 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 12 Nov 2012 12:13:52 -0600 Subject: [PATCH] HHH-7777 - Deprecate XmlRepresentableType (cherry picked from commit 5ef8a667ff06ec3bbcee8cb41a37992f425f6e59) --- .../java/org/hibernate/type/CustomType.java | 56 ++++++++++++++----- .../hibernate/usertype/EnhancedUserType.java | 26 +++++---- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/CustomType.java b/hibernate-core/src/main/java/org/hibernate/type/CustomType.java index 5d65c78d144c..8df22529f406 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/CustomType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/CustomType.java @@ -54,7 +54,10 @@ * @author Gavin King * @author Steve Ebersole */ -public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType, BasicType { +public class CustomType + extends AbstractType + implements IdentifierType, DiscriminatorType, VersionType, BasicType, StringRepresentableType { + private final UserType userType; private final String name; private final int[] types; @@ -146,7 +149,7 @@ public Object replace( SessionImplementor session, Object owner, Map copyCache) throws HibernateException { - return userType.replace(original, target, owner); + return userType.replace( original, target, owner ); } public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) @@ -163,20 +166,12 @@ public void nullSafeSet(PreparedStatement st, Object value, int index, SessionIm @SuppressWarnings({ "UnusedDeclaration" }) public String toXMLString(Object value, SessionFactoryImplementor factory) { - if ( value == null ) { - return null; - } - if ( userType instanceof EnhancedUserType ) { - return ( (EnhancedUserType) userType ).toXMLString( value ); - } - else { - return value.toString(); - } + return toString( value ); } @SuppressWarnings({ "UnusedDeclaration" }) public Object fromXMLString(String xml, Mapping factory) { - return ( (EnhancedUserType) userType ).fromXMLString(xml); + return fromStringValue( xml ); } public String getName() { @@ -193,7 +188,7 @@ public boolean isMutable() { } public Object stringToObject(String xml) { - return ( (EnhancedUserType) userType ).fromXMLString(xml); + return fromStringValue( xml ); } public String objectToSQLString(Object value, Dialect dialect) throws Exception { @@ -246,4 +241,39 @@ public boolean isDirty(Object old, Object current, boolean[] checkable, SessionI throws HibernateException { return checkable[0] && isDirty(old, current, session); } + + @Override + @SuppressWarnings("unchecked") + public String toString(Object value) throws HibernateException { + if ( StringRepresentableType.class.isInstance( userType ) ) { + return ( (StringRepresentableType) userType ).toString( value ); + } + if ( value == null ) { + return null; + } + if ( EnhancedUserType.class.isInstance( userType ) ) { + //noinspection deprecation + return ( (EnhancedUserType) userType ).toXMLString( value ); + } + return value.toString(); + } + + @Override + public Object fromStringValue(String string) throws HibernateException { + if ( StringRepresentableType.class.isInstance( userType ) ) { + return ( (StringRepresentableType) userType ).fromStringValue( string ); + } + if ( EnhancedUserType.class.isInstance( userType ) ) { + //noinspection deprecation + return ( (EnhancedUserType) userType ).fromXMLString( string ); + } + throw new HibernateException( + String.format( + "Could not process #fromStringValue, UserType class [%s] did not implement %s or %s", + name, + StringRepresentableType.class.getName(), + EnhancedUserType.class.getName() + ) + ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/usertype/EnhancedUserType.java b/hibernate-core/src/main/java/org/hibernate/usertype/EnhancedUserType.java index 9b51a488f85e..90422be67c99 100755 --- a/hibernate-core/src/main/java/org/hibernate/usertype/EnhancedUserType.java +++ b/hibernate-core/src/main/java/org/hibernate/usertype/EnhancedUserType.java @@ -1,10 +1,10 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * Copyright (c) 2008, 2012, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Middleware LLC. + * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU @@ -20,15 +20,11 @@ * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA - * */ package org.hibernate.usertype; - /** - * A custom type that may function as an identifier or - * discriminator type, or may be marshalled to and from - * an XML document + * A custom type that may function as an identifier or discriminator type * * @author Gavin King */ @@ -39,13 +35,21 @@ public interface EnhancedUserType extends UserType { public String objectToSQLString(Object value); /** - * Return a string representation of this value, as it - * should appear in an XML document + * Return a string representation of this value, as it should appear in an XML document + * + * @deprecated To be removed in 5. Implement {@link org.hibernate.type.StringRepresentableType#toString(Object)} + * instead. See HHH-7776 for details */ + @Deprecated public String toXMLString(Object value); + /** - * Parse a string representation of this value, as it - * appears in an XML document + * Parse a string representation of this value, as it appears in an XML document + * + * @deprecated To be removed in 5. Implement + * {@link org.hibernate.type.StringRepresentableType#fromStringValue(String)} instead. + * See HHH-7776 for details */ + @Deprecated public Object fromXMLString(String xmlValue); }