1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "memory/oopFactory.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "oops/typeArrayKlass.hpp"
  32 #include "runtime/fieldType.hpp"
  33 #include "runtime/signature.hpp"
  34 
  35 BasicType FieldType::basic_type(Symbol* signature) {
  36   return char2type(signature->char_at(0));
  37 }
  38 
  39 // Check if it is a valid array signature
  40 bool FieldType::is_valid_array_signature(Symbol* sig) {
  41   assert(sig->utf8_length() > 1, "this should already have been checked");
  42   assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
  43   // The first character is already checked
  44   int i = 1;
  45   int len = sig->utf8_length();
  46   // First skip all '['s
  47   while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
  48 
  49   // Check type
  50   switch(sig->char_at(i)) {
  51     case JVM_SIGNATURE_BYTE:
  52     case JVM_SIGNATURE_CHAR:
  53     case JVM_SIGNATURE_DOUBLE:
  54     case JVM_SIGNATURE_FLOAT:
  55     case JVM_SIGNATURE_INT:
  56     case JVM_SIGNATURE_LONG:
  57     case JVM_SIGNATURE_SHORT:
  58     case JVM_SIGNATURE_BOOLEAN:
  59       // If it is an array, the type is the last character
  60       return (i + 1 == len);
  61     case JVM_SIGNATURE_CLASS:
  62       // If it is an object, the last character must be a ';'
  63       return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
  64   }
  65 
  66   return false;
  67 }
  68 
  69 
  70 BasicType FieldType::get_array_info(Symbol* signature, FieldArrayInfo& fd, TRAPS) {
  71   assert(basic_type(signature) == T_ARRAY, "must be array");
  72   int index = 1;
  73   int dim   = 1;
  74   while (signature->char_at(index) == JVM_SIGNATURE_ARRAY) {
  75     index++;
  76     dim++;
  77   }
  78   ResourceMark rm;
  79   char *element = signature->as_C_string() + index;
  80   BasicType element_type = char2type(element[0]);
  81   if (element_type == T_OBJECT) {
  82     int len = (int)strlen(element);
  83     assert(element[len-1] == JVM_SIGNATURE_ENDCLASS, "last char should be a semicolon");
  84     element[len-1] = '\0';        // chop off semicolon
  85     fd._object_key = SymbolTable::new_symbol(element + 1);
  86   }
  87   // Pass dimension back to caller
  88   fd._dimension = dim;
  89   return element_type;
  90 }