4
4
*/
5
5
package org .hibernate .models .bytebuddy .internal .values ;
6
6
7
- import java .lang .reflect .Field ;
8
- import java .util .List ;
9
7
10
- import org .hibernate .models .ModelsException ;
11
- import org .hibernate .models .bytebuddy .spi .ByteBuddyModelsContext ;
8
+ import java .lang .annotation .Annotation ;
9
+
10
+ import org .hibernate .models .bytebuddy .internal .AnnotationUsageBuilder ;
12
11
import org .hibernate .models .bytebuddy .spi .ValueConverter ;
12
+ import org .hibernate .models .spi .AnnotationDescriptor ;
13
+ import org .hibernate .models .spi .AnnotationDescriptorRegistry ;
13
14
import org .hibernate .models .spi .SourceModelBuildingContext ;
14
15
import org .hibernate .models .spi .ValueTypeDescriptor ;
15
16
17
+ import net .bytebuddy .description .annotation .AnnotationDescription ;
16
18
import net .bytebuddy .description .annotation .AnnotationValue ;
17
19
18
20
/**
@@ -28,46 +30,171 @@ public ArrayValueConverter(ValueTypeDescriptor<V> elementTypeDescriptor) {
28
30
}
29
31
30
32
@ Override
31
- public V [] convert (AnnotationValue <?,?> byteBuddyValue , SourceModelBuildingContext modelContext ) {
32
- assert byteBuddyValue != null ;
33
+ public V [] convert (AnnotationValue <?,?> annotationValue , SourceModelBuildingContext modelContext ) {
34
+ assert annotationValue != null ;
35
+
36
+ final Class <?> elementValueType = elementTypeDescriptor .getValueType ();
37
+
38
+ if ( Boolean .class == elementValueType ) {
39
+ return convertBooleanArray ( annotationValue , modelContext );
40
+ }
41
+
42
+ if ( Byte .class == elementValueType ) {
43
+ return convertByteArray ( annotationValue , modelContext );
44
+ }
33
45
34
- // UGH...
35
- final List <AnnotationValue <?,?>> byteBuddyValues = extractValueValues ( byteBuddyValue , modelContext );
46
+ if ( Short .class == elementValueType ) {
47
+ return convertShortArray ( annotationValue , modelContext );
48
+ }
49
+
50
+ if ( Integer .class == elementValueType ) {
51
+ return convertIntArray ( annotationValue , modelContext );
52
+ }
36
53
37
- final V [] result = elementTypeDescriptor .makeArray ( byteBuddyValues .size (), modelContext );
38
- final ValueConverter <V > elementWrapper = modelContext .as ( ByteBuddyModelsContext .class ).getValueConverter ( elementTypeDescriptor );
54
+ if ( Long .class == elementValueType ) {
55
+ return convertLongArray ( annotationValue , modelContext );
56
+ }
57
+
58
+ if ( double .class == elementValueType ) {
59
+ return convertDoubleArray ( annotationValue , modelContext );
60
+ }
39
61
40
- for ( int i = 0 ; i < byteBuddyValues . size (); i ++ ) {
41
- result [ i ] = elementWrapper . convert ( byteBuddyValues . get ( i ) , modelContext );
62
+ if ( float . class == elementValueType ) {
63
+ return convertFloatArray ( annotationValue , modelContext );
42
64
}
43
- return result ;
65
+
66
+ if ( Character .class == elementValueType ) {
67
+ return convertCharacterArray ( annotationValue , modelContext );
68
+ }
69
+
70
+ if ( elementValueType .isAnnotation () ) {
71
+ return convertNestedAnnotationArray ( annotationValue , modelContext );
72
+ }
73
+
74
+ final Class <?> arrayType = elementValueType .arrayType ();
75
+ //noinspection unchecked
76
+ return (V []) annotationValue .resolve ( arrayType );
44
77
}
45
78
46
- private List < AnnotationValue <?, ?>> extractValueValues (
47
- AnnotationValue <?, ?> byteBuddyValue ,
79
+ private V [] convertBooleanArray (
80
+ AnnotationValue <?, ?> annotationValue ,
48
81
SourceModelBuildingContext modelContext ) {
49
- try {
50
- //noinspection unchecked
51
- return (List <AnnotationValue <?,?>>) getValuesField ( modelContext ).get ( byteBuddyValue );
82
+ final boolean [] resolved = annotationValue .resolve ( boolean [].class );
83
+ final Boolean [] result = (Boolean []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
84
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
85
+ result [i ] = resolved [i ];
52
86
}
53
- catch (IllegalAccessException e ) {
54
- throw new ModelsException ( "Could not access Byte Buddy's `AnnotationValue.ForDescriptionArray#values` field" , e );
87
+ //noinspection unchecked
88
+ return (V []) result ;
89
+ }
90
+
91
+ private V [] convertByteArray (
92
+ AnnotationValue <?, ?> annotationValue ,
93
+ SourceModelBuildingContext modelContext ) {
94
+ final byte [] resolved = annotationValue .resolve ( byte [].class );
95
+ final Byte [] result = (Byte []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
96
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
97
+ result [i ] = resolved [i ];
55
98
}
99
+ //noinspection unchecked
100
+ return (V []) result ;
56
101
}
57
102
58
- private Field getValuesField (SourceModelBuildingContext modelContext ) {
59
- return ARRRRGGGGHHHHH ;
103
+ private V [] convertShortArray (
104
+ AnnotationValue <?, ?> annotationValue ,
105
+ SourceModelBuildingContext modelContext ) {
106
+ final short [] resolved = annotationValue .resolve ( short [].class );
107
+ final Short [] result = (Short []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
108
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
109
+ result [i ] = resolved [i ];
110
+ }
111
+ //noinspection unchecked
112
+ return (V []) result ;
60
113
}
61
114
62
- private static Field ARRRRGGGGHHHHH ;
115
+ private V [] convertIntArray (
116
+ AnnotationValue <?, ?> annotationValue ,
117
+ SourceModelBuildingContext modelContext ) {
118
+ final int [] resolved = annotationValue .resolve ( int [].class );
119
+ final Integer [] result = (Integer []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
120
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
121
+ result [i ] = resolved [i ];
122
+ }
123
+ //noinspection unchecked
124
+ return (V []) result ;
125
+ }
126
+
127
+ private V [] convertLongArray (
128
+ AnnotationValue <?, ?> annotationValue ,
129
+ SourceModelBuildingContext modelContext ) {
130
+ final long [] resolved = annotationValue .resolve ( long [].class );
131
+ final Long [] result = (Long []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
132
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
133
+ result [i ] = resolved [i ];
134
+ }
135
+ //noinspection unchecked
136
+ return (V []) result ;
137
+ }
138
+
139
+ private V [] convertDoubleArray (
140
+ AnnotationValue <?, ?> annotationValue ,
141
+ SourceModelBuildingContext modelContext ) {
142
+ final double [] resolved = annotationValue .resolve ( double [].class );
143
+ final Double [] result = (Double []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
144
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
145
+ result [i ] = resolved [i ];
146
+ }
147
+ //noinspection unchecked
148
+ return (V []) result ;
149
+ }
150
+
151
+ private V [] convertFloatArray (
152
+ AnnotationValue <?, ?> annotationValue ,
153
+ SourceModelBuildingContext modelContext ) {
154
+ final float [] resolved = annotationValue .resolve ( float [].class );
155
+ final Float [] result = (Float []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
156
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
157
+ result [i ] = resolved [i ];
158
+ }
159
+ //noinspection unchecked
160
+ return (V []) result ;
161
+ }
63
162
64
- static {
65
- try {
66
- ARRRRGGGGHHHHH = AnnotationValue .ForDescriptionArray .class .getDeclaredField ( "values" );
67
- ARRRRGGGGHHHHH .setAccessible ( true );
163
+ private V [] convertCharacterArray (
164
+ AnnotationValue <?, ?> annotationValue ,
165
+ SourceModelBuildingContext modelContext ) {
166
+ final char [] resolved = annotationValue .resolve ( char [].class );
167
+ final Character [] result = (Character []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
168
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
169
+ result [i ] = resolved [i ];
68
170
}
69
- catch (Exception e ) {
70
- throw new ModelsException ( "Could not access Byte Buddy's `AnnotationValue.ForDescriptionArray#values` field" , e );
171
+ //noinspection unchecked
172
+ return (V []) result ;
173
+ }
174
+
175
+ private V [] convertNestedAnnotationArray (
176
+ AnnotationValue <?, ?> annotationValue ,
177
+ SourceModelBuildingContext modelContext ) {
178
+ final AnnotationDescriptorRegistry descriptorRegistry = modelContext .getAnnotationDescriptorRegistry ();
179
+
180
+ //noinspection unchecked
181
+ final Class <? extends Annotation > annotationType = (Class <? extends Annotation >) elementTypeDescriptor .getValueType ();
182
+ final AnnotationDescriptor <? extends Annotation > annotationDescriptor = descriptorRegistry .getDescriptor ( annotationType );
183
+
184
+ final AnnotationDescription [] resolved = annotationValue .resolve ( AnnotationDescription [].class );
185
+ final Annotation [] result = (Annotation []) elementTypeDescriptor .makeArray ( resolved .length , modelContext );
186
+
187
+ for ( int i = 0 ; i < resolved .length ; i ++ ) {
188
+ final AnnotationDescription annotationDescription = resolved [i ];
189
+ final Annotation usage = AnnotationUsageBuilder .makeUsage (
190
+ annotationDescription ,
191
+ annotationDescriptor ,
192
+ modelContext
193
+ );
194
+ result [i ] = usage ;
71
195
}
196
+
197
+ //noinspection unchecked
198
+ return (V []) result ;
72
199
}
73
200
}
0 commit comments