make @AttributeAccessor annotation typesafe

This commit is contained in:
Gavin King 2021-03-02 23:57:43 +01:00
parent ef69465f48
commit 4b56842c7f
4 changed files with 24 additions and 4 deletions

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.annotations;
import org.hibernate.property.access.spi.PropertyAccessStrategy;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
@ -43,6 +45,13 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
public @interface AttributeAccessor {
/**
* Names the {@link org.hibernate.property.access.spi.PropertyAccessStrategy} strategy.
*
* @deprecated use {@link #strategy()}
*/
String value();
@Deprecated
String value() default "";
/**
* A class implementing {@link org.hibernate.property.access.spi.PropertyAccessStrategy}.
*/
Class<? extends PropertyAccessStrategy> strategy() default PropertyAccessStrategy.class;
}

View File

@ -41,6 +41,7 @@ import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.ToOne;
import org.hibernate.mapping.Value;
import org.hibernate.property.access.spi.PropertyAccessStrategy;
import org.hibernate.tuple.AnnotationValueGeneration;
import org.hibernate.tuple.GenerationTiming;
import org.hibernate.tuple.ValueGeneration;
@ -276,7 +277,17 @@ public class PropertyBinder {
if ( property.isAnnotationPresent( AttributeAccessor.class ) ) {
final AttributeAccessor accessor = property.getAnnotation( AttributeAccessor.class );
prop.setPropertyAccessorName( accessor.value() );
String value = accessor.value();
Class<?> type = accessor.strategy();
if ( !value.isEmpty() ) {
prop.setPropertyAccessorName( value );
}
else if ( !PropertyAccessStrategy.class.equals(type) ) {
prop.setPropertyAccessorName( type.getName() );
}
else {
throw new AnnotationException("@AttributeAccessor must specify a PropertyAccessStrategy type");
}
}
}

View File

@ -83,7 +83,7 @@ public class AttributeAccessorTest {
this.id = id;
}
@AttributeAccessor("org.hibernate.orm.test.bootstrap.binding.annotations.access.AttributeAccessorTest$BasicAttributeAccessor")
@AttributeAccessor(strategy =BasicAttributeAccessor.class)
public String getName() {
return name;
}

View File

@ -78,7 +78,7 @@ public class AttributeAccessorTest extends BaseEnversJPAFunctionalTestCase {
this.id = id;
}
@AttributeAccessor( "org.hibernate.envers.test.integration.accesstype.AttributeAccessorTest$BasicAttributeAccessor" )
@AttributeAccessor( strategy =BasicAttributeAccessor.class )
public String getName() {
return name;
}