KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > asm > util > attrs > ASMStackMapAttribute


1 /**
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.tc.asm.util.attrs;
31
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import com.tc.asm.Attribute;
36 import com.tc.asm.ClassReader;
37 import com.tc.asm.Label;
38 import com.tc.asm.attrs.StackMapAttribute;
39 import com.tc.asm.attrs.StackMapFrame;
40 import com.tc.asm.attrs.StackMapType;
41
42 /**
43  * An {@link ASMifiable} {@link StackMapAttribute} sub class.
44  *
45  * @author Eugene Kuleshov
46  */

47 public class ASMStackMapAttribute extends StackMapAttribute implements
48         ASMifiable,
49         Traceable
50 {
51     /**
52      * Length of the attribute used for comparison
53      */

54     private int len;
55
56     public ASMStackMapAttribute() {
57         super();
58     }
59
60     public ASMStackMapAttribute(List JavaDoc frames, int len) {
61         super(frames);
62         this.len = len;
63     }
64
65     protected Attribute read(
66         ClassReader cr,
67         int off,
68         int len,
69         char[] buf,
70         int codeOff,
71         Label[] labels)
72     {
73         StackMapAttribute attr = (StackMapAttribute) super.read(cr,
74                 off,
75                 len,
76                 buf,
77                 codeOff,
78                 labels);
79
80         return new ASMStackMapAttribute(attr.getFrames(), len);
81     }
82
83     public void asmify(StringBuffer JavaDoc buf, String JavaDoc varName, Map JavaDoc labelNames) {
84         List JavaDoc frames = getFrames();
85         buf.append("{\n");
86         buf.append("StackMapAttribute ").append(varName).append("Attr");
87         buf.append(" = new StackMapAttribute();\n");
88         if (frames.size() > 0) {
89             for (int i = 0; i < frames.size(); i++) {
90                 asmify((StackMapFrame) frames.get(i), buf, varName + "frame"
91                         + i, labelNames);
92             }
93         }
94         buf.append(varName).append(".visitAttribute(").append(varName);
95         buf.append("Attr);\n}\n");
96     }
97
98     void asmify(
99         StackMapFrame f,
100         StringBuffer JavaDoc buf,
101         String JavaDoc varName,
102         Map JavaDoc labelNames)
103     {
104         declareLabel(buf, labelNames, f.label);
105         buf.append("{\n");
106
107         buf.append("StackMapFrame ")
108                 .append(varName)
109                 .append(" = new StackMapFrame();\n");
110
111         buf.append(varName)
112                 .append(".label = ")
113                 .append(labelNames.get(f.label))
114                 .append(";\n");
115
116         asmifyTypeInfo(buf, varName, labelNames, f.locals, "locals");
117         asmifyTypeInfo(buf, varName, labelNames, f.stack, "stack");
118
119         buf.append("cvAttr.frames.add(").append(varName).append(");\n");
120         buf.append("}\n");
121     }
122
123     void asmifyTypeInfo(
124         StringBuffer JavaDoc buf,
125         String JavaDoc varName,
126         Map JavaDoc labelNames,
127         List JavaDoc infos,
128         String JavaDoc field)
129     {
130         if (infos.size() > 0) {
131             buf.append("{\n");
132             for (int i = 0; i < infos.size(); i++) {
133                 StackMapType typeInfo = (StackMapType) infos.get(i);
134                 String JavaDoc localName = varName + "Info" + i;
135                 int type = typeInfo.getType();
136                 buf.append("StackMapType ")
137                         .append(localName)
138                         .append(" = StackMapType.getTypeInfo( StackMapType.ITEM_")
139                         .append(StackMapType.ITEM_NAMES[type])
140                         .append(");\n");
141
142                 switch (type) {
143                     case StackMapType.ITEM_Object: //
144
buf.append(localName)
145                                 .append(".setObject(\"")
146                                 .append(typeInfo.getObject())
147                                 .append("\");\n");
148                         break;
149
150                     case StackMapType.ITEM_Uninitialized: //
151
declareLabel(buf, labelNames, typeInfo.getLabel());
152                         buf.append(localName)
153                                 .append(".setLabel(")
154                                 .append(labelNames.get(typeInfo.getLabel()))
155                                 .append(");\n");
156                         break;
157                 }
158                 buf.append(varName)
159                         .append(".")
160                         .append(field)
161                         .append(".add(")
162                         .append(localName)
163                         .append(");\n");
164             }
165             buf.append("}\n");
166         }
167     }
168
169     static void declareLabel(StringBuffer JavaDoc buf, Map JavaDoc labelNames, Label l) {
170         String JavaDoc name = (String JavaDoc) labelNames.get(l);
171         if (name == null) {
172             name = "l" + labelNames.size();
173             labelNames.put(l, name);
174             buf.append("Label ").append(name).append(" = new Label();\n");
175         }
176     }
177
178     public void trace(StringBuffer JavaDoc buf, Map JavaDoc labelNames) {
179         List JavaDoc frames = getFrames();
180         buf.append("[\n");
181         for (int i = 0; i < frames.size(); i++) {
182             StackMapFrame f = (StackMapFrame) frames.get(i);
183
184             buf.append(" Frame:");
185             appendLabel(buf, labelNames, f.label);
186
187             buf.append(" locals[");
188             traceTypeInfo(buf, labelNames, f.locals);
189             buf.append("]");
190             buf.append(" stack[");
191             traceTypeInfo(buf, labelNames, f.stack);
192             buf.append("]\n");
193         }
194         buf.append(" ] length:").append(len).append("\n");
195     }
196
197     private void traceTypeInfo(StringBuffer JavaDoc buf, Map JavaDoc labelNames, List JavaDoc infos) {
198         String JavaDoc sep = "";
199         for (int i = 0; i < infos.size(); i++) {
200             StackMapType t = (StackMapType) infos.get(i);
201
202             buf.append(sep).append(StackMapType.ITEM_NAMES[t.getType()]);
203             sep = ", ";
204             if (t.getType() == StackMapType.ITEM_Object) {
205                 buf.append(":").append(t.getObject());
206             }
207             if (t.getType() == StackMapType.ITEM_Uninitialized) {
208                 buf.append(":");
209                 appendLabel(buf, labelNames, t.getLabel());
210             }
211         }
212     }
213
214     protected void appendLabel(StringBuffer JavaDoc buf, Map JavaDoc labelNames, Label l) {
215         String JavaDoc name = (String JavaDoc) labelNames.get(l);
216         if (name == null) {
217             name = "L" + labelNames.size();
218             labelNames.put(l, name);
219         }
220         buf.append(name);
221     }
222
223 }
224
Popular Tags