ArrayType.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. import org.apache.bcel.Const;

  19. /**
  20.  * Denotes array type, such as int[][]
  21.  */
  22. public final class ArrayType extends ReferenceType {

  23.     private final int dimensions;
  24.     private final Type basicType;

  25.     /**
  26.      * Convenience constructor for array type, e.g. int[]
  27.      *
  28.      * @param type array type, e.g. T_INT
  29.      * @param dimensions array dimensions
  30.      */
  31.     public ArrayType(final byte type, final int dimensions) {
  32.         this(BasicType.getType(type), dimensions);
  33.     }

  34.     /**
  35.      * Convenience constructor for reference array type, e.g. Object[]
  36.      *
  37.      * @param className complete name of class ({@link String}, for example)
  38.      * @param dimensions array dimensions
  39.      */
  40.     public ArrayType(final String className, final int dimensions) {
  41.         this(ObjectType.getInstance(className), dimensions);
  42.     }

  43.     /**
  44.      * Constructor for array of given type
  45.      *
  46.      * @param type type of array (may be an array itself)
  47.      * @param dimensions array dimensions
  48.      */
  49.     public ArrayType(final Type type, final int dimensions) {
  50.         super(Const.T_ARRAY, "<dummy>");
  51.         if (dimensions < 1 || dimensions > Const.MAX_BYTE) {
  52.             throw new ClassGenException("Invalid number of dimensions: " + dimensions);
  53.         }
  54.         switch (type.getType()) {
  55.         case Const.T_ARRAY:
  56.             final ArrayType array = (ArrayType) type;
  57.             this.dimensions = dimensions + array.dimensions;
  58.             basicType = array.basicType;
  59.             break;
  60.         case Const.T_VOID:
  61.             throw new ClassGenException("Invalid type: void[]");
  62.         default: // Basic type or reference
  63.             this.dimensions = dimensions;
  64.             basicType = type;
  65.             break;
  66.         }
  67.         final StringBuilder buf = new StringBuilder();
  68.         for (int i = 0; i < this.dimensions; i++) {
  69.             buf.append('[');
  70.         }
  71.         buf.append(basicType.getSignature());
  72.         this.signature = buf.toString();
  73.     }

  74.     /**
  75.      * @return true if both type objects refer to the same array type.
  76.      */
  77.     @Override
  78.     public boolean equals(final Object type) {
  79.         if (type instanceof ArrayType) {
  80.             final ArrayType array = (ArrayType) type;
  81.             return array.dimensions == dimensions && array.basicType.equals(basicType);
  82.         }
  83.         return false;
  84.     }

  85.     /**
  86.      * @return basic type of array, i.e., for int[][][] the basic type is int
  87.      */
  88.     public Type getBasicType() {
  89.         return basicType;
  90.     }

  91.     /**
  92.      * Gets the name of referenced class.
  93.      *
  94.      * @return name of referenced class.
  95.      * @since 6.7.0
  96.      */
  97.     @Override
  98.     public String getClassName() {
  99.         return signature;
  100.     }

  101.     /**
  102.      * @return number of dimensions of array
  103.      */
  104.     public int getDimensions() {
  105.         return dimensions;
  106.     }

  107.     /**
  108.      * @return element type of array, i.e., for int[][][] the element type is int[][]
  109.      */
  110.     public Type getElementType() {
  111.         if (dimensions == 1) {
  112.             return basicType;
  113.         }
  114.         return new ArrayType(basicType, dimensions - 1);
  115.     }

  116.     /**
  117.      * @return a hash code value for the object.
  118.      */
  119.     @Override
  120.     public int hashCode() {
  121.         return basicType.hashCode() ^ dimensions;
  122.     }
  123. }