1/* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5/* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21package com.sun.org.apache.bcel.internal.generic; 22 23import com.sun.org.apache.bcel.internal.Const; 24 25/** 26 * Denotes array type, such as int[][] 27 * 28 * @version $Id: ArrayType.java 1749603 2016-06-21 20:50:19Z ggregory $ 29 */ 30public final class ArrayType extends ReferenceType { 31 32 private int dimensions; 33 private Type basic_type; 34 35 /** 36 * Convenience constructor for array type, e.g. int[] 37 * 38 * @param type array type, e.g. T_INT 39 */ 40 public ArrayType(final byte type, final int dimensions) { 41 this(BasicType.getType(type), dimensions); 42 } 43 44 /** 45 * Convenience constructor for reference array type, e.g. Object[] 46 * 47 * @param class_name complete name of class (java.lang.String, e.g.) 48 */ 49 public ArrayType(final String class_name, final int dimensions) { 50 this(ObjectType.getInstance(class_name), dimensions); 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(final Type type, final int dimensions) { 59 super(Const.T_ARRAY, "<dummy>"); 60 if ((dimensions < 1) || (dimensions > Const.MAX_BYTE)) { 61 throw new ClassGenException("Invalid number of dimensions: " + dimensions); 62 } 63 switch (type.getType()) { 64 case Const.T_ARRAY: 65 final ArrayType array = (ArrayType) type; 66 this.dimensions = dimensions + array.dimensions; 67 basic_type = array.basic_type; 68 break; 69 case Const.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 final StringBuilder buf = new StringBuilder(); 77 for (int i = 0; i < this.dimensions; i++) { 78 buf.append('['); 79 } 80 buf.append(basic_type.getSignature()); 81 super.setSignature(buf.toString()); 82 } 83 84 /** 85 * @return basic type of array, i.e., for int[][][] the basic type is int 86 */ 87 public Type getBasicType() { 88 return basic_type; 89 } 90 91 /** 92 * @return element type of array, i.e., for int[][][] the element type is 93 * int[][] 94 */ 95 public Type getElementType() { 96 if (dimensions == 1) { 97 return basic_type; 98 } 99 return new ArrayType(basic_type, dimensions - 1); 100 } 101 102 /** 103 * @return number of dimensions of array 104 */ 105 public int getDimensions() { 106 return dimensions; 107 } 108 109 /** 110 * @return a hash code value for the object. 111 */ 112 @Override 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 @Override 121 public boolean equals(final Object _type) { 122 if (_type instanceof ArrayType) { 123 final ArrayType array = (ArrayType) _type; 124 return (array.dimensions == dimensions) && array.basic_type.equals(basic_type); 125 } 126 return false; 127 } 128} 129