001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import org.apache.bcel.Const;
022
023/**
024 * Denotes basic type such as int.
025 */
026public final class BasicType extends Type {
027
028    /**
029     * Gets the BasicType for the given type constant.
030     *
031     * @param type the type constant.
032     * @return the BasicType.
033     * @since 6.0
034     */
035    // @since 6.0 no longer final
036    public static BasicType getType(final byte type) {
037        switch (type) {
038        case Const.T_VOID:
039            return VOID;
040        case Const.T_BOOLEAN:
041            return BOOLEAN;
042        case Const.T_BYTE:
043            return BYTE;
044        case Const.T_SHORT:
045            return SHORT;
046        case Const.T_CHAR:
047            return CHAR;
048        case Const.T_INT:
049            return INT;
050        case Const.T_LONG:
051            return LONG;
052        case Const.T_DOUBLE:
053            return DOUBLE;
054        case Const.T_FLOAT:
055            return FLOAT;
056        default:
057            throw new ClassGenException("Invalid type: " + type);
058        }
059    }
060
061    /**
062     * Constructor for basic types such as int, long, 'void'
063     *
064     * @param type one of T_INT, T_BOOLEAN, ..., T_VOID.
065     * @see Const
066     */
067    BasicType(final byte type) {
068        super(type, Const.getShortTypeName(type));
069        if (type < Const.T_BOOLEAN || type > Const.T_VOID) {
070            throw new ClassGenException("Invalid type: " + type);
071        }
072    }
073
074    /**
075     * @return true if both type objects refer to the same type.
076     */
077    @Override
078    public boolean equals(final Object type) {
079        return type instanceof BasicType && ((BasicType) type).getType() == this.getType();
080    }
081
082    /**
083     * @return a hash code value for the object.
084     */
085    @Override
086    public int hashCode() {
087        return super.getType();
088    }
089}