View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import org.apache.bcel.Const;
22  
23  /**
24   * Denotes basic type such as int.
25   */
26  public final class BasicType extends Type {
27  
28      /**
29       * Gets the BasicType for the given type constant.
30       *
31       * @param type the type constant.
32       * @return the BasicType.
33       * @since 6.0
34       */
35      // @since 6.0 no longer final
36      public static BasicType getType(final byte type) {
37          switch (type) {
38          case Const.T_VOID:
39              return VOID;
40          case Const.T_BOOLEAN:
41              return BOOLEAN;
42          case Const.T_BYTE:
43              return BYTE;
44          case Const.T_SHORT:
45              return SHORT;
46          case Const.T_CHAR:
47              return CHAR;
48          case Const.T_INT:
49              return INT;
50          case Const.T_LONG:
51              return LONG;
52          case Const.T_DOUBLE:
53              return DOUBLE;
54          case Const.T_FLOAT:
55              return FLOAT;
56          default:
57              throw new ClassGenException("Invalid type: " + type);
58          }
59      }
60  
61      /**
62       * Constructor for basic types such as int, long, 'void'
63       *
64       * @param type one of T_INT, T_BOOLEAN, ..., T_VOID.
65       * @see Const
66       */
67      BasicType(final byte type) {
68          super(type, Const.getShortTypeName(type));
69          if (type < Const.T_BOOLEAN || type > Const.T_VOID) {
70              throw new ClassGenException("Invalid type: " + type);
71          }
72      }
73  
74      /**
75       * @return true if both type objects refer to the same type.
76       */
77      @Override
78      public boolean equals(final Object type) {
79          return type instanceof BasicType && ((BasicType) type).getType() == this.getType();
80      }
81  
82      /**
83       * @return a hash code value for the object.
84       */
85      @Override
86      public int hashCode() {
87          return super.getType();
88      }
89  }