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    // @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}