HHH-11752 - Remove references to old types and fix LGTM warning.

This commit is contained in:
Chris Cranford 2017-05-15 23:57:19 -04:00
parent cab613de11
commit 9826528435
1 changed files with 84 additions and 55 deletions

View File

@ -15,7 +15,6 @@ import org.hibernate.mapping.Value;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.CustomType; import org.hibernate.type.CustomType;
import org.hibernate.type.EnumType; import org.hibernate.type.EnumType;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.dom4j.Element; import org.dom4j.Element;
@ -24,56 +23,30 @@ import org.dom4j.Element;
* Generates metadata for basic properties: immutable types (including enums). * Generates metadata for basic properties: immutable types (including enums).
* *
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
* @author Chris Cranford
*/ */
public final class BasicMetadataGenerator { public final class BasicMetadataGenerator {
@SuppressWarnings({"unchecked"})
boolean addBasic( boolean addBasic(
Element parent, PropertyAuditingData propertyAuditingData, Element parent,
Value value, SimpleMapperBuilder mapper, boolean insertable, boolean key) { PropertyAuditingData propertyAuditingData,
final Type type = value.getType(); Value value,
SimpleMapperBuilder mapper,
boolean insertable,
boolean key) {
if ( type instanceof BasicType if ( value.getType() instanceof BasicType ) {
|| type instanceof SerializableToBlobType
|| "org.hibernate.type.PrimitiveByteArrayBlobType".equals( type.getClass().getName() ) ) {
if ( parent != null ) { if ( parent != null ) {
final boolean addNestedType = (value instanceof SimpleValue) final Element propMapping = buildProperty(
&& ( (SimpleValue) value ).getTypeParameters() != null;
String typeName = type.getName();
if ( typeName == null ) {
typeName = type.getClass().getName();
}
final Element propMapping = MetadataTools.addProperty(
parent, parent,
propertyAuditingData.getName(), propertyAuditingData,
addNestedType ? null : typeName, value,
propertyAuditingData.isForceInsertable() || insertable, insertable,
key key
); );
MetadataTools.addColumns( propMapping, value.getColumnIterator() );
if ( addNestedType ) { if ( isAddNestedType( value ) ) {
final Properties typeParameters = ( (SimpleValue) value ).getTypeParameters(); applyNestedType( (SimpleValue) value, propMapping );
final Element typeMapping = propMapping.addElement( "type" );
typeMapping.addAttribute( "name", typeName );
if ( "org.hibernate.type.EnumType".equals( typeName ) ) {
// Proper handling of enumeration type
mapEnumerationType( typeMapping, type, typeParameters );
}
else {
// By default copying all Hibernate properties
for ( Object object : typeParameters.keySet() ) {
final String keyType = (String) object;
final String property = typeParameters.getProperty( keyType );
if ( property != null ) {
typeMapping.addElement( "param" ).addAttribute( "name", keyType ).setText( property );
}
}
}
} }
} }
@ -81,14 +54,13 @@ public final class BasicMetadataGenerator {
if ( mapper != null ) { if ( mapper != null ) {
mapper.add( propertyAuditingData.getPropertyData() ); mapper.add( propertyAuditingData.getPropertyData() );
} }
}
else {
return false;
}
return true; return true;
} }
return false;
}
private void mapEnumerationType(Element parent, Type type, Properties parameters) { private void mapEnumerationType(Element parent, Type type, Properties parameters) {
if ( parameters.getProperty( EnumType.ENUM ) != null ) { if ( parameters.getProperty( EnumType.ENUM ) != null ) {
parent.addElement( "param" ) parent.addElement( "param" )
@ -96,20 +68,18 @@ public final class BasicMetadataGenerator {
.setText( parameters.getProperty( EnumType.ENUM ) ); .setText( parameters.getProperty( EnumType.ENUM ) );
} }
else { else {
parent.addElement( "param" ).addAttribute( "name", EnumType.ENUM ).setText( parent.addElement( "param" )
type.getReturnedClass() .addAttribute( "name", EnumType.ENUM )
.getName() .setText( type.getReturnedClass().getName() );
);
} }
if ( parameters.getProperty( EnumType.NAMED ) != null ) { if ( parameters.getProperty( EnumType.NAMED ) != null ) {
parent.addElement( "param" ).addAttribute( "name", EnumType.NAMED ).setText( parent.addElement( "param" )
parameters.getProperty( .addAttribute( "name", EnumType.NAMED )
EnumType.NAMED .setText( parameters.getProperty( EnumType.NAMED ) );
)
);
} }
else { else {
parent.addElement( "param" ).addAttribute( "name", EnumType.NAMED ) parent.addElement( "param" )
.addAttribute( "name", EnumType.NAMED )
.setText( "" + !( (EnumType) ( (CustomType) type ).getUserType() ).isOrdinal() ); .setText( "" + !( (EnumType) ( (CustomType) type ).getUserType() ).isOrdinal() );
} }
} }
@ -143,4 +113,63 @@ public final class BasicMetadataGenerator {
return true; return true;
} }
private boolean isAddNestedType(Value value) {
if ( value instanceof SimpleValue ) {
if ( ( (SimpleValue) value ).getTypeParameters() != null ) {
return true;
}
}
return false;
}
private Element buildProperty(
Element parent,
PropertyAuditingData propertyAuditingData,
Value value,
boolean insertable,
boolean key) {
final Element propMapping = MetadataTools.addProperty(
parent,
propertyAuditingData.getName(),
isAddNestedType( value ) ? null : getBasicTypeName( value.getType() ),
propertyAuditingData.isForceInsertable() || insertable,
key
);
MetadataTools.addColumns( propMapping, value.getColumnIterator() );
return propMapping;
}
private void applyNestedType(SimpleValue value, Element propertyMapping) {
final Properties typeParameters = value.getTypeParameters();
final Element typeMapping = propertyMapping.addElement( "type" );
final String typeName = getBasicTypeName( value.getType() );
typeMapping.addAttribute( "name", typeName );
if ( EnumType.class.getName().equals( typeName ) ) {
// Proper handling of enumeration type
mapEnumerationType( typeMapping, value.getType(), typeParameters );
}
else {
// By default copying all Hibernate properties
for ( Object object : typeParameters.keySet() ) {
final String keyType = (String) object;
final String property = typeParameters.getProperty( keyType );
if ( property != null ) {
typeMapping.addElement( "param" ).addAttribute( "name", keyType ).setText( property );
}
}
}
}
private String getBasicTypeName(Type type) {
String typeName = type.getName();
if ( typeName == null ) {
typeName = type.getClass().getName();
}
return typeName;
}
} }