< prev index next >

src/java.base/share/classes/java/lang/constant/ConstantUtils.java

Print this page

287                                 legal = true;
288                         }
289                     }
290                     return 0;
291                 case JVM_SIGNATURE_ARRAY:
292                     arrayDim++;
293                     if (arrayDim > MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
294                         throw new IllegalArgumentException(String.format("Cannot create an array type descriptor with more than %d dimensions",
295                                 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS));
296                     }
297                     // The rest of what's there better be a legal descriptor
298                     index++;
299                     voidOK = false;
300                     break;
301                 default:
302                     return 0;
303             }
304         }
305         return 0;
306     }













































































307 }

287                                 legal = true;
288                         }
289                     }
290                     return 0;
291                 case JVM_SIGNATURE_ARRAY:
292                     arrayDim++;
293                     if (arrayDim > MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
294                         throw new IllegalArgumentException(String.format("Cannot create an array type descriptor with more than %d dimensions",
295                                 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS));
296                     }
297                     // The rest of what's there better be a legal descriptor
298                     index++;
299                     voidOK = false;
300                     break;
301                 default:
302                     return 0;
303             }
304         }
305         return 0;
306     }
307 
308     static boolean verifyUnqualifiedClassName(String name) {
309         for (int index = 0; index < name.length(); index++) {
310             char ch = name.charAt(index);
311             if (ch < 128) {
312                 if (ch == '.' || ch == ';' || ch == '[' ) {
313                     return false;   // do not permit '.', ';', or '['
314                 }
315                 if (ch == '/') {
316                     // check for '//' or leading or trailing '/' which are not legal
317                     // unqualified name must not be empty
318                     if (index == 0 || index + 1 >= name.length() || name.charAt(index + 1) == '/') {
319                         return false;
320                     }
321                 }
322             } else {
323                 index ++;
324             }
325         }
326         return true;
327     }
328 
329     /**
330      * Returns the basic type of the given descriptor.  If {@code verifyClassName}
331      * is true, then this method will validate that the characters at [start, end)
332      * within the given string describe a valid field type descriptor.
333      *
334      * @return the character represents the basic type that the descriptor string
335      * references
336      * @throws IllegalArgumentException if the descriptor string is not valid
337      */
338     static char basicType(String descriptor, int start, int end, boolean verifyClassName) {
339         int arrayDim = 0;
340         int index = start;
341         while (index < end) {
342             char c = descriptor.charAt(index);
343             switch (c) {
344                 case JVM_SIGNATURE_VOID:
345                 case JVM_SIGNATURE_BOOLEAN:
346                 case JVM_SIGNATURE_BYTE:
347                 case JVM_SIGNATURE_CHAR:
348                 case JVM_SIGNATURE_SHORT:
349                 case JVM_SIGNATURE_INT:
350                 case JVM_SIGNATURE_FLOAT:
351                 case JVM_SIGNATURE_LONG:
352                 case JVM_SIGNATURE_DOUBLE:
353                     return c;
354                 case JVM_SIGNATURE_CLASS:
355                     index++;
356                     int indexOfSemi = descriptor.indexOf(';', index);
357                     if (indexOfSemi != -1) {
358                         if (verifyClassName) {
359                             String unqualifiedName = descriptor.substring(index, indexOfSemi);
360                             boolean legal = verifyUnqualifiedClassName(unqualifiedName);
361                             if (!legal) {
362                                 throw new IllegalArgumentException(String.format("not a valid type descriptor: %s", descriptor));
363                             }
364                         }
365                         return c;
366                     }
367                     throw new IllegalArgumentException(String.format("not a valid type descriptor: %s", descriptor));
368                 case JVM_SIGNATURE_ARRAY:
369                     arrayDim++;
370                     if (arrayDim > MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
371                         throw new IllegalArgumentException(String.format("Cannot create an array type descriptor with more than %d dimensions",
372                                 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS));
373                     }
374                     // The rest of what's there better be a legal descriptor
375                     index++;
376                     break;
377                 default:
378                     throw new IllegalArgumentException(String.format("not a valid type descriptor: %s", descriptor));
379             }
380         }
381         throw new IllegalArgumentException(String.format("not a valid type descriptor: %s", descriptor));
382     }
383 
384 }
< prev index next >