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;
030:
031: /**
032: * A non standard class, field, method or code attribute.
033: *
034: * @author Eric Bruneton
035: * @author Eugene Kuleshov
036: */
037: public class Attribute {
038:
039: /**
040: * The type of this attribute.
041: */
042: public final String type;
043:
044: /**
045: * The raw value of this attribute, used only for unknown attributes.
046: */
047: byte[] value;
048:
049: /**
050: * The next attribute in this attribute list. May be <tt>null</tt>.
051: */
052: Attribute next;
053:
054: /**
055: * Constructs a new empty attribute.
056: *
057: * @param type the type of the attribute.
058: */
059: protected Attribute(final String type) {
060: this .type = type;
061: }
062:
063: /**
064: * Returns <tt>true</tt> if this type of attribute is unknown. The default
065: * implementation of this method always returns <tt>true</tt>.
066: *
067: * @return <tt>true</tt> if this type of attribute is unknown.
068: */
069: public boolean isUnknown() {
070: return true;
071: }
072:
073: /**
074: * Returns <tt>true</tt> if this type of attribute is a code attribute.
075: *
076: * @return <tt>true</tt> if this type of attribute is a code attribute.
077: */
078: public boolean isCodeAttribute() {
079: return false;
080: }
081:
082: /**
083: * Returns the labels corresponding to this attribute.
084: *
085: * @return the labels corresponding to this attribute, or <tt>null</tt> if
086: * this attribute is not a code attribute that contains labels.
087: */
088: protected Label[] getLabels() {
089: return null;
090: }
091:
092: /**
093: * Reads a {@link #type type} attribute. This method must return a <i>new</i>
094: * {@link Attribute} object, of type {@link #type type}, corresponding to
095: * the <tt>len</tt> bytes starting at the given offset, in the given class
096: * reader.
097: *
098: * @param cr the class that contains the attribute to be read.
099: * @param off index of the first byte of the attribute's content in {@link
100: * ClassReader#b cr.b}. The 6 attribute header bytes, containing the
101: * type and the length of the attribute, are not taken into account
102: * here.
103: * @param len the length of the attribute's content.
104: * @param buf buffer to be used to call
105: * {@link ClassReader#readUTF8 readUTF8},
106: * {@link ClassReader#readClass(int,char[]) readClass} or
107: * {@link ClassReader#readConst readConst}.
108: * @param codeOff index of the first byte of code's attribute content in
109: * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
110: * not a code attribute. The 6 attribute header bytes, containing the
111: * type and the length of the attribute, are not taken into account
112: * here.
113: * @param labels the labels of the method's code, or <tt>null</tt> if the
114: * attribute to be read is not a code attribute.
115: * @return a <i>new</i> {@link Attribute} object corresponding to the given
116: * bytes.
117: */
118: protected Attribute read(final ClassReader cr, final int off,
119: final int len, final char[] buf, final int codeOff,
120: final Label[] labels) {
121: final Attribute attr = new Attribute(this .type);
122: attr.value = new byte[len];
123: System.arraycopy(cr.b, off, attr.value, 0, len);
124: return attr;
125: }
126:
127: /**
128: * Returns the byte array form of this attribute.
129: *
130: * @param cw the class to which this attribute must be added. This parameter
131: * can be used to add to the constant pool of this class the items
132: * that corresponds to this attribute.
133: * @param code the bytecode of the method corresponding to this code
134: * attribute, or <tt>null</tt> if this attribute is not a code
135: * attributes.
136: * @param len the length of the bytecode of the method corresponding to this
137: * code attribute, or <tt>null</tt> if this attribute is not a code
138: * attribute.
139: * @param maxStack the maximum stack size of the method corresponding to
140: * this code attribute, or -1 if this attribute is not a code
141: * attribute.
142: * @param maxLocals the maximum number of local variables of the method
143: * corresponding to this code attribute, or -1 if this attribute is
144: * not a code attribute.
145: * @return the byte array form of this attribute.
146: */
147: protected ByteVector write(final ClassWriter cw, final byte[] code,
148: final int len, final int maxStack, final int maxLocals) {
149: final ByteVector v = new ByteVector();
150: v.data = this .value;
151: v.length = this .value.length;
152: return v;
153: }
154:
155: /**
156: * Returns the length of the attribute list that begins with this attribute.
157: *
158: * @return the length of the attribute list that begins with this attribute.
159: */
160: final int getCount() {
161: int count = 0;
162: Attribute attr = this ;
163: while (attr != null) {
164: count += 1;
165: attr = attr.next;
166: }
167: return count;
168: }
169:
170: /**
171: * Returns the size of all the attributes in this attribute list.
172: *
173: * @param cw the class writer to be used to convert the attributes into byte
174: * arrays, with the {@link #write write} method.
175: * @param code the bytecode of the method corresponding to these code
176: * attributes, or <tt>null</tt> if these attributes are not code
177: * attributes.
178: * @param len the length of the bytecode of the method corresponding to
179: * these code attributes, or <tt>null</tt> if these attributes are
180: * not code attributes.
181: * @param maxStack the maximum stack size of the method corresponding to
182: * these code attributes, or -1 if these attributes are not code
183: * attributes.
184: * @param maxLocals the maximum number of local variables of the method
185: * corresponding to these code attributes, or -1 if these attributes
186: * are not code attributes.
187: * @return the size of all the attributes in this attribute list. This size
188: * includes the size of the attribute headers.
189: */
190: final int getSize(final ClassWriter cw, final byte[] code,
191: final int len, final int maxStack, final int maxLocals) {
192: Attribute attr = this ;
193: int size = 0;
194: while (attr != null) {
195: cw.newUTF8(attr.type);
196: size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
197: attr = attr.next;
198: }
199: return size;
200: }
201:
202: /**
203: * Writes all the attributes of this attribute list in the given byte
204: * vector.
205: *
206: * @param cw the class writer to be used to convert the attributes into byte
207: * arrays, with the {@link #write write} method.
208: * @param code the bytecode of the method corresponding to these code
209: * attributes, or <tt>null</tt> if these attributes are not code
210: * attributes.
211: * @param len the length of the bytecode of the method corresponding to
212: * these code attributes, or <tt>null</tt> if these attributes are
213: * not code attributes.
214: * @param maxStack the maximum stack size of the method corresponding to
215: * these code attributes, or -1 if these attributes are not code
216: * attributes.
217: * @param maxLocals the maximum number of local variables of the method
218: * corresponding to these code attributes, or -1 if these attributes
219: * are not code attributes.
220: * @param out where the attributes must be written.
221: */
222: final void put(final ClassWriter cw, final byte[] code,
223: final int len, final int maxStack, final int maxLocals,
224: final ByteVector out) {
225: Attribute attr = this ;
226: while (attr != null) {
227: final ByteVector b = attr.write(cw, code, len, maxStack,
228: maxLocals);
229: out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
230: out.putByteArray(b.data, 0, b.length);
231: attr = attr.next;
232: }
233: }
234: }
|