HHH-8804 - Ability to use parametrized type as AttributeConverter type parameter
(cherry picked from commit 48cafb2664
)
This commit is contained in:
parent
c2100677f1
commit
021122a2dc
|
@ -26,11 +26,11 @@ package org.hibernate.cfg;
|
|||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
import org.hibernate.AssertionFailure;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
|
@ -64,7 +64,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 [" +
|
||||
|
@ -121,12 +121,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;
|
||||
}
|
||||
|
|
|
@ -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() );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue