BasicType.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. import org.apache.bcel.Const;

  19. /**
  20.  * Denotes basic type such as int.
  21.  */
  22. public final class BasicType extends Type {

  23.     // @since 6.0 no longer final
  24.     public static BasicType getType(final byte type) {
  25.         switch (type) {
  26.         case Const.T_VOID:
  27.             return VOID;
  28.         case Const.T_BOOLEAN:
  29.             return BOOLEAN;
  30.         case Const.T_BYTE:
  31.             return BYTE;
  32.         case Const.T_SHORT:
  33.             return SHORT;
  34.         case Const.T_CHAR:
  35.             return CHAR;
  36.         case Const.T_INT:
  37.             return INT;
  38.         case Const.T_LONG:
  39.             return LONG;
  40.         case Const.T_DOUBLE:
  41.             return DOUBLE;
  42.         case Const.T_FLOAT:
  43.             return FLOAT;
  44.         default:
  45.             throw new ClassGenException("Invalid type: " + type);
  46.         }
  47.     }

  48.     /**
  49.      * Constructor for basic types such as int, long, 'void'
  50.      *
  51.      * @param type one of T_INT, T_BOOLEAN, ..., T_VOID
  52.      * @see Const
  53.      */
  54.     BasicType(final byte type) {
  55.         super(type, Const.getShortTypeName(type));
  56.         if (type < Const.T_BOOLEAN || type > Const.T_VOID) {
  57.             throw new ClassGenException("Invalid type: " + type);
  58.         }
  59.     }

  60.     /**
  61.      * @return true if both type objects refer to the same type
  62.      */
  63.     @Override
  64.     public boolean equals(final Object type) {
  65.         return type instanceof BasicType && ((BasicType) type).getType() == this.getType();
  66.     }

  67.     /**
  68.      * @return a hash code value for the object.
  69.      */
  70.     @Override
  71.     public int hashCode() {
  72.         return super.getType();
  73.     }
  74. }