HHH-8804 - Ability to use parametrized type as AttributeConverter type parameter

This commit is contained in:
Steve Ebersole 2015-05-12 23:34:20 -05:00
parent 99e1250e4e
commit 48cafb2664
2 changed files with 18 additions and 18 deletions

View File

@ -26,12 +26,12 @@ package org.hibernate.cfg;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable; import java.lang.reflect.TypeVariable;
import javax.persistence.AttributeConverter; import javax.persistence.AttributeConverter;
import javax.persistence.Converter; import javax.persistence.Converter;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
/** /**
@ -140,7 +140,7 @@ public class AttributeConverterDefinition {
+ "] specified more than 2 parameterized types" + "] specified more than 2 parameterized types"
); );
} }
entityAttributeType = extractClass(attributeConverterSignature.getActualTypeArguments()[0]); entityAttributeType = extractClass( attributeConverterSignature.getActualTypeArguments()[0] );
if ( entityAttributeType == null ) { if ( entityAttributeType == null ) {
throw new AnnotationException( throw new AnnotationException(
"Could not determine 'entity attribute' type from given AttributeConverter [" + "Could not determine 'entity attribute' type from given AttributeConverter [" +
@ -199,10 +199,11 @@ public class AttributeConverterDefinition {
} }
private static Class extractClass(Type type) { private static Class extractClass(Type type) {
if(type instanceof Class) { if ( type instanceof Class ) {
return (Class) type; return (Class) type;
} else if (type instanceof ParameterizedType) { }
return extractClass(((ParameterizedType) type).getRawType()); else if ( type instanceof ParameterizedType ) {
return extractClass( ( (ParameterizedType) type ).getRawType() );
} }
return null; return null;
} }

View File

@ -1,27 +1,27 @@
package org.hibernate.test.cfg; package org.hibernate.test.type;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.AttributeConverter; import javax.persistence.AttributeConverter;
import org.hibernate.cfg.AttributeConverterDefinition; import org.hibernate.cfg.AttributeConverterDefinition;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
/** /**
* Tests creation of AttributeConverterDefinition instances and * Test the ability to interpret and understand AttributeConverter impls which
* the classes of the extracted type arguments * use parameterized types as one of (typically the "attribute type") its parameter types.
* *
* @author Svein Baardsen * @author Svein Baardsen
* @author Steve Ebersole
*/ */
public class AttributeConverterDefinitionTest extends BaseUnitTestCase { public class ParameterizedAttributeConverterParameterTypeTest extends BaseUnitTestCase {
public static class CustomAttributeConverter implements AttributeConverter<List<String>, Integer> { public static class CustomAttributeConverter implements AttributeConverter<List<String>, Integer> {
@Override @Override
public Integer convertToDatabaseColumn(List<String> attribute) { public Integer convertToDatabaseColumn(List<String> attribute) {
return attribute.size(); return attribute.size();
@ -31,14 +31,13 @@ public class AttributeConverterDefinitionTest extends BaseUnitTestCase {
public List<String> convertToEntityAttribute(Integer dbData) { public List<String> convertToEntityAttribute(Integer dbData) {
return new ArrayList<String>(dbData); return new ArrayList<String>(dbData);
} }
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8804") @TestForIssue(jiraKey = "HHH-8804")
public void testGenericTypeParameters() { public void testGenericTypeParameters() {
AttributeConverterDefinition def = AttributeConverterDefinition.from(CustomAttributeConverter.class); AttributeConverterDefinition def = AttributeConverterDefinition.from( CustomAttributeConverter.class );
assertEquals(List.class, def.getEntityAttributeType()); assertEquals( List.class, def.getEntityAttributeType() );
} }
} }