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