    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 basic type such as int.
    024 *
    025 */
    026public final class BasicType extends Type {
    027
    028    // @since 6.0 no longer final
    029    public static BasicType getType(final byte type) {
    030        switch (type) {
    031        case Const.T_VOID:
    032            return VOID;
    033        case Const.T_BOOLEAN:
    034            return BOOLEAN;
    035        case Const.T_BYTE:
    036            return BYTE;
    037        case Const.T_SHORT:
    038            return SHORT;
    039        case Const.T_CHAR:
    040            return CHAR;
    041        case Const.T_INT:
    042            return INT;
    043        case Const.T_LONG:
    044            return LONG;
    045        case Const.T_DOUBLE:
    046            return DOUBLE;
    047        case Const.T_FLOAT:
    048            return FLOAT;
    049        default:
    050            throw new ClassGenException("Invalid type: " + type);
    051        }
    052    }
    053
    054    /**
    055     * Constructor for basic types such as int, long, `void'
    056     *
    057     * @param type one of T_INT, T_BOOLEAN, ..., T_VOID
    058     * @see Const
    059     */
    060    BasicType(final byte type) {
    061        super(type, Const.getShortTypeName(type));
    062        if (type < Const.T_BOOLEAN || type > Const.T_VOID) {
    063            throw new ClassGenException("Invalid type: " + type);
    064        }
    065    }
    066
    067    /**
    068     * @return true if both type objects refer to the same type
    069     */
    070    @Override
    071    public boolean equals(final Object type) {
    072        return type instanceof BasicType && ((BasicType) type).getType() == this.getType();
    073    }
    074
    075    /**
    076     * @return a hash code value for the object.
    077     */
    078    @Override
    079    public int hashCode() {
    080        return super.getType();
    081    }
    082}