View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.generic;
    18
    19  import org.apache.bcel.Constants;
    20
    21  /***
    22   * Denotes array type, such as int[][]
    23   *
    24   * @version $Id: ArrayType.java 386056 2006-03-15 11:31:56Z tcurdt $
    25   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    26   */
    27  public final class ArrayType extends ReferenceType {
    28
    29      private int dimensions;
    30      private Type basic_type;
    31
    32
    33      /***
    34       * Convenience constructor for array type, e.g. int[]
    35       *
    36       * @param type array type, e.g. T_INT
    37       */
    38      public ArrayType(byte type, int dimensions) {
    39          this(BasicType.getType(type), dimensions);
    40      }
    41
    42
    43      /***
    44       * Convenience constructor for reference array type, e.g. Object[]
    45       *
    46       * @param class_name complete name of class (java.lang.String, e.g.)
    47       */
    48      public ArrayType(String class_name, int dimensions) {
    49          this(new ObjectType(class_name), dimensions);
    50      }
    51
    52
    53      /***
    54       * Constructor for array of given type
    55       *
    56       * @param type type of array (may be an array itself)
    57       */
    58      public ArrayType(Type type, int dimensions) {
    59          super(Constants.T_ARRAY, "<dummy>");
    60          if ((dimensions < 1) || (dimensions > Constants.MAX_BYTE)) {
    61              throw new ClassGenException("Invalid number of dimensions: " + dimensions);
    62          }
    63          switch (type.getType()) {
    64              case Constants.T_ARRAY:
    65                  ArrayType array = (ArrayType) type;
    66                  this.dimensions = dimensions + array.dimensions;
    67                  basic_type = array.basic_type;
    68                  break;
    69              case Constants.T_VOID:
    70                  throw new ClassGenException("Invalid type: void[]");
    71              default: // Basic type or reference
    72                  this.dimensions = dimensions;
    73                  basic_type = type;
    74                  break;
    75          }
    76          StringBuffer buf = new StringBuffer();
    77          for (int i = 0; i < this.dimensions; i++) {
    78              buf.append('[');
    79          }
    80          buf.append(basic_type.getSignature());
    81          signature = buf.toString();
    82      }
    83
    84
    85      /***
    86       * @return basic type of array, i.e., for int[][][] the basic type is int
    87       */
    88      public Type getBasicType() {
    89          return basic_type;
    90      }
    91
    92
    93      /***
    94       * @return element type of array, i.e., for int[][][] the element type is int[][]
    95       */
    96      public Type getElementType() {
    97          if (dimensions == 1) {
    98              return basic_type;
    99          }
    100         return new ArrayType(basic_type, dimensions - 1);
    101     }
    102
    103
    104     /*** @return number of dimensions of array
    105      */
    106     public int getDimensions() {
    107         return dimensions;
    108     }
    109
    110
    111     /*** @return a hash code value for the object.
    112      */
    113     public int hashCode() {
    114         return basic_type.hashCode() ^ dimensions;
    115     }
    116
    117
    118     /*** @return true if both type objects refer to the same array type.
    119      */
    120     public boolean equals( Object _type ) {
    121         if (_type instanceof ArrayType) {
    122             ArrayType array = (ArrayType) _type;
    123             return (array.dimensions == dimensions) && array.basic_type.equals(basic_type);
    124         }
    125         return false;
    126     }
    127 }