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