HHH-8804 AttributeConverterDefinition now extracts the raw class from

parameterized type arguments to AttributeConverter
This commit is contained in:
Svein 2015-05-08 17:26:15 +02:00 committed by Steve Ebersole
parent 23794bf294
commit 99e1250e4e
2 changed files with 56 additions and 3 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 = (Class) attributeConverterSignature.getActualTypeArguments()[0];
entityAttributeType = extractClass(attributeConverterSignature.getActualTypeArguments()[0]);
if ( entityAttributeType == null ) {
throw new AnnotationException(
"Could not determine 'entity attribute' type from given AttributeConverter [" +
@ -148,7 +148,7 @@ public class AttributeConverterDefinition {
);
}
databaseColumnType = (Class) attributeConverterSignature.getActualTypeArguments()[1];
databaseColumnType = extractClass(attributeConverterSignature.getActualTypeArguments()[1]);
if ( databaseColumnType == null ) {
throw new AnnotationException(
"Could not determine 'database column' type from given AttributeConverter [" +
@ -197,6 +197,15 @@ public class AttributeConverterDefinition {
return (Class) boundTypes[0];
}
private static Class extractClass(Type type) {
if(type instanceof Class) {
return (Class) type;
} else if (type instanceof ParameterizedType) {
return extractClass(((ParameterizedType) type).getRawType());
}
return null;
}
@Override
public String toString() {

View File

@ -0,0 +1,44 @@
package org.hibernate.test.cfg;
import static org.junit.Assert.assertEquals;
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;
/**
* Tests creation of AttributeConverterDefinition instances and
* the classes of the extracted type arguments
*
* @author Svein Baardsen
*/
public class AttributeConverterDefinitionTest extends BaseUnitTestCase {
public static class CustomAttributeConverter implements AttributeConverter<List<String>, Integer> {
@Override
public Integer convertToDatabaseColumn(List<String> attribute) {
return attribute.size();
}
@Override
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());
}
}