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 */
017package org.apache.bcel.generic;
018
019import org.apache.bcel.Const;
020
021/**
022 * Returnaddress, the type JSR or JSR_W instructions push upon the stack.
023 *
024 * see vmspec2 �3.3.3
025 */
026public class ReturnaddressType extends Type {
027
028    public static final ReturnaddressType NO_TARGET = new ReturnaddressType();
029    private InstructionHandle returnTarget;
030
031    /**
032     * A Returnaddress [that doesn't know where to return to].
033     */
034    private ReturnaddressType() {
035        super(Const.T_ADDRESS, "<return address>");
036    }
037
038    /**
039     * Creates a ReturnaddressType object with a target.
040     */
041    public ReturnaddressType(final InstructionHandle returnTarget) {
042        super(Const.T_ADDRESS, "<return address targeting " + returnTarget + ">");
043        this.returnTarget = returnTarget;
044    }
045
046    /**
047     * Returns if the two Returnaddresses refer to the same target.
048     */
049    @Override
050    public boolean equals(final Object rat) {
051        if (!(rat instanceof ReturnaddressType)) {
052            return false;
053        }
054        final ReturnaddressType that = (ReturnaddressType) rat;
055        if (this.returnTarget == null || that.returnTarget == null) {
056            return that.returnTarget == this.returnTarget;
057        }
058        return that.returnTarget.equals(this.returnTarget);
059    }
060
061    /**
062     * @return the target of this ReturnaddressType
063     */
064    public InstructionHandle getTarget() {
065        return returnTarget;
066    }
067
068    /**
069     * @return a hash code value for the object.
070     */
071    @Override
072    public int hashCode() {
073        if (returnTarget == null) {
074            return 0;
075        }
076        return returnTarget.hashCode();
077    }
078}