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