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