001: /**
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.drools.asm.util.attrs;
030:
031: import java.util.List;
032: import java.util.Map;
033:
034: import org.drools.asm.Attribute;
035: import org.drools.asm.ClassReader;
036: import org.drools.asm.Label;
037: import org.drools.asm.attrs.StackMapAttribute;
038: import org.drools.asm.attrs.StackMapFrame;
039: import org.drools.asm.attrs.StackMapType;
040:
041: /**
042: * An {@link ASMifiable} {@link StackMapAttribute} sub class.
043: *
044: * @author Eugene Kuleshov
045: */
046: public class ASMStackMapAttribute extends StackMapAttribute implements
047: ASMifiable, Traceable {
048: /**
049: * Length of the attribute used for comparison
050: */
051: private int len;
052:
053: public ASMStackMapAttribute() {
054: super ();
055: }
056:
057: public ASMStackMapAttribute(final List frames, final int len) {
058: super (frames);
059: this .len = len;
060: }
061:
062: protected Attribute read(final ClassReader cr, final int off,
063: final int len, final char[] buf, final int codeOff,
064: final Label[] labels) {
065: final StackMapAttribute attr = (StackMapAttribute) super .read(
066: cr, off, len, buf, codeOff, labels);
067:
068: return new ASMStackMapAttribute(attr.getFrames(), len);
069: }
070:
071: public void asmify(final StringBuffer buf, final String varName,
072: final Map labelNames) {
073: final List frames = getFrames();
074: buf.append("{\n");
075: buf.append("StackMapAttribute ").append(varName).append("Attr");
076: buf.append(" = new StackMapAttribute();\n");
077: if (frames.size() > 0) {
078: for (int i = 0; i < frames.size(); i++) {
079: asmify((StackMapFrame) frames.get(i), buf, varName
080: + "frame" + i, labelNames);
081: }
082: }
083: buf.append(varName).append(".visitAttribute(").append(varName);
084: buf.append("Attr);\n}\n");
085: }
086:
087: void asmify(final StackMapFrame f, final StringBuffer buf,
088: final String varName, final Map labelNames) {
089: declareLabel(buf, labelNames, f.label);
090: buf.append("{\n");
091:
092: buf.append("StackMapFrame ").append(varName).append(
093: " = new StackMapFrame();\n");
094:
095: buf.append(varName).append(".label = ").append(
096: labelNames.get(f.label)).append(";\n");
097:
098: asmifyTypeInfo(buf, varName, labelNames, f.locals, "locals");
099: asmifyTypeInfo(buf, varName, labelNames, f.stack, "stack");
100:
101: buf.append("cvAttr.frames.add(").append(varName).append(");\n");
102: buf.append("}\n");
103: }
104:
105: void asmifyTypeInfo(final StringBuffer buf, final String varName,
106: final Map labelNames, final List infos, final String field) {
107: if (infos.size() > 0) {
108: buf.append("{\n");
109: for (int i = 0; i < infos.size(); i++) {
110: final StackMapType typeInfo = (StackMapType) infos
111: .get(i);
112: final String localName = varName + "Info" + i;
113: final int type = typeInfo.getType();
114: buf
115: .append("StackMapType ")
116: .append(localName)
117: .append(
118: " = StackMapType.getTypeInfo( StackMapType.ITEM_")
119: .append(StackMapType.ITEM_NAMES[type]).append(
120: ");\n");
121:
122: switch (type) {
123: case StackMapType.ITEM_Object: //
124: buf.append(localName).append(".setObject(\"")
125: .append(typeInfo.getObject()).append(
126: "\");\n");
127: break;
128:
129: case StackMapType.ITEM_Uninitialized: //
130: declareLabel(buf, labelNames, typeInfo.getLabel());
131: buf.append(localName).append(".setLabel(").append(
132: labelNames.get(typeInfo.getLabel()))
133: .append(");\n");
134: break;
135: }
136: buf.append(varName).append(".").append(field).append(
137: ".add(").append(localName).append(");\n");
138: }
139: buf.append("}\n");
140: }
141: }
142:
143: static void declareLabel(final StringBuffer buf,
144: final Map labelNames, final Label l) {
145: String name = (String) labelNames.get(l);
146: if (name == null) {
147: name = "l" + labelNames.size();
148: labelNames.put(l, name);
149: buf.append("Label ").append(name).append(
150: " = new Label();\n");
151: }
152: }
153:
154: public void trace(final StringBuffer buf, final Map labelNames) {
155: final List frames = getFrames();
156: buf.append("[\n");
157: for (int i = 0; i < frames.size(); i++) {
158: final StackMapFrame f = (StackMapFrame) frames.get(i);
159:
160: buf.append(" Frame:");
161: appendLabel(buf, labelNames, f.label);
162:
163: buf.append(" locals[");
164: traceTypeInfo(buf, labelNames, f.locals);
165: buf.append("]");
166: buf.append(" stack[");
167: traceTypeInfo(buf, labelNames, f.stack);
168: buf.append("]\n");
169: }
170: buf.append(" ] length:").append(this .len).append("\n");
171: }
172:
173: private void traceTypeInfo(final StringBuffer buf,
174: final Map labelNames, final List infos) {
175: String sep = "";
176: for (int i = 0; i < infos.size(); i++) {
177: final StackMapType t = (StackMapType) infos.get(i);
178:
179: buf.append(sep)
180: .append(StackMapType.ITEM_NAMES[t.getType()]);
181: sep = ", ";
182: if (t.getType() == StackMapType.ITEM_Object) {
183: buf.append(":").append(t.getObject());
184: }
185: if (t.getType() == StackMapType.ITEM_Uninitialized) {
186: buf.append(":");
187: appendLabel(buf, labelNames, t.getLabel());
188: }
189: }
190: }
191:
192: protected void appendLabel(final StringBuffer buf,
193: final Map labelNames, final Label l) {
194: String name = (String) labelNames.get(l);
195: if (name == null) {
196: name = "L" + labelNames.size();
197: labelNames.put(l, name);
198: }
199: buf.append(name);
200: }
201:
202: }
|