From f2338af9eb88302635da168a0c280e6365ceb0f7 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 1 Aug 2011 14:35:15 +0200 Subject: [PATCH] HHH-6520 Some formatting and method name changes. Also moving AbstractAttributeTypeResolver into the type sub-package --- .../annotations/attribute/BasicAttribute.java | 41 +++--- .../AbstractAttributeTypeResolver.java | 48 +++---- .../attribute/type/AttributeTypeResolver.java | 18 ++- .../type/AttributeTypeResolverImpl.java | 79 +++++++++++ .../type/CompositeAttributeTypeResolver.java | 80 +++++------ .../type/EnumeratedTypeResolver.java | 128 ++++++++---------- .../type/ExplicitAttributeTypeResolver.java | 86 ------------ .../attribute/type/LobTypeResolver.java | 118 ++++++++-------- .../attribute/type/TemporalTypeResolver.java | 116 ++++++++-------- .../attribute/type/package-info.java | 28 ++++ 10 files changed, 371 insertions(+), 371 deletions(-) rename hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/{ => type}/AbstractAttributeTypeResolver.java (51%) create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolverImpl.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/ExplicitAttributeTypeResolver.java create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/package-info.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java index b00c09a7d7..8a3c6fe20e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java @@ -46,9 +46,9 @@ import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; import org.hibernate.metamodel.source.annotations.TypeEnumConversionHelper; import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolver; +import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolverImpl; import org.hibernate.metamodel.source.annotations.attribute.type.CompositeAttributeTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.type.EnumeratedTypeResolver; -import org.hibernate.metamodel.source.annotations.attribute.type.ExplicitAttributeTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.type.LobTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.type.TemporalTypeResolver; @@ -105,7 +105,7 @@ public class BasicAttribute extends MappedAttribute { private final String customWriteFragment; private final String customReadFragment; private final String checkCondition; - private AttributeTypeResolver resolver; + private final AttributeTypeResolver resolver; public static BasicAttribute createSimpleAttribute(String name, Class attributeType, @@ -127,7 +127,7 @@ public class BasicAttribute extends MappedAttribute { annotations, JPADotNames.EMBEDDED_ID ); - //if this attribute has either @Id or @EmbeddedId, then it is an id attribute + //if this attribute has either @Id or @EmbeddedId, then it is an id attribute isId = ( idAnnotation != null || embeddedIdAnnotation != null ); AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION ); @@ -156,6 +156,7 @@ public class BasicAttribute extends MappedAttribute { this.customReadFragment = readWrite[0]; this.customWriteFragment = readWrite[1]; this.checkCondition = parseCheckAnnotation(); + this.resolver = getDefaultHibernateTypeResolver(); } public final ColumnValues getColumnValues() { @@ -356,26 +357,22 @@ public class BasicAttribute extends MappedAttribute { return generator; } - @Override - public AttributeTypeResolver getHibernateTypeResolver() { - if ( resolver == null ) { - resolver = getDefaultHibernateTypeResolver(); - } - return resolver; - } + @Override + public AttributeTypeResolver getHibernateTypeResolver() { + return resolver; + } - protected AttributeTypeResolver getDefaultHibernateTypeResolver() { - - CompositeAttributeTypeResolver resolver = new CompositeAttributeTypeResolver( - new ExplicitAttributeTypeResolver( - this - ) - ); - resolver.addHibernateTypeResolver( new TemporalTypeResolver( this ) ); - resolver.addHibernateTypeResolver( new LobTypeResolver( this ) ); - resolver.addHibernateTypeResolver( new EnumeratedTypeResolver( this ) ); - return resolver; - } + private AttributeTypeResolver getDefaultHibernateTypeResolver() { + CompositeAttributeTypeResolver resolver = new CompositeAttributeTypeResolver( + new AttributeTypeResolverImpl( + this + ) + ); + resolver.addHibernateTypeResolver( new TemporalTypeResolver( this ) ); + resolver.addHibernateTypeResolver( new LobTypeResolver( this ) ); + resolver.addHibernateTypeResolver( new EnumeratedTypeResolver( this ) ); + return resolver; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AbstractAttributeTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AbstractAttributeTypeResolver.java similarity index 51% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AbstractAttributeTypeResolver.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AbstractAttributeTypeResolver.java index 817c6af873..ff0999985b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AbstractAttributeTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AbstractAttributeTypeResolver.java @@ -22,7 +22,7 @@ * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations.attribute; +package org.hibernate.metamodel.source.annotations.attribute.type; import java.util.Collections; import java.util.Map; @@ -30,39 +30,31 @@ import java.util.Map; import org.jboss.jandex.AnnotationInstance; import org.hibernate.internal.util.StringHelper; -import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolver; /** * @author Strong Liu */ public abstract class AbstractAttributeTypeResolver implements AttributeTypeResolver { - protected abstract AnnotationInstance getAnnotationInstance(); + protected abstract AnnotationInstance getTypeDeterminingAnnotationInstance(); - protected abstract String resolveHibernateTypeName(AnnotationInstance annotationInstance); + protected abstract String resolveHibernateTypeName(AnnotationInstance annotationInstance); - protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { - return Collections.emptyMap(); - } -// private String explicitHibernateTypeName; -// private Map explicitHibernateTypeParameters; - /** - * An optional explicit hibernate type name specified via {@link org.hibernate.annotations.Type}. - */ - @Override - final public String getExplicitHibernateTypeName() { - return resolveHibernateTypeName( getAnnotationInstance() ); - } + protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { + return Collections.emptyMap(); + } - /** - * Optional type parameters. See {@link #getExplicitHibernateTypeName()}. - */ - @Override - final public Map getExplicitHibernateTypeParameters() { - if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) { - return resolveHibernateTypeParameters( getAnnotationInstance() ); - } - else { - return Collections.emptyMap(); - } - } + @Override + final public String getExplicitHibernateTypeName() { + return resolveHibernateTypeName( getTypeDeterminingAnnotationInstance() ); + } + + @Override + final public Map getExplicitHibernateTypeParameters() { + if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) { + return resolveHibernateTypeParameters( getTypeDeterminingAnnotationInstance() ); + } + else { + return Collections.emptyMap(); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolver.java index f1dc0e1254..7333c64109 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolver.java @@ -27,9 +27,23 @@ package org.hibernate.metamodel.source.annotations.attribute.type; import java.util.Map; /** + * Determines explicit Hibernate type information for JPA mapped attributes when additional type information is + * provided via annotations like {@link javax.persistence.Lob}, {@link javax.persistence.Enumerated} and + * {@link javax.persistence.Temporal}. + * * @author Strong Liu */ public interface AttributeTypeResolver { - String getExplicitHibernateTypeName(); - Map getExplicitHibernateTypeParameters(); + /** + * @return returns an explicit hibernate type name in case the mapped attribute has an additional + * {@link org.hibernate.annotations.Type} annotation or an implicit type is given via the use of annotations like + * {@link javax.persistence.Lob}, {@link javax.persistence.Enumerated} and + * {@link javax.persistence.Temporal}. + */ + String getExplicitHibernateTypeName(); + + /** + * @return Returns a map of optional type parameters. See {@link #getExplicitHibernateTypeName()}. + */ + Map getExplicitHibernateTypeParameters(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolverImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolverImpl.java new file mode 100644 index 0000000000..5abc9e2f2b --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/AttributeTypeResolverImpl.java @@ -0,0 +1,79 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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 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 + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ + +package org.hibernate.metamodel.source.annotations.attribute.type; + +import java.util.HashMap; +import java.util.Map; + +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationValue; + +import org.hibernate.metamodel.source.annotations.HibernateDotNames; +import org.hibernate.metamodel.source.annotations.JandexHelper; +import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; + +/** + * @author Strong Liu + */ +public class AttributeTypeResolverImpl extends AbstractAttributeTypeResolver { + private final MappedAttribute mappedAttribute; + + public AttributeTypeResolverImpl(MappedAttribute mappedAttribute) { + this.mappedAttribute = mappedAttribute; + } + + @Override + protected String resolveHibernateTypeName(AnnotationInstance typeAnnotation) { + String typeName = null; + if ( typeAnnotation != null ) { + typeName = JandexHelper.getValue( typeAnnotation, "type", String.class ); + } + return typeName; + } + + @Override + protected Map resolveHibernateTypeParameters(AnnotationInstance typeAnnotation) { + HashMap typeParameters = new HashMap(); + AnnotationValue parameterAnnotationValue = typeAnnotation.value( "parameters" ); + if ( parameterAnnotationValue != null ) { + AnnotationInstance[] parameterAnnotations = parameterAnnotationValue.asNestedArray(); + for ( AnnotationInstance parameterAnnotationInstance : parameterAnnotations ) { + typeParameters.put( + JandexHelper.getValue( parameterAnnotationInstance, "name", String.class ), + JandexHelper.getValue( parameterAnnotationInstance, "value", String.class ) + ); + } + } + return typeParameters; + } + + @Override + protected AnnotationInstance getTypeDeterminingAnnotationInstance() { + return JandexHelper.getSingleAnnotation( + mappedAttribute.annotations(), + HibernateDotNames.TYPE + ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/CompositeAttributeTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/CompositeAttributeTypeResolver.java index 096a75f701..26c36ff4ba 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/CompositeAttributeTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/CompositeAttributeTypeResolver.java @@ -36,48 +36,48 @@ import org.hibernate.internal.util.collections.CollectionHelper; * @author Strong Liu */ public class CompositeAttributeTypeResolver implements AttributeTypeResolver { - private List resolvers = new ArrayList(); - private final ExplicitAttributeTypeResolver explicitHibernateTypeResolver; + private List resolvers = new ArrayList(); + private final AttributeTypeResolverImpl explicitHibernateTypeResolver; - public CompositeAttributeTypeResolver(ExplicitAttributeTypeResolver explicitHibernateTypeResolver) { - if ( explicitHibernateTypeResolver == null ) { - throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); - } - this.explicitHibernateTypeResolver = explicitHibernateTypeResolver; - } + public CompositeAttributeTypeResolver(AttributeTypeResolverImpl explicitHibernateTypeResolver) { + if ( explicitHibernateTypeResolver == null ) { + throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); + } + this.explicitHibernateTypeResolver = explicitHibernateTypeResolver; + } - public void addHibernateTypeResolver(AttributeTypeResolver resolver) { - if ( resolver == null ) { - throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); - } - resolvers.add( resolver ); - } + public void addHibernateTypeResolver(AttributeTypeResolver resolver) { + if ( resolver == null ) { + throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); + } + resolvers.add( resolver ); + } - @Override - public String getExplicitHibernateTypeName() { - String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName(); - if ( StringHelper.isEmpty( type ) ) { - for ( AttributeTypeResolver resolver : resolvers ) { - type = resolver.getExplicitHibernateTypeName(); - if ( StringHelper.isNotEmpty( type ) ) { - break; - } - } - } - return type; - } + @Override + public String getExplicitHibernateTypeName() { + String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName(); + if ( StringHelper.isEmpty( type ) ) { + for ( AttributeTypeResolver resolver : resolvers ) { + type = resolver.getExplicitHibernateTypeName(); + if ( StringHelper.isNotEmpty( type ) ) { + break; + } + } + } + return type; + } - @Override - public Map getExplicitHibernateTypeParameters() { - Map parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters(); - if ( CollectionHelper.isEmpty( parameters ) ) { - for ( AttributeTypeResolver resolver : resolvers ) { - parameters = resolver.getExplicitHibernateTypeParameters(); - if ( CollectionHelper.isNotEmpty( parameters ) ) { - break; - } - } - } - return parameters; - } + @Override + public Map getExplicitHibernateTypeParameters() { + Map parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters(); + if ( CollectionHelper.isEmpty( parameters ) ) { + for ( AttributeTypeResolver resolver : resolvers ) { + parameters = resolver.getExplicitHibernateTypeParameters(); + if ( CollectionHelper.isNotEmpty( parameters ) ) { + break; + } + } + } + return parameters; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/EnumeratedTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/EnumeratedTypeResolver.java index f386645b6d..64a2f664e7 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/EnumeratedTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/EnumeratedTypeResolver.java @@ -34,7 +34,6 @@ import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; -import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; import org.hibernate.type.EnumType; @@ -42,81 +41,62 @@ import org.hibernate.type.EnumType; * @author Strong Liu */ public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver { - private final MappedAttribute mappedAttribute; - private final boolean isMapKey; + private final MappedAttribute mappedAttribute; + private final boolean isMapKey; - public EnumeratedTypeResolver(MappedAttribute mappedAttribute) { - if ( mappedAttribute == null ) { - throw new AssertionFailure( "MappedAttribute is null" ); - } - this.mappedAttribute = mappedAttribute; - this.isMapKey = false;//todo - } + public EnumeratedTypeResolver(MappedAttribute mappedAttribute) { + if ( mappedAttribute == null ) { + throw new AssertionFailure( "MappedAttribute is null" ); + } + this.mappedAttribute = mappedAttribute; + this.isMapKey = false;//todo + } - @Override - protected AnnotationInstance getAnnotationInstance() { - return JandexHelper.getSingleAnnotation( - mappedAttribute.annotations(), - JPADotNames.ENUMERATED - ); - } + @Override + protected AnnotationInstance getTypeDeterminingAnnotationInstance() { + return JandexHelper.getSingleAnnotation( + mappedAttribute.annotations(), + JPADotNames.ENUMERATED + ); + } - @Override - public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) { - boolean isEnum = mappedAttribute.getAttributeType().isEnum(); - if ( !isEnum ) { - if ( enumeratedAnnotation != null ) { - throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." ); - } - else { - return null; - } - } - return EnumType.class.getName(); - } - - @Override - protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { - HashMap typeParameters = new HashMap(); - typeParameters.put( EnumType.ENUM, mappedAttribute.getAttributeType().getName() ); - if ( annotationInstance != null ) { - javax.persistence.EnumType enumType = JandexHelper.getEnumValue( - annotationInstance, - "value", - javax.persistence.EnumType.class - ); - if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) { - typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) ); - } - else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) { - typeParameters.put( EnumType.TYPE, String.valueOf( Types.VARCHAR ) ); - } - else { - throw new AssertionFailure( "Unknown EnumType: " + enumType ); - } - } - else { - typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) ); - } - //todo -// Schema schema = mappedAttribute.getContext().getMetadataImplementor().getDatabase().getDefaultSchema(); -// Identifier schemaIdentifier = schema.getName().getSchema(); -// Identifier catalogIdentifier = schema.getName().getCatalog(); -// String schemaName = schemaIdentifier == null ? "" : schemaIdentifier.getName(); -// String catalogName = catalogIdentifier == null ? "" : catalogIdentifier.getName(); -// typeParameters.put( EnumType.SCHEMA, schemaName ); -// typeParameters.put( EnumType.CATALOG, catalogName ); - /** - String schema = columns[0].getTable().getSchema(); - schema = schema == null ? "" : schema; - String catalog = columns[0].getTable().getCatalog(); - catalog = catalog == null ? "" : catalog; - typeParameters.setProperty( EnumType.SCHEMA, schema ); - typeParameters.setProperty( EnumType.CATALOG, catalog ); - typeParameters.setProperty( EnumType.TABLE, columns[0].getTable().getName() ); - typeParameters.setProperty( EnumType.COLUMN, columns[0].getName() ); - */ - return typeParameters; - } + @Override + public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) { + boolean isEnum = mappedAttribute.getAttributeType().isEnum(); + if ( !isEnum ) { + if ( enumeratedAnnotation != null ) { + throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." ); + } + else { + return null; + } + } + return EnumType.class.getName(); + } + @Override + protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { + HashMap typeParameters = new HashMap(); + typeParameters.put( EnumType.ENUM, mappedAttribute.getAttributeType().getName() ); + if ( annotationInstance != null ) { + javax.persistence.EnumType enumType = JandexHelper.getEnumValue( + annotationInstance, + "value", + javax.persistence.EnumType.class + ); + if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) { + typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) ); + } + else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) { + typeParameters.put( EnumType.TYPE, String.valueOf( Types.VARCHAR ) ); + } + else { + throw new AssertionFailure( "Unknown EnumType: " + enumType ); + } + } + else { + typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) ); + } + return typeParameters; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/ExplicitAttributeTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/ExplicitAttributeTypeResolver.java deleted file mode 100644 index ff2f8d4c4b..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/ExplicitAttributeTypeResolver.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, 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 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 - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ - -package org.hibernate.metamodel.source.annotations.attribute.type; - -import java.util.HashMap; -import java.util.Map; - -import org.jboss.jandex.AnnotationInstance; -import org.jboss.jandex.AnnotationValue; - -import org.hibernate.metamodel.source.annotations.HibernateDotNames; -import org.hibernate.metamodel.source.annotations.JandexHelper; -import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver; -import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; - -/** - * @author Strong Liu - */ -public class ExplicitAttributeTypeResolver extends AbstractAttributeTypeResolver { - private final MappedAttribute mappedAttribute; - - - public ExplicitAttributeTypeResolver(MappedAttribute mappedAttribute) { - this.mappedAttribute = mappedAttribute; - } - - @Override - protected String resolveHibernateTypeName(AnnotationInstance typeAnnotation) { - String typeName = null; - if ( typeAnnotation != null ) { - typeName = JandexHelper.getValue( typeAnnotation, "type", String.class ); - } - return typeName; - } - - @Override - protected Map resolveHibernateTypeParameters(AnnotationInstance typeAnnotation) { - HashMap typeParameters = new HashMap(); - AnnotationValue parameterAnnotationValue = typeAnnotation.value( "parameters" ); - if ( parameterAnnotationValue != null ) { - AnnotationInstance[] parameterAnnotations = parameterAnnotationValue.asNestedArray(); - for ( AnnotationInstance parameterAnnotationInstance : parameterAnnotations ) { - typeParameters.put( - JandexHelper.getValue( parameterAnnotationInstance, "name", String.class ), - JandexHelper.getValue( - parameterAnnotationInstance, - "value", - String.class - ) - ); - } - } - return typeParameters; - } - - @Override - protected AnnotationInstance getAnnotationInstance() { - return JandexHelper.getSingleAnnotation( - mappedAttribute.annotations(), - HibernateDotNames.TYPE - ); - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/LobTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/LobTypeResolver.java index 99d048099b..8eb2341a82 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/LobTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/LobTypeResolver.java @@ -35,7 +35,6 @@ import org.jboss.jandex.AnnotationInstance; import org.hibernate.AssertionFailure; import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; -import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; import org.hibernate.type.CharacterArrayClobType; import org.hibernate.type.PrimitiveCharacterArrayClobType; @@ -47,67 +46,66 @@ import org.hibernate.type.WrappedMaterializedBlobType; * @author Strong Liu */ public class LobTypeResolver extends AbstractAttributeTypeResolver { - private final MappedAttribute mappedAttribute; + private final MappedAttribute mappedAttribute; + public LobTypeResolver(MappedAttribute mappedAttribute) { + if ( mappedAttribute == null ) { + throw new AssertionFailure( "MappedAttribute is null" ); + } + this.mappedAttribute = mappedAttribute; + } - public LobTypeResolver(MappedAttribute mappedAttribute) { - if ( mappedAttribute == null ) { - throw new AssertionFailure( "MappedAttribute is null" ); - } - this.mappedAttribute = mappedAttribute; - } + @Override + protected AnnotationInstance getTypeDeterminingAnnotationInstance() { + return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB ); + } - @Override - protected AnnotationInstance getAnnotationInstance() { - return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB ); - } + @Override + public String resolveHibernateTypeName(AnnotationInstance annotationInstance) { + if ( annotationInstance == null ) { + return null; + } + String type = null; + if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = StandardBasicTypes.CLOB.getName(); + } + else if ( Blob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = StandardBasicTypes.BLOB.getName(); + } + else if ( String.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = StandardBasicTypes.MATERIALIZED_CLOB.getName(); + } + else if ( Character[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = CharacterArrayClobType.class.getName(); + } + else if ( char[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = PrimitiveCharacterArrayClobType.class.getName(); + } + else if ( Byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = WrappedMaterializedBlobType.class.getName(); + } + else if ( byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = StandardBasicTypes.MATERIALIZED_BLOB.getName(); + } + else if ( Serializable.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { + type = SerializableToBlobType.class.getName(); + } + else { + type = "blob"; + } + return type; + } - @Override - public String resolveHibernateTypeName(AnnotationInstance annotationInstance) { - if ( annotationInstance == null ) { - return null; - } - String type = null; - if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = StandardBasicTypes.CLOB.getName(); - } - else if ( Blob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = StandardBasicTypes.BLOB.getName(); - } - else if ( String.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = StandardBasicTypes.MATERIALIZED_CLOB.getName(); - } - else if ( Character[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = CharacterArrayClobType.class.getName(); - } - else if ( char[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = PrimitiveCharacterArrayClobType.class.getName(); - } - else if ( Byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = WrappedMaterializedBlobType.class.getName(); - } - else if ( byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = StandardBasicTypes.MATERIALIZED_BLOB.getName(); - } - else if ( Serializable.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { - type = SerializableToBlobType.class.getName(); - } - else { - type = "blob"; - } - return type; - } - - @Override - protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { - if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) { - HashMap typeParameters = new HashMap(); - typeParameters.put( - SerializableToBlobType.CLASS_NAME, - mappedAttribute.getAttributeType().getName() - ); - return typeParameters; - } - return null; - } + @Override + protected Map resolveHibernateTypeParameters(AnnotationInstance annotationInstance) { + if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) { + HashMap typeParameters = new HashMap(); + typeParameters.put( + SerializableToBlobType.CLASS_NAME, + mappedAttribute.getAttributeType().getName() + ); + return typeParameters; + } + return null; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/TemporalTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/TemporalTypeResolver.java index 79f0ddcab8..f1ae78c511 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/TemporalTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/TemporalTypeResolver.java @@ -35,7 +35,6 @@ import org.hibernate.AssertionFailure; import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; -import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver; import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; import org.hibernate.type.StandardBasicTypes; @@ -43,67 +42,66 @@ import org.hibernate.type.StandardBasicTypes; * @author Strong Liu */ public class TemporalTypeResolver extends AbstractAttributeTypeResolver { - private final MappedAttribute mappedAttribute; - private final boolean isMapKey; + private final MappedAttribute mappedAttribute; + private final boolean isMapKey; - public TemporalTypeResolver(MappedAttribute mappedAttribute) { - if ( mappedAttribute == null ) { - throw new AssertionFailure( "MappedAttribute is null" ); - } - this.mappedAttribute = mappedAttribute; - this.isMapKey = false;//todo - } + public TemporalTypeResolver(MappedAttribute mappedAttribute) { + if ( mappedAttribute == null ) { + throw new AssertionFailure( "MappedAttribute is null" ); + } + this.mappedAttribute = mappedAttribute; + this.isMapKey = false;//todo + } - @Override - public String resolveHibernateTypeName(AnnotationInstance temporalAnnotation) { + @Override + public String resolveHibernateTypeName(AnnotationInstance temporalAnnotation) { - if ( isTemporalType( mappedAttribute.getAttributeType() ) ) { - if ( temporalAnnotation == null ) { - //SPEC 11.1.47 The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. - throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is a Temporal type, but no @Temporal annotation found." ); - } - TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class ); - boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() ); - String type = null; - switch ( temporalType ) { - case DATE: - type = isDate ? StandardBasicTypes.DATE.getName() : StandardBasicTypes.CALENDAR_DATE.getName(); - break; - case TIME: - type = StandardBasicTypes.TIME.getName(); - if ( !isDate ) { - throw new NotYetImplementedException( "Calendar cannot persist TIME only" ); - } - break; - case TIMESTAMP: - type = isDate ? StandardBasicTypes.TIMESTAMP.getName() : StandardBasicTypes.CALENDAR.getName(); - break; - default: - throw new AssertionFailure( "Unknown temporal type: " + temporalType ); - } - return type; - } - else { - if ( temporalAnnotation != null ) { - throw new AnnotationException( - "@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + mappedAttribute - .getName() - ); - } - } - return null; - } + if ( isTemporalType( mappedAttribute.getAttributeType() ) ) { + if ( temporalAnnotation == null ) { + //SPEC 11.1.47 The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. + throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is a Temporal type, but no @Temporal annotation found." ); + } + TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class ); + boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() ); + String type; + switch ( temporalType ) { + case DATE: + type = isDate ? StandardBasicTypes.DATE.getName() : StandardBasicTypes.CALENDAR_DATE.getName(); + break; + case TIME: + type = StandardBasicTypes.TIME.getName(); + if ( !isDate ) { + throw new NotYetImplementedException( "Calendar cannot persist TIME only" ); + } + break; + case TIMESTAMP: + type = isDate ? StandardBasicTypes.TIMESTAMP.getName() : StandardBasicTypes.CALENDAR.getName(); + break; + default: + throw new AssertionFailure( "Unknown temporal type: " + temporalType ); + } + return type; + } + else { + if ( temporalAnnotation != null ) { + throw new AnnotationException( + "@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + mappedAttribute + .getName() + ); + } + } + return null; + } - @Override - protected AnnotationInstance getAnnotationInstance() { - return JandexHelper.getSingleAnnotation( - mappedAttribute.annotations(), - JPADotNames.TEMPORAL - ); - } - - private static boolean isTemporalType(Class type) { - return Date.class.isAssignableFrom( type ) || Calendar.class.isAssignableFrom( type ); - } + @Override + protected AnnotationInstance getTypeDeterminingAnnotationInstance() { + return JandexHelper.getSingleAnnotation( + mappedAttribute.annotations(), + JPADotNames.TEMPORAL + ); + } + private static boolean isTemporalType(Class type) { + return Date.class.isAssignableFrom( type ) || Calendar.class.isAssignableFrom( type ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/package-info.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/package-info.java new file mode 100644 index 0000000000..98888c4dcb --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/type/package-info.java @@ -0,0 +1,28 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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 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 + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.attribute.type; + +/** + * This package contains type binding code for basic attributes. + */ \ No newline at end of file