Debian Codegraph @

src/main/java/org/jboss/classfilewriter/attributes/StackMapTableAttribute.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.attributes;
19
20 import org.jboss.classfilewriter.ClassMethod;
21 import org.jboss.classfilewriter.code.CodeAttribute;
22 import org.jboss.classfilewriter.code.StackEntry;
23 import org.jboss.classfilewriter.code.StackEntryType;
24 import org.jboss.classfilewriter.code.StackFrame;
25 import org.jboss.classfilewriter.code.StackFrameType;
26 import org.jboss.classfilewriter.constpool.ConstPool;
27 import org.jboss.classfilewriter.util.ByteArrayDataOutputStream;
28 import org.jboss.classfilewriter.util.LazySize;
29
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map.Entry;
35
36 /**
37 * A JDK 6 StackMap sttribute.
38 *
39 *TODO: this will currently fall over if the code length, max locals or max stack is above 65535
40 *
41 * @author Stuart Douglas
42 *
43 */
44 public class StackMapTableAttribute extends Attribute {
45
46 private static final int FULL_FRAME = 255;
47 private static final int SAME_FRAME_EXTENDED = 251;
48
49
50 public static final String NAME = "StackMapTable";
51
52 private final ClassMethod method;
53
54 public StackMapTableAttribute(ClassMethod classMethod, ConstPool constPool) {
55 super(NAME, constPool);
56 method = classMethod;
57 }
58
59 @Override
60 public void writeData(ByteArrayDataOutputStream stream) throws IOException {
61 // as we don't know the size yet we write everything to a byte stream first
62 // TODO: make this better
63 final CodeAttribute ca = method.getCodeAttribute();
64 // now we need to write the stack frames.
65 // for now we are going to write all frames as full frames
66 // TODO: optimise the frame creation
67
68 // write to dstream
69 LazySize size = stream.writeSize();
70 stream.writeShort(ca.getStackFrames().size());
71 int lastPos = -1;
72 for (Entry<Integer, StackFrame> entry : method.getCodeAttribute().getStackFrames().entrySet()) {
73 int offset = entry.getKey() - lastPos - 1;
74 lastPos = entry.getKey();
75 StackFrame frame = entry.getValue();
76 if (frame.getType() == StackFrameType.SAME_FRAME || frame.getType() == StackFrameType.SAME_FRAME_EXTENDED) {
77 writeSameFrame(stream, offset, lastPos, frame);
78 } else if (frame.getType() == StackFrameType.SAME_LOCALS_1_STACK && offset < (127 - 64)) {
79 writeSameLocals1Stack(stream, offset, lastPos, frame);
80 } else {
81 writeFullFrame(stream, offset, lastPos, entry.getValue());
82 }
83 }
84 size.markEnd();
85 }
86
87 private void writeSameLocals1Stack(DataOutputStream dstream, int offset, int lastPos, StackFrame frame) throws IOException {
88 dstream.writeByte(offset + 64);
89 frame.getStackState().getContents().get(0).write(dstream);
90 }
91
92 private void writeSameFrame(DataOutputStream dstream, int offset, int lastPos, StackFrame frame) throws IOException {
93 if (offset > 63) {
94 dstream.writeByte(SAME_FRAME_EXTENDED);
95 dstream.writeShort(offset);
96 } else {
97 dstream.writeByte(offset);
98 }
99 }
100
101 /**
102 * writes a full_frame to the stack map table
103 */
104 private void writeFullFrame(DataOutputStream dstream, int offset, int position, StackFrame value) throws IOException {
105 dstream.writeByte(FULL_FRAME);
106 dstream.writeShort(offset);
107 List<StackEntry> realLocalVars = new ArrayList<StackEntry>(value.getLocalVariableState().getContents().size());
108 for (StackEntry i : value.getLocalVariableState().getContents()) {
109 if (i.getType() != StackEntryType.TOP) {
110 realLocalVars.add(i);
111 }
112 }
113 dstream.writeShort(realLocalVars.size());
114 for (StackEntry i : realLocalVars) {
115 i.write(dstream);
116 }
117 // TODO: this is inefficient, the stack should store the number of TOP values in each frame
118 List<StackEntry> realStack = new ArrayList<StackEntry>(value.getStackState().getContents().size());
119 for (StackEntry i : value.getStackState().getContents()) {
120 if (i.getType() != StackEntryType.TOP) {
121 realStack.add(i);
122 }
123 }
124 dstream.writeShort(realStack.size());
125 for (StackEntry i : realStack) {
126 i.write(dstream);
127 }
128 }
129
130 }