|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.boot.model.internal; |
| 6 | + |
| 7 | +import org.hibernate.AnnotationException; |
| 8 | +import org.hibernate.annotations.AttributeBinderType; |
| 9 | +import org.hibernate.annotations.TypeBinderType; |
| 10 | +import org.hibernate.binder.AttributeBinder; |
| 11 | +import org.hibernate.binder.TypeBinder; |
| 12 | +import org.hibernate.boot.spi.MetadataBuildingContext; |
| 13 | +import org.hibernate.mapping.Component; |
| 14 | +import org.hibernate.mapping.PersistentClass; |
| 15 | +import org.hibernate.mapping.Property; |
| 16 | + |
| 17 | +import java.lang.annotation.Annotation; |
| 18 | + |
| 19 | +import static org.hibernate.internal.util.GenericsHelper.typeArguments; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Gavin King |
| 23 | + * @since 7.3 |
| 24 | + */ |
| 25 | +public class Binders { |
| 26 | + static <A extends Annotation> void callTypeBinder( |
| 27 | + Annotation annotation, Class<A> annotationType, |
| 28 | + Component embeddable, |
| 29 | + MetadataBuildingContext context) { |
| 30 | + try { |
| 31 | + typeBinder( annotationType ) |
| 32 | + .bind( annotationType.cast( annotation ), |
| 33 | + context, embeddable ); |
| 34 | + } |
| 35 | + catch (Exception e) { |
| 36 | + throw new AnnotationException( |
| 37 | + "Error processing @TypeBinderType annotation '%s' for embeddable type '%s'" |
| 38 | + .formatted( annotation, embeddable.getComponentClassName() ), e ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static <A extends Annotation> void callTypeBinder( |
| 43 | + Annotation annotation, Class<A> annotationType, |
| 44 | + PersistentClass entity, |
| 45 | + MetadataBuildingContext context) { |
| 46 | + try { |
| 47 | + typeBinder( annotationType ) |
| 48 | + .bind( annotationType.cast( annotation ), |
| 49 | + context, entity ); |
| 50 | + } |
| 51 | + catch (Exception e) { |
| 52 | + throw new AnnotationException( |
| 53 | + "Error processing @TypeBinderType annotation '%s' for entity type '%s'" |
| 54 | + .formatted( annotation, entity.getClassName() ), e ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static <A extends Annotation> void callPropertyBinder( |
| 59 | + Annotation annotation, Class<A> annotationType, |
| 60 | + PersistentClass entity, Property property, |
| 61 | + MetadataBuildingContext context) { |
| 62 | + try { |
| 63 | + propertyBinder( annotationType ) |
| 64 | + .bind( annotationType.cast( annotation ), |
| 65 | + context, entity, property ); |
| 66 | + } |
| 67 | + catch (Exception e) { |
| 68 | + throw new AnnotationException( |
| 69 | + "error processing @AttributeBinderType annotation '%s' for attribute '%s' of entity type '%s'" |
| 70 | + .formatted( annotation, property.getName(), entity.getClassName() ), e ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static <A extends Annotation> TypeBinder<A> typeBinder(Class<A> annotationType) |
| 75 | + throws Exception { |
| 76 | + final var binderType = |
| 77 | + annotationType.getAnnotation( TypeBinderType.class ) |
| 78 | + .binder(); |
| 79 | + checkImplementedTypeArgument( annotationType, binderType, TypeBinder.class ); |
| 80 | + @SuppressWarnings("unchecked") // Safe, we just checked |
| 81 | + final var castBinderType = (Class<? extends TypeBinder<A>>) binderType; |
| 82 | + return castBinderType.getDeclaredConstructor().newInstance(); |
| 83 | + } |
| 84 | + |
| 85 | + private static <A extends Annotation> AttributeBinder<A> propertyBinder(Class<A> annotationType) |
| 86 | + throws Exception { |
| 87 | + final var binderType = |
| 88 | + annotationType.getAnnotation( AttributeBinderType.class ) |
| 89 | + .binder(); |
| 90 | + checkImplementedTypeArgument( annotationType, binderType, PropertyBinder.class ); |
| 91 | + @SuppressWarnings("unchecked") // Safe, we just checked |
| 92 | + final var castBinderType = (Class<? extends AttributeBinder<A>>) binderType; |
| 93 | + return castBinderType.getDeclaredConstructor().newInstance(); |
| 94 | + } |
| 95 | + |
| 96 | + private static void checkImplementedTypeArgument( |
| 97 | + Class<? extends Annotation> annotationType, |
| 98 | + Class<?> binderType, Class<?> implementedType) { |
| 99 | + final var args = typeArguments( implementedType, binderType ); |
| 100 | + if ( args.length == 1 ) { |
| 101 | + final var requiredAnnotationType = args[0]; |
| 102 | + if ( annotationType != requiredAnnotationType ) { |
| 103 | + throw new AnnotationException( |
| 104 | + "Wrong kind of binder for annotation type:" |
| 105 | + + " '%s' does not accept an annotation of type '%s'" |
| 106 | + .formatted( binderType.getTypeName(), |
| 107 | + annotationType.getTypeName() ) |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments