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.Type;
import java.lang.reflect.TypeVariable;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.jboss.logging.Logger;
/**
@ -140,7 +140,7 @@ public class AttributeConverterDefinition {
+ "] specified more than 2 parameterized types"
);
}
entityAttributeType = extractClass(attributeConverterSignature.getActualTypeArguments()[0]);
entityAttributeType = extractClass( attributeConverterSignature.getActualTypeArguments()[0] );
if ( entityAttributeType == null ) {
throw new AnnotationException(
"Could not determine 'entity attribute' type from given AttributeConverter [" +
@ -197,12 +197,13 @@ public class AttributeConverterDefinition {
return (Class) boundTypes[0];
}
private static Class extractClass(Type type) {
if(type instanceof Class) {
if ( type instanceof Class ) {
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;
}

View File

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