|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.models.bytebuddy.internal; |
| 6 | + |
| 7 | +import java.lang.annotation.Annotation; |
| 8 | +import java.lang.annotation.Documented; |
| 9 | +import java.lang.annotation.Repeatable; |
| 10 | +import java.lang.annotation.Retention; |
| 11 | +import java.lang.annotation.Target; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.function.BiConsumer; |
| 17 | + |
| 18 | +import org.hibernate.models.bytebuddy.spi.ByteBuddyModelsContext; |
| 19 | +import org.hibernate.models.bytebuddy.spi.ValueExtractor; |
| 20 | +import org.hibernate.models.internal.util.CollectionHelper; |
| 21 | +import org.hibernate.models.spi.AnnotationDescriptor; |
| 22 | +import org.hibernate.models.spi.AnnotationDescriptorRegistry; |
| 23 | +import org.hibernate.models.spi.AttributeDescriptor; |
| 24 | +import org.hibernate.models.spi.SourceModelBuildingContext; |
| 25 | + |
| 26 | +import net.bytebuddy.description.annotation.AnnotationDescription; |
| 27 | +import net.bytebuddy.description.annotation.AnnotationList; |
| 28 | +import net.bytebuddy.description.annotation.AnnotationSource; |
| 29 | + |
| 30 | +/** |
| 31 | + * Helper for building annotation usages/instances based on |
| 32 | + * Jandex {@linkplain AnnotationDescription} references |
| 33 | + * |
| 34 | + * @author Steve Ebersole |
| 35 | + */ |
| 36 | +public class AnnotationUsageBuilder { |
| 37 | + public static <A extends Annotation> A makeUsage( |
| 38 | + AnnotationDescription annotationDescription, |
| 39 | + AnnotationDescriptor<A> annotationDescriptor, |
| 40 | + SourceModelBuildingContext modelContext) { |
| 41 | + final Map<String, Object> attributeValues = extractAttributeValues( |
| 42 | + annotationDescription, |
| 43 | + annotationDescriptor, |
| 44 | + modelContext |
| 45 | + ); |
| 46 | + return annotationDescriptor.createUsage( attributeValues, modelContext ); |
| 47 | + } |
| 48 | + |
| 49 | + private static <A extends Annotation> Map<String, Object> extractAttributeValues( |
| 50 | + AnnotationDescription annotationDescription, |
| 51 | + AnnotationDescriptor<A> annotationDescriptor, |
| 52 | + SourceModelBuildingContext modelContext) { |
| 53 | + |
| 54 | + if ( CollectionHelper.isEmpty( annotationDescriptor.getAttributes() ) ) { |
| 55 | + return Collections.emptyMap(); |
| 56 | + } |
| 57 | + |
| 58 | + final ConcurrentHashMap<String, Object> valueMap = new ConcurrentHashMap<>(); |
| 59 | + for ( int i = 0; i < annotationDescriptor.getAttributes().size(); i++ ) { |
| 60 | + final AttributeDescriptor<?> attributeDescriptor = annotationDescriptor.getAttributes().get( i ); |
| 61 | + final ValueExtractor<?> extractor = modelContext |
| 62 | + .as( ByteBuddyModelContextImpl.class ) |
| 63 | + .getValueExtractor( attributeDescriptor.getTypeDescriptor() ); |
| 64 | + final Object attributeValue = extractor.extractValue( |
| 65 | + annotationDescription, |
| 66 | + attributeDescriptor.getName(), |
| 67 | + modelContext |
| 68 | + ); |
| 69 | + valueMap.put( attributeDescriptor.getName(), attributeValue ); |
| 70 | + } |
| 71 | + return valueMap; |
| 72 | + } |
| 73 | + |
| 74 | + public static Map<Class<? extends Annotation>, ? extends Annotation> collectUsages( |
| 75 | + AnnotationSource annotationSource, |
| 76 | + ByteBuddyModelsContext modelContext) { |
| 77 | + if ( annotationSource == null ) { |
| 78 | + return Collections.emptyMap(); |
| 79 | + } |
| 80 | + final Map<Class<? extends Annotation>, Annotation> result = new HashMap<>(); |
| 81 | + processAnnotations( |
| 82 | + annotationSource.getDeclaredAnnotations(), |
| 83 | + result::put, |
| 84 | + modelContext |
| 85 | + ); |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Process annotations creating usage instances passed back to the consumer |
| 91 | + */ |
| 92 | + public static void processAnnotations( |
| 93 | + AnnotationList annotations, |
| 94 | + BiConsumer<Class<? extends Annotation>, Annotation> consumer, |
| 95 | + ByteBuddyModelsContext buildingContext) { |
| 96 | + final AnnotationDescriptorRegistry annotationDescriptorRegistry = buildingContext.getAnnotationDescriptorRegistry(); |
| 97 | + |
| 98 | + for ( AnnotationDescription annotation : annotations ) { |
| 99 | + if ( annotation.getAnnotationType().represents( Documented.class ) |
| 100 | + || annotation.getAnnotationType().represents( Repeatable.class ) |
| 101 | + || annotation.getAnnotationType().represents( Retention.class ) |
| 102 | + || annotation.getAnnotationType().represents( Target.class ) ) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + final Class<? extends Annotation> annotationType = buildingContext |
| 107 | + .getClassLoading() |
| 108 | + .classForName( annotation.getAnnotationType().getTypeName() ); |
| 109 | + final AnnotationDescriptor<?> annotationDescriptor = annotationDescriptorRegistry.getDescriptor( annotationType ); |
| 110 | + final Annotation usage = makeUsage( |
| 111 | + annotation, |
| 112 | + annotationDescriptor, |
| 113 | + buildingContext |
| 114 | + ); |
| 115 | + consumer.accept( annotationType, usage ); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments