1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.javaflow.bytecode.transformation.bcel.analyser;
18
19 import org.apache.bcel.generic.Type;
20 import org.apache.bcel.generic.ReferenceType;
21 import org.apache.bcel.generic.ReturnaddressType;
22 import org.apache.bcel.verifier.exc.StructuralCodeConstraintException;
23 import org.apache.bcel.verifier.exc.AssertionViolatedException;
24
25
26
27
28
29
30
31
32
33
34
35 public class Frame{
36
37
38
39
40
41
42
43 public static UninitializedObjectType _this;
44
45
46
47
48 private LocalVariables locals;
49
50
51
52
53 private OperandStack stack;
54
55
56
57
58 public Frame(int maxLocals, int maxStack){
59 locals = new LocalVariables(maxLocals);
60 stack = new OperandStack(maxStack);
61 }
62
63
64
65
66 public Frame(LocalVariables locals, OperandStack stack){
67 this.locals = locals;
68 this.stack = stack;
69 }
70
71
72
73
74 protected Object clone(){
75 Frame f = new Frame(locals.getClone(), stack.getClone());
76 return f;
77 }
78
79
80
81
82 public Frame getClone(){
83 return (Frame) clone();
84 }
85
86
87
88
89 public LocalVariables getLocals(){
90 return locals;
91 }
92
93
94
95
96 public OperandStack getStack(){
97 return stack;
98 }
99
100
101
102
103 public boolean equals(Object o){
104 if (!(o instanceof Frame)) return false;
105 Frame f = (Frame) o;
106 return this.stack.equals(f.stack) && this.locals.equals(f.locals);
107 }
108
109
110
111
112 public String toString(){
113 String s="Local Variables:\n";
114 s += locals;
115 s += "OperandStack:\n";
116 s += stack;
117 return s;
118 }
119
120
121
122
123
124
125
126
127
128
129 try {
130
131
132
133 if ((!(lhs instanceof UninitializedObjectType)) && (rhs instanceof UninitializedObjectType)) {
134 throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected.");
135 }
136
137 if ((!(lhs.equals(rhs))) && (lhs instanceof UninitializedObjectType) && (rhs instanceof UninitializedObjectType)) {
138 throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected.");
139 }
140
141 if (lhs instanceof UninitializedObjectType) {
142 if (! (rhs instanceof UninitializedObjectType)) {
143 lhs = ((UninitializedObjectType) lhs).getInitialized();
144 }
145 }
146 if ((lhs instanceof ReferenceType) && (rhs instanceof ReferenceType)) {
147 if(lhs.equals(rhs)) {
148 return lhs;
149 }
150
151 Type sup = ((ReferenceType) lhs).getFirstCommonSuperclass((ReferenceType) rhs);
152
153 if (sup != null) {
154 return sup;
155 } else {
156
157 throw new AssertionViolatedException("Could not load all the super classes of '" + lhs + "' and '" + rhs + "'.");
158 }
159 }
160
161 if ((lhs instanceof ReturnaddressType) && (rhs instanceof ReturnaddressType)) {
162
163 return lhs;
164 }
165
166 if (!lhs.equals(rhs)) {
167 if(errorIfFailed) {
168 throw new StructuralCodeConstraintException("Cannot merge different types:"+lhs+" and "+rhs);
169 } else {
170 return Type.UNKNOWN;
171 }
172 }
173
174 return lhs;
175 } catch (ClassNotFoundException e) {
176
177 throw new AssertionViolatedException("Missing class: " + e.toString());
178 }
179 }
180 }