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    /**
034     * A Returnaddress [that doesn't know where to return to].
035     */
036    private ReturnaddressType() {
037        super(Const.T_ADDRESS, "<return address>");
038    }
039
040
041    /**
042     * Creates a ReturnaddressType object with a target.
043     */
044    public ReturnaddressType(final InstructionHandle returnTarget) {
045        super(Const.T_ADDRESS, "<return address targeting " + returnTarget + ">");
046        this.returnTarget = returnTarget;
047    }
048
049
050    /** @return a hash code value for the object.
051     */
052    @Override
053    public int hashCode() {
054        if (returnTarget == null) {
055            return 0;
056        }
057        return returnTarget.hashCode();
058    }
059
060
061    /**
062     * Returns if the two Returnaddresses refer to the same target.
063     */
064    @Override
065    public boolean equals( final Object rat ) {
066        if (!(rat instanceof ReturnaddressType)) {
067            return false;
068        }
069        final ReturnaddressType that = (ReturnaddressType) rat;
070        if (this.returnTarget == null || that.returnTarget == null) {
071            return that.returnTarget == this.returnTarget;
072        }
073        return that.returnTarget.equals(this.returnTarget);
074    }
075
076
077    /**
078     * @return the target of this ReturnaddressType
079     */
080    public InstructionHandle getTarget() {
081        return returnTarget;
082    }
083}