001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.generic; 019 020import org.apache.bcel.Const; 021 022/** 023 * Denotes array type, such as int[][] 024 * 025 */ 026public final class ArrayType extends ReferenceType { 027 028 private int dimensions; 029 private Type basic_type; 030 031 032 /** 033 * Convenience constructor for array type, e.g. int[] 034 * 035 * @param type array type, e.g. T_INT 036 */ 037 public ArrayType(final byte type, final int dimensions) { 038 this(BasicType.getType(type), dimensions); 039 } 040 041 042 /** 043 * Convenience constructor for reference array type, e.g. Object[] 044 * 045 * @param class_name complete name of class (java.lang.String, e.g.) 046 */ 047 public ArrayType(final String class_name, final int dimensions) { 048 this(ObjectType.getInstance(class_name), dimensions); 049 } 050 051 052 /** 053 * Constructor for array of given type 054 * 055 * @param type type of array (may be an array itself) 056 */ 057 public ArrayType(final Type type, final int dimensions) { 058 super(Const.T_ARRAY, "<dummy>"); 059 if ((dimensions < 1) || (dimensions > Const.MAX_BYTE)) { 060 throw new ClassGenException("Invalid number of dimensions: " + dimensions); 061 } 062 switch (type.getType()) { 063 case Const.T_ARRAY: 064 final ArrayType array = (ArrayType) type; 065 this.dimensions = dimensions + array.dimensions; 066 basic_type = array.basic_type; 067 break; 068 case Const.T_VOID: 069 throw new ClassGenException("Invalid type: void[]"); 070 default: // Basic type or reference 071 this.dimensions = dimensions; 072 basic_type = type; 073 break; 074 } 075 final StringBuilder buf = new StringBuilder(); 076 for (int i = 0; i < this.dimensions; i++) { 077 buf.append('['); 078 } 079 buf.append(basic_type.getSignature()); 080 super.setSignature(buf.toString()); 081 } 082 083 084 /** 085 * @return basic type of array, i.e., for int[][][] the basic type is int 086 */ 087 public Type getBasicType() { 088 return basic_type; 089 } 090 091 092 /** 093 * @return element type of array, i.e., for int[][][] the element type is int[][] 094 */ 095 public Type getElementType() { 096 if (dimensions == 1) { 097 return basic_type; 098 } 099 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}