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