Skip to content

Commit

Permalink
HHH-8854 Create AttributeConverterDefinition even if AttributeConverter
Browse files Browse the repository at this point in the history
is only implemented via superclass or interface
  • Loading branch information
Svein authored and sebersole committed May 21, 2015
1 parent 1812bb2 commit b584513
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
Expand Up @@ -9,12 +9,15 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;

import org.jboss.logging.Logger;

/**
Expand Down Expand Up @@ -109,6 +112,12 @@ public AttributeConverterDefinition(AttributeConverter attributeConverter, boole

final Class attributeConverterClass = attributeConverter.getClass();
final ParameterizedType attributeConverterSignature = extractAttributeConverterParameterizedType( attributeConverterClass );
if ( attributeConverterSignature == null ) {
throw new AssertionFailure(
"Could not extract ParameterizedType representation of AttributeConverter definition " +
"from AttributeConverter implementation class [" + attributeConverterClass.getName() + "]"
);
}

if ( attributeConverterSignature.getActualTypeArguments().length < 2 ) {
throw new AnnotationException(
Expand Down Expand Up @@ -140,20 +149,26 @@ public AttributeConverterDefinition(AttributeConverter attributeConverter, boole
}
}

private ParameterizedType extractAttributeConverterParameterizedType(Class attributeConverterClass) {
for ( Type type : attributeConverterClass.getGenericInterfaces() ) {
if ( ParameterizedType.class.isInstance( type ) ) {
final ParameterizedType parameterizedType = (ParameterizedType) type;
if ( AttributeConverter.class.equals( parameterizedType.getRawType() ) ) {
private ParameterizedType extractAttributeConverterParameterizedType(Type base) {
if ( base != null ) {
Class clazz = extractClass( base );
List<Type> types = new ArrayList<Type>();
types.add( clazz.getGenericSuperclass() );
types.addAll( Arrays.asList( clazz.getGenericInterfaces() ) );
for ( Type type : types ) {
if ( ParameterizedType.class.isInstance( type ) ) {
final ParameterizedType parameterizedType = (ParameterizedType) type;
if ( AttributeConverter.class.equals( parameterizedType.getRawType() ) ) {
return parameterizedType;
}
}
ParameterizedType parameterizedType = extractAttributeConverterParameterizedType( type );
if ( parameterizedType != null ) {
return parameterizedType;
}
}
}

throw new AssertionFailure(
"Could not extract ParameterizedType representation of AttributeConverter definition " +
"from AttributeConverter implementation class [" + attributeConverterClass.getName() + "]"
);
return null;
}

public AttributeConverter getAttributeConverter() {
Expand Down
@@ -0,0 +1,65 @@
package org.hibernate.test.type;

import static org.junit.Assert.assertEquals;

import javax.persistence.AttributeConverter;

import org.hibernate.cfg.AttributeConverterDefinition;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;

/**
* Test the ability to interpret and understand AttributeConverter impls when the base class does not
* explicitly implement AttributeConverter but implements it via an interface or superclass.
*
* @author Svein Baardsen
*/
@TestForIssue(jiraKey = "HHH-8854")
public class AttributeConverterOnSuperclassTest extends BaseUnitTestCase {

public static class StringIntegerAttributeConverter implements AttributeConverter<String, Integer> {

@Override
public Integer convertToDatabaseColumn(String attribute) {
return Integer.valueOf( attribute );
}

@Override
public String convertToEntityAttribute(Integer dbData) {
return String.valueOf( dbData );
}
}

public static class StringIntegerConverterSubclass extends StringIntegerAttributeConverter {
}

@Test
public void testAttributeConverterOnSuperclass() {
AttributeConverterDefinition def = AttributeConverterDefinition.from( StringIntegerConverterSubclass.class );
assertEquals( String.class, def.getEntityAttributeType() );
}

public interface StringLongAttributeConverter extends AttributeConverter<String, Long> {
}

public static class StringLongAttributeConverterImpl implements StringLongAttributeConverter {

@Override
public Long convertToDatabaseColumn(String attribute) {
return Long.valueOf( attribute );
}

@Override
public String convertToEntityAttribute(Long dbData) {
return String.valueOf( dbData );
}
}

@Test
public void testAttributeConverterOnInterface() {
AttributeConverterDefinition def = AttributeConverterDefinition.from( StringLongAttributeConverterImpl.class );
assertEquals( String.class, def.getEntityAttributeType() );
}

}

0 comments on commit b584513

Please sign in to comment.