Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/LocalVariableState.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.jboss.classfilewriter.code;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.jboss.classfilewriter.ClassMethod;
25 import org.jboss.classfilewriter.InvalidBytecodeException;
26 import org.jboss.classfilewriter.constpool.ConstPool;
27
28 /**
29 * In immutable local variable state
30 *
31 * @author Stuart Douglas
32 *
33 */
34 public class LocalVariableState {
35
36 /**
37 * The contents, null is used to represent the additional spot taken up by a wide variable.
38 * <p>
39 * This list may be shared between frames, so it must never be modified
40 * <p>
41 * The very first element represents the first local variable (this for non static methods)
42 */
43 private final List<StackEntry> contents;
44
45 private final ConstPool constPool;
46
47 /**
48 * construct the initial local variable state for a method
49 */
50 public LocalVariableState(ClassMethod method) {
51 this.constPool = method.getClassFile().getConstPool();
52 contents = new ArrayList<StackEntry>();
53 if (!method.isStatic()) {
54 if (method.isConstructor()) {
55 contents.add(new StackEntry(StackEntryType.UNINITIALIZED_THIS, method.getClassFile().getDescriptor()));
56 } else {
57 contents.add(StackEntry.of(method.getClassFile().getDescriptor(), method.getClassFile().getConstPool()));
58 }
59 }
60 for (String param : method.getParameters()) {
61 StackEntry entry = StackEntry.of(param, method.getClassFile().getConstPool());
62 contents.add(entry);
63 if (entry.isWide()) {
64 contents.add(new StackEntry(StackEntryType.TOP, param));
65 }
66 }
67 }
68
69 public LocalVariableState(final ConstPool pool, final String ... entries) {
70 this.constPool = pool;
71 contents = new ArrayList<StackEntry>();
72 for(String c : entries) {
73 final StackEntry entry = StackEntry.of(c, constPool);
74 contents.add(entry);
75 }
76 }
77
78 private LocalVariableState(final List<StackEntry> contents, ConstPool constPool) {
79 this.contents = contents;
80 this.constPool = constPool;
81 }
82
83 public List<StackEntry> getContents() {
84 return Collections.unmodifiableList(contents);
85 }
86
87 public StackEntry get(int index) {
88 return contents.get(index);
89 }
90
91 public LocalVariableState storeWide(int index, StackEntry entry) {
92 ArrayList<StackEntry> newContents = new ArrayList<StackEntry>(contents.size());
93 for (int i = 0; i <= index || i < contents.size(); ++i) {
94 if (index == i) {
95 newContents.add(entry);
96 newContents.add(new StackEntry(StackEntryType.TOP, entry.getDescriptor()));
97 ++i;
98 } else if (i >= contents.size()) {
99 // write a null in unitialised slots
100 // not sure if this is correct
101 newContents.add(new StackEntry(StackEntryType.NULL, null));
102 } else {
103 newContents.add(contents.get(i));
104 }
105 }
106 return new LocalVariableState(newContents, constPool);
107 }
108
109 public LocalVariableState store(int index, StackEntry entry) {
110 ArrayList<StackEntry> newContents = new ArrayList<StackEntry>(contents.size());
111 for (int i = 0; i <= index || i < contents.size(); ++i) {
112 if (index == i) {
113 newContents.add(entry);
114 } else if (i >= contents.size()) {
115 // write a null in unitialised slots
116 // not sure if this is correct
117 newContents.add(new StackEntry(StackEntryType.NULL, null));
118 } else {
119 newContents.add(contents.get(i));
120 }
121 }
122 return new LocalVariableState(newContents, constPool);
123 }
124
125 public int size() {
126 return contents.size();
127 }
128
129 @Override
130 public String toString() {
131 return "Local Variables: " + contents.toString();
132 }
133
134 public LocalVariableState constructorCall(StackEntry entry) {
135 List<StackEntry> newContents = new ArrayList<StackEntry>(contents.size());
136 if (entry.getType() == StackEntryType.UNINITIALIZED_THIS) {
137 for (int i = 0; i < contents.size(); ++i) {
138 StackEntry stackEntry = contents.get(i);
139 if (stackEntry.getType() == StackEntryType.UNINITIALIZED_THIS) {
140 newContents.add(StackEntry.of(stackEntry.getDescriptor(), constPool));
141 } else {
142 newContents.add(stackEntry);
143 }
144 }
145 return new LocalVariableState(newContents, constPool);
146 } else if (entry.getType() == StackEntryType.UNITITIALIZED_OBJECT) {
147 for (int i = 0; i < contents.size(); ++i) {
148 StackEntry stackEntry = contents.get(i);
149 if (stackEntry.getType() == StackEntryType.UNITITIALIZED_OBJECT
150 && stackEntry.getNewInstructionLocation() == entry.getNewInstructionLocation()) {
151 newContents.add(StackEntry.of(stackEntry.getDescriptor(), constPool));
152 } else {
153 newContents.add(stackEntry);
154 }
155 }
156 return new LocalVariableState(newContents, constPool);
157 } else {
158 throw new InvalidBytecodeException("entry is not an unitialized object. " + toString());
159 }
160 }
161
162 public LocalVariableState updateMerged(int pos, StackEntry frame) {
163 List<StackEntry> newContents = new ArrayList<StackEntry>(contents);
164 newContents.remove(pos);
165 newContents.add(pos, frame);
166 return new LocalVariableState(newContents, constPool);
167 }
168 }