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.verifier.structurals;
020
021/**
022 * This class represents a JVM execution frame; that means, a local variable array and an operand stack.
023 */
024
025public class Frame {
026
027    /**
028     * For instance initialization methods, it is important to remember which instance it is that is not initialized yet. It
029     * will be initialized invoking another constructor later. NULL means the instance already *is* initialized.
030     *
031     * @deprecated Use the getter/setter to access the field as it may be made private in a later release
032     */
033    @Deprecated
034    protected static UninitializedObjectType _this;
035
036    /**
037     * @return the _this
038     * @since 6.0
039     */
040    public static UninitializedObjectType getThis() {
041        return _this;
042    }
043
044    /**
045     * @param _this the _this to set
046     * @since 6.0
047     */
048    public static void setThis(final UninitializedObjectType _this) {
049        Frame._this = _this;
050    }
051
052    /**
053     */
054    private final LocalVariables locals;
055
056    /**
057     */
058    private final OperandStack stack;
059
060    /**
061     */
062    public Frame(final int maxLocals, final int maxStack) {
063        locals = new LocalVariables(maxLocals);
064        stack = new OperandStack(maxStack);
065    }
066
067    /**
068     */
069    public Frame(final LocalVariables locals, final OperandStack stack) {
070        this.locals = locals;
071        this.stack = stack;
072    }
073
074    /**
075     */
076    @Override
077    protected Object clone() {
078        return new Frame(locals.getClone(), stack.getClone());
079    }
080
081    /**
082     */
083    @Override
084    public boolean equals(final Object o) {
085        if (!(o instanceof Frame)) {
086            return false; // implies "null" is non-equal.
087        }
088        final Frame f = (Frame) o;
089        return this.stack.equals(f.stack) && this.locals.equals(f.locals);
090    }
091
092    /**
093     */
094    public Frame getClone() {
095        return (Frame) clone();
096    }
097
098    /**
099     */
100    public LocalVariables getLocals() {
101        return locals;
102    }
103
104    /**
105     */
106    public OperandStack getStack() {
107        return stack;
108    }
109
110    /**
111     * @return a hash code value for the object.
112     */
113    @Override
114    public int hashCode() {
115        return stack.hashCode() ^ locals.hashCode();
116    }
117
118    /**
119     * Returns a String representation of the Frame instance.
120     */
121    @Override
122    public String toString() {
123        final StringBuilder s = new StringBuilder("Local Variables:\n");
124        s.append(locals);
125        s.append("OperandStack:\n");
126        s.append(stack);
127        return s.toString();
128    }
129}