    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 final int dimensions;
    029    private final Type basicType;
    030
    031    /**
    032     * Convenience constructor for array type, e.g. int[]
    033     *
    034     * @param type array type, e.g. T_INT
    035     */
    036    public ArrayType(final byte type, final int dimensions) {
    037        this(BasicType.getType(type), dimensions);
    038    }
    039
    040    /**
    041     * Convenience constructor for reference array type, e.g. Object[]
    042     *
    043     * @param className complete name of class (java.lang.String, e.g.)
    044     */
    045    public ArrayType(final String className, final int dimensions) {
    046        this(ObjectType.getInstance(className), dimensions);
    047    }
    048
    049    /**
    050     * Constructor for array of given type
    051     *
    052     * @param type type of array (may be an array itself)
    053     */
    054    public ArrayType(final Type type, final int dimensions) {
    055        super(Const.T_ARRAY, "<dummy>");
    056        if (dimensions < 1 || dimensions > Const.MAX_BYTE) {
    057            throw new ClassGenException("Invalid number of dimensions: " + dimensions);
    058        }
    059        switch (type.getType()) {
    060        case Const.T_ARRAY:
    061            final ArrayType array = (ArrayType) type;
    062            this.dimensions = dimensions + array.dimensions;
    063            basicType = array.basicType;
    064            break;
    065        case Const.T_VOID:
    066            throw new ClassGenException("Invalid type: void[]");
    067        default: // Basic type or reference
    068            this.dimensions = dimensions;
    069            basicType = type;
    070            break;
    071        }
    072        final StringBuilder buf = new StringBuilder();
    073        for (int i = 0; i < this.dimensions; i++) {
    074            buf.append('[');
    075        }
    076        buf.append(basicType.getSignature());
    077        super.setSignature(buf.toString());
    078    }
    079
    080    /**
    081     * @return true if both type objects refer to the same array type.
    082     */
    083    @Override
    084    public boolean equals(final Object type) {
    085        if (type instanceof ArrayType) {
    086            final ArrayType array = (ArrayType) type;
    087            return array.dimensions == dimensions && array.basicType.equals(basicType);
    088        }
    089        return false;
    090    }
    091
    092    /**
    093     * @return basic type of array, i.e., for int[][][] the basic type is int
    094     */
    095    public Type getBasicType() {
    096        return basicType;
    097    }
    098
    099    /**
    100     * @return number of dimensions of array
    101     */
    102    public int getDimensions() {
    103        return dimensions;
    104    }
    105
    106    /**
    107     * @return element type of array, i.e., for int[][][] the element type is int[][]
    108     */
    109    public Type getElementType() {
    110        if (dimensions == 1) {
    111            return basicType;
    112        }
    113        return new ArrayType(basicType, dimensions - 1);
    114    }
    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}