001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import org.apache.bcel.Const;
022
023/**
024 * Returnaddress, the type JSR or JSR_W instructions push upon the stack.
025 *
026 * see vmspec2 �3.3.3
027 */
028public class ReturnaddressType extends Type {
029
030    /** Predefined constant for a returnaddress with no target. */
031    public static final ReturnaddressType NO_TARGET = new ReturnaddressType();
032    private InstructionHandle returnTarget;
033
034    /**
035     * A Returnaddress [that doesn't know where to return to].
036     */
037    private ReturnaddressType() {
038        super(Const.T_ADDRESS, "<return address>");
039    }
040
041    /**
042     * Creates a ReturnaddressType object with a target.
043     *
044     * @param returnTarget The target instruction.
045     */
046    public ReturnaddressType(final InstructionHandle returnTarget) {
047        super(Const.T_ADDRESS, "<return address targeting " + returnTarget + ">");
048        this.returnTarget = returnTarget;
049    }
050
051    /**
052     * Returns if the two Returnaddresses refer to the same target.
053     *
054     * @param rat The object to compare.
055     * @return true if equal.
056     */
057    @Override
058    public boolean equals(final Object rat) {
059        if (!(rat instanceof ReturnaddressType)) {
060            return false;
061        }
062        final ReturnaddressType that = (ReturnaddressType) rat;
063        if (this.returnTarget == null || that.returnTarget == null) {
064            return that.returnTarget == this.returnTarget;
065        }
066        return that.returnTarget.equals(this.returnTarget);
067    }
068
069    /**
070     * Gets the target of this ReturnaddressType.
071     *
072     * @return The target of this ReturnaddressType.
073     */
074    public InstructionHandle getTarget() {
075        return returnTarget;
076    }
077
078    /**
079     * @return A hash code value for the object.
080     */
081    @Override
082    public int hashCode() {
083        if (returnTarget == null) {
084            return 0;
085        }
086        return returnTarget.hashCode();
087    }
088}