View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.verifier.structurals;
20  
21  /**
22   * This class represents a JVM execution frame; that means, a local variable array and an operand stack.
23   */
24  
25  public class Frame {
26  
27      /**
28       * For instance initialization methods, it is important to remember which instance it is that is not initialized yet. It
29       * will be initialized invoking another constructor later. NULL means the instance already *is* initialized.
30       *
31       * @deprecated Use the getter/setter to access the field as it may be made private in a later release
32       */
33      @Deprecated
34      protected static UninitializedObjectType _this;
35  
36      /**
37       * Gets the static _this reference.
38       *
39       * @return the _this.
40       * @since 6.0
41       */
42      public static UninitializedObjectType getThis() {
43          return _this;
44      }
45  
46      /**
47       * Sets the static _this reference.
48       *
49       * @param _this the _this to set.
50       * @since 6.0
51       */
52      public static void setThis(final UninitializedObjectType _this) {
53          Frame._this = _this;
54      }
55  
56      /**
57       * The local variables.
58       */
59      private final LocalVariables locals;
60  
61      /**
62       * The operand stack.
63       */
64      private final OperandStack stack;
65  
66      /**
67       * Constructs a Frame.
68       *
69       * @param maxLocals the maximum number of local variables.
70       * @param maxStack the maximum stack size.
71       */
72      public Frame(final int maxLocals, final int maxStack) {
73          locals = new LocalVariables(maxLocals);
74          stack = new OperandStack(maxStack);
75      }
76  
77      /**
78       * Constructs a Frame.
79       *
80       * @param locals the local variables.
81       * @param stack the operand stack.
82       */
83      public Frame(final LocalVariables locals, final OperandStack stack) {
84          this.locals = locals;
85          this.stack = stack;
86      }
87  
88      /**
89       * Creates a clone of this frame.
90       *
91       * @return a clone of this frame.
92       */
93      @Override
94      protected Object clone() {
95          return new Frame(locals.getClone(), stack.getClone());
96      }
97  
98      /**
99       * Checks if this frame equals another object.
100      *
101      * @param o the object to compare.
102      * @return true if equal, false otherwise.
103      */
104     @Override
105     public boolean equals(final Object o) {
106         if (!(o instanceof Frame)) {
107             return false; // implies "null" is non-equal.
108         }
109         final Frame f = (Frame) o;
110         return this.stack.equals(f.stack) && this.locals.equals(f.locals);
111     }
112 
113     /**
114      * Gets a clone of this frame.
115      *
116      * @return a clone of this frame.
117      */
118     public Frame getClone() {
119         return (Frame) clone();
120     }
121 
122     /**
123      * Gets the local variables.
124      *
125      * @return the local variables.
126      */
127     public LocalVariables getLocals() {
128         return locals;
129     }
130 
131     /**
132      * Gets the operand stack.
133      *
134      * @return the operand stack.
135      */
136     public OperandStack getStack() {
137         return stack;
138     }
139 
140     /**
141      * @return a hash code value for the object.
142      */
143     @Override
144     public int hashCode() {
145         return stack.hashCode() ^ locals.hashCode();
146     }
147 
148     /**
149      * Returns a String representation of the Frame instance.
150      */
151     @Override
152     public String toString() {
153         final StringBuilder s = new StringBuilder("Local Variables:\n");
154         s.append(locals);
155         s.append("OperandStack:\n");
156         s.append(stack);
157         return s.toString();
158     }
159 }