From d9495474ef21f78b58e1ea03d16e798f8e6f912b Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 21 May 2015 13:14:12 -0500 Subject: [PATCH] HHH-8854 - Could not extract ParameterizedType representation of AttributeConverter definition --- .../cfg/AttributeConverterDefinition.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AttributeConverterDefinition.java b/hibernate-core/src/main/java/org/hibernate/cfg/AttributeConverterDefinition.java index 6f55a93313..9d647108fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AttributeConverterDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AttributeConverterDefinition.java @@ -31,6 +31,7 @@ import java.util.Arrays; import java.util.List; import javax.persistence.AttributeConverter; +import javax.persistence.Converter; import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; @@ -47,6 +48,82 @@ public class AttributeConverterDefinition { private final Class entityAttributeType; private final Class databaseColumnType; + + /** + * Build an AttributeConverterDefinition from the AttributeConverter Class reference and + * whether or not to auto-apply it. + * + * @param attributeConverterClass The AttributeConverter Class + * @param autoApply Should the AttributeConverter be auto-applied? + * + * @return The constructed definition + */ + public static AttributeConverterDefinition from(Class attributeConverterClass, boolean autoApply) { + return new AttributeConverterDefinition( + instantiateAttributeConverter( attributeConverterClass ), + autoApply + ); + } + + private static AttributeConverter instantiateAttributeConverter(Class attributeConverterClass) { + try { + return attributeConverterClass.newInstance(); + } + catch (Exception e) { + throw new AnnotationException( + "Unable to instantiate AttributeConverter [" + attributeConverterClass.getName() + "]", + e + ); + } + } + + /** + * Build an AttributeConverterDefinition from the AttributeConverter Class reference. The + * converter is searched for a {@link Converter} annotation to determine whether it should + * be treated as auto-apply. If the annotation is present, {@link Converter#autoApply()} is + * used to make that determination. If the annotation is not present, {@code false} is assumed. + * + * @param attributeConverterClass The converter class + * + * @return The constructed definition + */ + public static AttributeConverterDefinition from(Class attributeConverterClass) { + return from( instantiateAttributeConverter( attributeConverterClass ) ); + } + + /** + * Build an AttributeConverterDefinition from an AttributeConverter instance. The + * converter is searched for a {@link Converter} annotation to determine whether it should + * be treated as auto-apply. If the annotation is present, {@link Converter#autoApply()} is + * used to make that determination. If the annotation is not present, {@code false} is assumed. + * + * @param attributeConverter The AttributeConverter instance + * + * @return The constructed definition + */ + public static AttributeConverterDefinition from(AttributeConverter attributeConverter) { + boolean autoApply = false; + Converter converterAnnotation = attributeConverter.getClass().getAnnotation( Converter.class ); + if ( converterAnnotation != null ) { + autoApply = converterAnnotation.autoApply(); + } + + return new AttributeConverterDefinition( attributeConverter, autoApply ); + } + + /** + * Build an AttributeConverterDefinition from the AttributeConverter instance and + * whether or not to auto-apply it. + * + * @param attributeConverter The AttributeConverter instance + * @param autoApply Should the AttributeConverter be auto-applied? + * + * @return The constructed definition + */ + public static AttributeConverterDefinition from(AttributeConverter attributeConverter, boolean autoApply) { + return new AttributeConverterDefinition( attributeConverter, autoApply ); + } + public AttributeConverterDefinition(AttributeConverter attributeConverter, boolean autoApply) { this.attributeConverter = attributeConverter; this.autoApply = autoApply;