28
28
import java .lang .invoke .TypeDescriptor ;
29
29
import java .util .stream .Stream ;
30
30
31
+ import jdk .internal .constant .PrimitiveClassDescImpl ;
32
+ import jdk .internal .constant .ReferenceClassDescImpl ;
31
33
import sun .invoke .util .Wrapper ;
32
34
33
- import static java .lang .constant .ConstantUtils .binaryToInternal ;
34
- import static java .lang .constant .ConstantUtils .dropLastChar ;
35
- import static java .lang .constant .ConstantUtils .internalToBinary ;
36
- import static java .lang .constant .ConstantUtils .validateMemberName ;
37
- import static java .util .Objects .requireNonNull ;
38
35
import static java .util .stream .Collectors .joining ;
36
+ import static jdk .internal .constant .ConstantUtils .MAX_ARRAY_TYPE_DESC_DIMENSIONS ;
37
+ import static jdk .internal .constant .ConstantUtils .arrayDepth ;
38
+ import static jdk .internal .constant .ConstantUtils .binaryToInternal ;
39
+ import static jdk .internal .constant .ConstantUtils .dropFirstAndLastChar ;
40
+ import static jdk .internal .constant .ConstantUtils .internalToBinary ;
41
+ import static jdk .internal .constant .ConstantUtils .validateBinaryClassName ;
42
+ import static jdk .internal .constant .ConstantUtils .validateInternalClassName ;
43
+ import static jdk .internal .constant .ConstantUtils .validateMemberName ;
39
44
40
45
/**
41
46
* A <a href="package-summary.html#nominal">nominal descriptor</a> for a
@@ -77,7 +82,7 @@ public sealed interface ClassDesc
77
82
* @see ClassDesc#ofInternalName(String)
78
83
*/
79
84
static ClassDesc of (String name ) {
80
- ConstantUtils . validateBinaryClassName (requireNonNull ( name ) );
85
+ validateBinaryClassName (name );
81
86
return ClassDesc .ofDescriptor ("L" + binaryToInternal (name ) + ";" );
82
87
}
83
88
@@ -103,7 +108,7 @@ static ClassDesc of(String name) {
103
108
* @since 20
104
109
*/
105
110
static ClassDesc ofInternalName (String name ) {
106
- ConstantUtils . validateInternalClassName (requireNonNull ( name ) );
111
+ validateInternalClassName (name );
107
112
return ClassDesc .ofDescriptor ("L" + name + ";" );
108
113
}
109
114
@@ -122,11 +127,11 @@ static ClassDesc ofInternalName(String name) {
122
127
* not in the correct format
123
128
*/
124
129
static ClassDesc of (String packageName , String className ) {
125
- ConstantUtils . validateBinaryClassName (requireNonNull ( packageName ) );
130
+ validateBinaryClassName (packageName );
126
131
if (packageName .isEmpty ()) {
127
132
return of (className );
128
133
}
129
- validateMemberName (requireNonNull ( className ) , false );
134
+ validateMemberName (className , false );
130
135
return ofDescriptor ("L" + binaryToInternal (packageName ) +
131
136
"/" + className + ";" );
132
137
}
@@ -162,7 +167,7 @@ static ClassDesc ofDescriptor(String descriptor) {
162
167
return (descriptor .length () == 1 )
163
168
? Wrapper .forPrimitiveType (descriptor .charAt (0 )).classDescriptor ()
164
169
// will throw IAE on descriptor.length == 0 or if array dimensions too long
165
- : new ReferenceClassDescImpl (descriptor );
170
+ : ReferenceClassDescImpl . of (descriptor );
166
171
}
167
172
168
173
/**
@@ -175,13 +180,18 @@ static ClassDesc ofDescriptor(String descriptor) {
175
180
* @jvms 4.4.1 The CONSTANT_Class_info Structure
176
181
*/
177
182
default ClassDesc arrayType () {
178
- int depth = ConstantUtils .arrayDepth (descriptorString ());
179
- if (depth >= ConstantUtils .MAX_ARRAY_TYPE_DESC_DIMENSIONS ) {
183
+ String desc = descriptorString ();
184
+ int depth = arrayDepth (desc );
185
+ if (depth >= MAX_ARRAY_TYPE_DESC_DIMENSIONS ) {
180
186
throw new IllegalStateException (
181
187
"Cannot create an array type descriptor with more than " +
182
- ConstantUtils . MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions" );
188
+ MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions" );
183
189
}
184
- return arrayType (1 );
190
+ String newDesc = "[" .concat (desc );
191
+ if (desc .length () == 1 && desc .charAt (0 ) == 'V' ) {
192
+ throw new IllegalArgumentException ("not a valid reference type descriptor: " + newDesc );
193
+ }
194
+ return ReferenceClassDescImpl .ofValidated (newDesc );
185
195
}
186
196
187
197
/**
@@ -196,22 +206,22 @@ default ClassDesc arrayType() {
196
206
* @jvms 4.4.1 The CONSTANT_Class_info Structure
197
207
*/
198
208
default ClassDesc arrayType (int rank ) {
199
- int netRank ;
200
209
if (rank <= 0 ) {
201
210
throw new IllegalArgumentException ("rank " + rank + " is not a positive value" );
202
211
}
203
- try {
204
- int currentDepth = ConstantUtils .arrayDepth (descriptorString ());
205
- netRank = Math .addExact (currentDepth , rank );
206
- if (netRank > ConstantUtils .MAX_ARRAY_TYPE_DESC_DIMENSIONS ) {
207
- throw new IllegalArgumentException ("rank: " + netRank +
208
- " exceeds maximum supported dimension of " +
209
- ConstantUtils .MAX_ARRAY_TYPE_DESC_DIMENSIONS );
210
- }
211
- } catch (ArithmeticException ae ) {
212
- throw new IllegalArgumentException ("Integer overflow in rank computation" );
212
+ String desc = descriptorString ();
213
+ long currentDepth = arrayDepth (desc );
214
+ long netRank = currentDepth + rank ;
215
+ if (netRank > MAX_ARRAY_TYPE_DESC_DIMENSIONS ) {
216
+ throw new IllegalArgumentException ("rank: " + netRank +
217
+ " exceeds maximum supported dimension of " +
218
+ MAX_ARRAY_TYPE_DESC_DIMENSIONS );
219
+ }
220
+ String newDesc = new StringBuilder (desc .length () + rank ).repeat ('[' , rank ).append (desc ).toString ();
221
+ if (desc .length () == 1 && desc .charAt (0 ) == 'V' ) {
222
+ throw new IllegalArgumentException ("not a valid reference type descriptor: " + newDesc );
213
223
}
214
- return ClassDesc . ofDescriptor ( "[" . repeat ( rank ) + descriptorString () );
224
+ return ReferenceClassDescImpl . ofValidated ( newDesc );
215
225
}
216
226
217
227
/**
@@ -235,7 +245,10 @@ default ClassDesc nested(String nestedName) {
235
245
validateMemberName (nestedName , false );
236
246
if (!isClassOrInterface ())
237
247
throw new IllegalStateException ("Outer class is not a class or interface type" );
238
- return ClassDesc .ofDescriptor (dropLastChar (descriptorString ()) + "$" + nestedName + ";" );
248
+ String desc = descriptorString ();
249
+ StringBuilder sb = new StringBuilder (desc .length () + nestedName .length () + 1 );
250
+ sb .append (desc , 0 , desc .length () - 1 ).append ('$' ).append (nestedName ).append (';' );
251
+ return ReferenceClassDescImpl .ofValidated (sb .toString ());
239
252
}
240
253
241
254
/**
@@ -255,7 +268,7 @@ default ClassDesc nested(String firstNestedName, String... moreNestedNames) {
255
268
if (!isClassOrInterface ())
256
269
throw new IllegalStateException ("Outer class is not a class or interface type" );
257
270
validateMemberName (firstNestedName , false );
258
- requireNonNull ( moreNestedNames );
271
+ // implicit null-check
259
272
for (String addNestedNames : moreNestedNames ) {
260
273
validateMemberName (addNestedNames , false );
261
274
}
@@ -299,7 +312,15 @@ default boolean isClassOrInterface() {
299
312
* if this descriptor does not describe an array type
300
313
*/
301
314
default ClassDesc componentType () {
302
- return isArray () ? ClassDesc .ofDescriptor (descriptorString ().substring (1 )) : null ;
315
+ if (isArray ()) {
316
+ String desc = descriptorString ();
317
+ if (desc .length () == 2 ) {
318
+ return Wrapper .forBasicType (desc .charAt (1 )).classDescriptor ();
319
+ } else {
320
+ return ReferenceClassDescImpl .ofValidated (desc .substring (1 ));
321
+ }
322
+ }
323
+ return null ;
303
324
}
304
325
305
326
/**
@@ -312,9 +333,9 @@ default ClassDesc componentType() {
312
333
default String packageName () {
313
334
if (!isClassOrInterface ())
314
335
return "" ;
315
- String className = internalToBinary ( ConstantUtils . dropFirstAndLastChar ( descriptorString ()) );
316
- int index = className .lastIndexOf ('. ' );
317
- return (index == -1 ) ? "" : className .substring (0 , index );
336
+ String desc = descriptorString ();
337
+ int index = desc .lastIndexOf ('/ ' );
338
+ return (index == -1 ) ? "" : internalToBinary ( desc .substring (1 , index ) );
318
339
}
319
340
320
341
/**
@@ -332,11 +353,11 @@ default String displayName() {
332
353
if (isPrimitive ())
333
354
return Wrapper .forBasicType (descriptorString ().charAt (0 )).primitiveSimpleName ();
334
355
else if (isClassOrInterface ()) {
335
- return descriptorString (). substring ( Math . max ( 1 , descriptorString (). lastIndexOf ( '/' ) + 1 ),
336
- descriptorString () .length () - 1 );
356
+ String desc = descriptorString ();
357
+ return desc . substring ( Math . max ( 1 , desc . lastIndexOf ( '/' ) + 1 ), desc .length () - 1 );
337
358
}
338
359
else if (isArray ()) {
339
- int depth = ConstantUtils . arrayDepth (descriptorString ());
360
+ int depth = arrayDepth (descriptorString ());
340
361
ClassDesc c = this ;
341
362
for (int i =0 ; i <depth ; i ++)
342
363
c = c .componentType ();
1 commit comments
openjdk-notifier[bot] commentedon May 17, 2024
Review
Issues