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.verifier.exc.AssertionViolatedException;
21
22 import java.util.Arrays;
23
24
25
26
27
28
29
30
31
32
33 public class LocalVariables{
34
35 private Type[] locals;
36
37
38
39
40 public LocalVariables(int maxLocals){
41 locals = new Type[maxLocals];
42 Arrays.fill(locals,Type.UNKNOWN);
43 }
44
45
46
47
48
49
50 protected Object clone(){
51 LocalVariables lvs = new LocalVariables(locals.length);
52 System.arraycopy(this.locals, 0, lvs.locals, 0, locals.length);
53 return lvs;
54 }
55
56
57
58
59 public Type get(int i){
60 return locals[i];
61 }
62
63
64
65
66
67 public LocalVariables getClone(){
68 return (LocalVariables) this.clone();
69 }
70
71
72
73
74
75 public int maxLocals(){
76 return locals.length;
77 }
78
79
80
81
82 public void set(int i, Type type){
83 if (type == Type.BYTE || type == Type.SHORT || type == Type.BOOLEAN || type == Type.CHAR){
84 throw new AssertionViolatedException("LocalVariables do not know about '"+type+"'. Use Type.INT instead.");
85 }
86 locals[i] = type;
87 }
88
89
90
91
92 public boolean equals(Object o){
93 if (!(o instanceof LocalVariables)) return false;
94 LocalVariables lv = (LocalVariables) o;
95 return Arrays.equals(this.locals, lv.locals);
96 }
97
98
99
100
101
102 public void merge(LocalVariables that){
103
104 if (this.locals.length != that.locals.length){
105 throw new AssertionViolatedException("Merging LocalVariables of different size?!? From different methods or what?!?");
106 }
107
108 for (int i=0; i<locals.length; i++) {
109 this.locals[i] = Frame.merge(this.locals[i], that.locals[i], false);
110 }
111 }
112
113
114
115
116
117 public String toString(){
118 String s = "";
119 for (int i=0; i<locals.length; i++){
120 s += Integer.toString(i)+": "+locals[i]+"\n";
121 }
122 return s;
123 }
124
125
126
127
128
129 public void initializeObject(UninitializedObjectType u){
130 for (int i=0; i<locals.length; i++){
131 if (locals[i] == u){
132 locals[i] = u.getInitialized();
133 }
134 }
135 }
136 }