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   * Returnaddress, the type JSR or JSR_W instructions push upon the stack.
25   *
26   * see vmspec2 �3.3.3
27   */
28  public class ReturnaddressType extends Type {
29  
30      public static final ReturnaddressType NO_TARGET = new ReturnaddressType();
31      private InstructionHandle returnTarget;
32  
33      /**
34       * A Returnaddress [that doesn't know where to return to].
35       */
36      private ReturnaddressType() {
37          super(Const.T_ADDRESS, "<return address>");
38      }
39  
40      /**
41       * Creates a ReturnaddressType object with a target.
42       */
43      public ReturnaddressType(final InstructionHandle returnTarget) {
44          super(Const.T_ADDRESS, "<return address targeting " + returnTarget + ">");
45          this.returnTarget = returnTarget;
46      }
47  
48      /**
49       * Returns if the two Returnaddresses refer to the same target.
50       */
51      @Override
52      public boolean equals(final Object rat) {
53          if (!(rat instanceof ReturnaddressType)) {
54              return false;
55          }
56          final ReturnaddressType that = (ReturnaddressType) rat;
57          if (this.returnTarget == null || that.returnTarget == null) {
58              return that.returnTarget == this.returnTarget;
59          }
60          return that.returnTarget.equals(this.returnTarget);
61      }
62  
63      /**
64       * @return the target of this ReturnaddressType.
65       */
66      public InstructionHandle getTarget() {
67          return returnTarget;
68      }
69  
70      /**
71       * @return a hash code value for the object.
72       */
73      @Override
74      public int hashCode() {
75          if (returnTarget == null) {
76              return 0;
77          }
78          return returnTarget.hashCode();
79      }
80  }