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