View Javadoc
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  
19  import org.apache.bcel.Const;
20  
21  /**
22   * Denotes basic type such as int.
23   */
24  public final class BasicType extends Type {
25  
26      // @since 6.0 no longer final
27      public static BasicType getType(final byte type) {
28          switch (type) {
29          case Const.T_VOID:
30              return VOID;
31          case Const.T_BOOLEAN:
32              return BOOLEAN;
33          case Const.T_BYTE:
34              return BYTE;
35          case Const.T_SHORT:
36              return SHORT;
37          case Const.T_CHAR:
38              return CHAR;
39          case Const.T_INT:
40              return INT;
41          case Const.T_LONG:
42              return LONG;
43          case Const.T_DOUBLE:
44              return DOUBLE;
45          case Const.T_FLOAT:
46              return FLOAT;
47          default:
48              throw new ClassGenException("Invalid type: " + type);
49          }
50      }
51  
52      /**
53       * Constructor for basic types such as int, long, 'void'
54       *
55       * @param type one of T_INT, T_BOOLEAN, ..., T_VOID
56       * @see Const
57       */
58      BasicType(final byte type) {
59          super(type, Const.getShortTypeName(type));
60          if (type < Const.T_BOOLEAN || type > Const.T_VOID) {
61              throw new ClassGenException("Invalid type: " + type);
62          }
63      }
64  
65      /**
66       * @return true if both type objects refer to the same type
67       */
68      @Override
69      public boolean equals(final Object type) {
70          return type instanceof BasicType && ((BasicType) type).getType() == this.getType();
71      }
72  
73      /**
74       * @return a hash code value for the object.
75       */
76      @Override
77      public int hashCode() {
78          return super.getType();
79      }
80  }