HHH-5147 Applied patch and removed obsolete code and comments

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19611 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2010-05-26 11:09:16 +00:00
parent db5b3a4c0a
commit 21af0d0146
1 changed files with 31 additions and 48 deletions

View File

@ -24,27 +24,29 @@
package org.hibernate.type; package org.hibernate.type;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.Properties; import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.util.StringHelper;
import org.hibernate.usertype.EnhancedUserType; import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.ParameterizedType;
import org.hibernate.util.ReflectHelper; import org.hibernate.util.ReflectHelper;
import org.slf4j.Logger; import org.hibernate.util.StringHelper;
import org.slf4j.LoggerFactory;
/** /**
* Enum type mapper * Enum type mapper
* Try and find the appropriate SQL type depending on column metadata * Try and find the appropriate SQL type depending on column metadata
* <p/> * <p/>
* TODO implements readobject/writeobject to recalculate the enumclasses * TODO implements readobject/writeobject to recalculate the enumclasses
*
* @author Emmanuel Bernard * @author Emmanuel Bernard
* @author Hardy Ferentschik
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class EnumType implements EnhancedUserType, ParameterizedType, Serializable { public class EnumType implements EnhancedUserType, ParameterizedType, Serializable {
@ -56,7 +58,8 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
* in order to check the trace-enablement. Driving this via a central Log-specific class * in order to check the trace-enablement. Driving this via a central Log-specific class
* would alleviate that performance hit, and yet still allow more "normal" logging usage/config. * would alleviate that performance hit, and yet still allow more "normal" logging usage/config.
*/ */
private static final boolean IS_VALUE_TRACING_ENABLED = LoggerFactory.getLogger( StringHelper.qualifier( Type.class.getName() ) ).isTraceEnabled(); private static final boolean IS_VALUE_TRACING_ENABLED = LoggerFactory.getLogger( StringHelper.qualifier( Type.class.getName() ) )
.isTraceEnabled();
private transient Logger log; private transient Logger log;
private Logger log() { private Logger log() {
@ -75,12 +78,10 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
private Class<? extends Enum> enumClass; private Class<? extends Enum> enumClass;
private transient Object[] enumValues; private transient Object[] enumValues;
private String catalog;
private String schema;
private int sqlType = Types.INTEGER; //before any guessing private int sqlType = Types.INTEGER; //before any guessing
public int[] sqlTypes() { public int[] sqlTypes() {
return new int[]{sqlType}; return new int[] { sqlType };
} }
public Class<? extends Enum> returnedClass() { public Class<? extends Enum> returnedClass() {
@ -106,7 +107,7 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
} }
if ( object instanceof Number ) { if ( object instanceof Number ) {
initEnumValues(); initEnumValues();
int ordinal = ( (Number) object ).intValue(); int ordinal = ( ( Number ) object ).intValue();
if ( ordinal < 0 || ordinal >= enumValues.length ) { if ( ordinal < 0 || ordinal >= enumValues.length ) {
throw new IllegalArgumentException( "Unknown ordinal value for enum " + enumClass + ": " + ordinal ); throw new IllegalArgumentException( "Unknown ordinal value for enum " + enumClass + ": " + ordinal );
} }
@ -116,36 +117,37 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
return enumValues[ordinal]; return enumValues[ordinal];
} }
else { else {
String name = (String) object; String name = ( String ) object;
if ( IS_VALUE_TRACING_ENABLED ) { if ( IS_VALUE_TRACING_ENABLED ) {
log().debug( "Returning '{}' as column {}", name, names[0] ); log().debug( "Returning '{}' as column {}", name, names[0] );
} }
try { try {
return Enum.valueOf( enumClass, name ); return Enum.valueOf( enumClass, name );
} }
catch (IllegalArgumentException iae) { catch ( IllegalArgumentException iae ) {
throw new IllegalArgumentException( "Unknown name value for enum " + enumClass + ": " + name, iae ); throw new IllegalArgumentException( "Unknown name value for enum " + enumClass + ": " + name, iae );
} }
} }
} }
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
//if (!guessed) guessType( st, index );
if ( value == null ) { if ( value == null ) {
if ( IS_VALUE_TRACING_ENABLED ) log().debug( "Binding null to parameter: {}", index ); if ( IS_VALUE_TRACING_ENABLED ) {
log().debug( "Binding null to parameter: {}", index );
}
st.setNull( index, sqlType ); st.setNull( index, sqlType );
} }
else { else {
boolean isOrdinal = isOrdinal( sqlType ); boolean isOrdinal = isOrdinal( sqlType );
if ( isOrdinal ) { if ( isOrdinal ) {
int ordinal = ( (Enum<?>) value ).ordinal(); int ordinal = ( ( Enum<?> ) value ).ordinal();
if ( IS_VALUE_TRACING_ENABLED ) { if ( IS_VALUE_TRACING_ENABLED ) {
log().debug( "Binding '{}' to parameter: {}", ordinal, index ); log().debug( "Binding '{}' to parameter: {}", ordinal, index );
} }
st.setObject( index, Integer.valueOf( ordinal ), sqlType ); st.setObject( index, Integer.valueOf( ordinal ), sqlType );
} }
else { else {
String enumString = ( (Enum<?>) value ).name(); String enumString = ( ( Enum<?> ) value ).name();
if ( IS_VALUE_TRACING_ENABLED ) { if ( IS_VALUE_TRACING_ENABLED ) {
log().debug( "Binding '{}' to parameter: {}", enumString, index ); log().debug( "Binding '{}' to parameter: {}", enumString, index );
} }
@ -183,7 +185,7 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value; return ( Serializable ) value;
} }
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Object assemble(Serializable cached, Object owner) throws HibernateException {
@ -199,23 +201,13 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
try { try {
enumClass = ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class ); enumClass = ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class );
} }
catch (ClassNotFoundException exception) { catch ( ClassNotFoundException exception ) {
throw new HibernateException( "Enum class not found", exception ); throw new HibernateException( "Enum class not found", exception );
} }
// is might be good to call it here, to see a possible error immediately
// initEnumValue();
//nullify unnullified properties yuck!
schema = parameters.getProperty( SCHEMA );
if ( "".equals( schema ) ) schema = null;
catalog = parameters.getProperty( CATALOG );
if ( "".equals( catalog ) ) catalog = null;
// table = parameters.getProperty( TABLE );
// column = parameters.getProperty( COLUMN );
String type = parameters.getProperty( TYPE ); String type = parameters.getProperty( TYPE );
if ( type != null ) { if ( type != null ) {
sqlType = Integer.decode( type ).intValue(); sqlType = Integer.decode( type );
// guessed = true;
} }
} }
@ -224,41 +216,32 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
*/ */
private void initEnumValues() { private void initEnumValues() {
if ( enumValues == null ) { if ( enumValues == null ) {
try { this.enumValues = enumClass.getEnumConstants();
Method method = enumClass.getDeclaredMethod( "values" ); if ( enumValues == null ) {
enumValues = (Object[]) method.invoke( null ); throw new NullPointerException( "Failed to init enumValues" );
}
catch (Exception e) {
throw new HibernateException( "Error while accessing enum.values(): " + enumClass, e );
} }
} }
} }
// is might be good to call initEnumValues() here, to see a possible error immediatelly, otherwise leave it commented
// private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
// initEnumValues();
// ois.defaultReadObject();
// }
public String objectToSQLString(Object value) { public String objectToSQLString(Object value) {
boolean isOrdinal = isOrdinal( sqlType ); boolean isOrdinal = isOrdinal( sqlType );
if ( isOrdinal ) { if ( isOrdinal ) {
int ordinal = ( (Enum) value ).ordinal(); int ordinal = ( ( Enum ) value ).ordinal();
return Integer.toString( ordinal ); return Integer.toString( ordinal );
} }
else { else {
return '\'' + ( (Enum) value ).name() + '\''; return '\'' + ( ( Enum ) value ).name() + '\'';
} }
} }
public String toXMLString(Object value) { public String toXMLString(Object value) {
boolean isOrdinal = isOrdinal( sqlType ); boolean isOrdinal = isOrdinal( sqlType );
if ( isOrdinal ) { if ( isOrdinal ) {
int ordinal = ( (Enum) value ).ordinal(); int ordinal = ( ( Enum ) value ).ordinal();
return Integer.toString( ordinal ); return Integer.toString( ordinal );
} }
else { else {
return ( (Enum) value ).name(); return ( ( Enum ) value ).name();
} }
} }
@ -271,11 +254,11 @@ public class EnumType implements EnhancedUserType, ParameterizedType, Serializab
} }
return enumValues[ordinal]; return enumValues[ordinal];
} }
catch(NumberFormatException e) { catch ( NumberFormatException e ) {
try { try {
return Enum.valueOf( enumClass, xmlValue ); return Enum.valueOf( enumClass, xmlValue );
} }
catch (IllegalArgumentException iae) { catch ( IllegalArgumentException iae ) {
throw new IllegalArgumentException( "Unknown name value for enum " + enumClass + ": " + xmlValue, iae ); throw new IllegalArgumentException( "Unknown name value for enum " + enumClass + ": " + xmlValue, iae );
} }
} }