KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > util > attrs > ASMStackMapAttribute


1 /**
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 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
31 package oracle.toplink.libraries.asm.util.attrs;
32
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import oracle.toplink.libraries.asm.Attribute;
37 import oracle.toplink.libraries.asm.ClassReader;
38 import oracle.toplink.libraries.asm.Label;
39 import oracle.toplink.libraries.asm.attrs.StackMapAttribute;
40 import oracle.toplink.libraries.asm.attrs.StackMapFrame;
41 import oracle.toplink.libraries.asm.attrs.StackMapType;
42
43 /**
44  * An {@link ASMifiable} {@link StackMapAttribute} sub class.
45  *
46  * @author Eugene Kuleshov
47  */

48
49 public class ASMStackMapAttribute extends StackMapAttribute
50   implements ASMifiable
51 {
52   
53   protected Attribute read (ClassReader cr, int off,
54     int len, char[] buf, int codeOff, Label[] labels)
55   {
56     StackMapAttribute attr =
57       (StackMapAttribute)super.read(cr, off, len, buf, codeOff, labels);
58     
59     ASMStackMapAttribute result = new ASMStackMapAttribute();
60     result.frames = attr.frames;
61     return result;
62   }
63
64   public void asmify (StringBuffer JavaDoc buf, String JavaDoc varName, Map JavaDoc labelNames) {
65     buf.append("{\n");
66     buf.append("StackMapAttribute ").append(varName).append("Attr");
67     buf.append(" = new StackMapAttribute();\n");
68     if (frames.size() > 0) {
69       for (int i = 0; i < frames.size(); i++) {
70         asmify((StackMapFrame)frames.get(i), buf, varName + "frame" + i, labelNames);
71       }
72     }
73     buf.append(varName).append(".visitAttribute(").append(varName);
74     buf.append("Attr);\n}\n");
75   }
76
77   void asmify (StackMapFrame f, StringBuffer JavaDoc buf, String JavaDoc varName, Map JavaDoc labelNames) {
78     declareLabel(buf, labelNames, f.label);
79     buf.append("{\n");
80     
81     buf.append("StackMapFrame ").append(varName).append( " = new StackMapFrame();\n");
82     
83     buf.append(varName).append(".label = ")
84       .append(labelNames.get(f.label)).append(";\n");
85
86     asmifyTypeInfo(buf, varName, labelNames, f.locals, "locals");
87     asmifyTypeInfo(buf, varName, labelNames, f.stack, "stack");
88
89     buf.append( "cvAttr.frames.add(").append(varName).append(");\n");
90     buf.append("}\n");
91   }
92
93   void asmifyTypeInfo (StringBuffer JavaDoc buf, String JavaDoc varName,
94                      Map JavaDoc labelNames, List JavaDoc infos, String JavaDoc field) {
95     if (infos.size() > 0) {
96       buf.append("{\n");
97       for (int i = 0; i < infos.size(); i++) {
98         StackMapType typeInfo = (StackMapType)infos.get(i);
99         String JavaDoc localName = varName + "Info" + i;
100         int type = typeInfo.getType();
101         buf.append("StackMapType ").append(localName)
102           .append(" = StackMapType.getTypeInfo( StackMapType.ITEM_")
103           .append(StackMapType.ITEM_NAMES[type]).append(");\n");
104
105         switch (type) {
106           case StackMapType.ITEM_Object: //
107
buf.append(localName).append(".setObject(\"")
108               .append(typeInfo.getObject()).append("\");\n");
109             break;
110
111           case StackMapType.ITEM_Uninitialized: //
112
declareLabel(buf, labelNames, typeInfo.getLabel());
113             buf.append(localName).append(".setLabel(")
114               .append(labelNames.get(typeInfo.getLabel())).append(");\n");
115             break;
116         }
117         buf.append(varName).append(".").append(field)
118           .append(".add(").append(localName).append(");\n");
119       }
120       buf.append("}\n");
121     }
122   }
123
124   static void declareLabel (StringBuffer JavaDoc buf, Map JavaDoc labelNames, Label l) {
125     String JavaDoc name = (String JavaDoc)labelNames.get(l);
126     if (name == null) {
127       name = "l" + labelNames.size();
128       labelNames.put(l, name);
129       buf.append("Label ").append(name).append(" = new Label();\n");
130     }
131   }
132 }
133
Popular Tags