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 net.sf.retrotranslator.runtime.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(ClassReader cr, int off, int len,
119: char[] buf, int codeOff, Label[] labels) {
120: Attribute attr = new Attribute(type);
121: attr.value = new byte[len];
122: System.arraycopy(cr.b, off, attr.value, 0, len);
123: return attr;
124: }
125:
126: /**
127: * Returns the byte array form of this attribute.
128: *
129: * @param cw the class to which this attribute must be added. This parameter
130: * can be used to add to the constant pool of this class the items
131: * that corresponds to this attribute.
132: * @param code the bytecode of the method corresponding to this code
133: * attribute, or <tt>null</tt> if this attribute is not a code
134: * attributes.
135: * @param len the length of the bytecode of the method corresponding to this
136: * code attribute, or <tt>null</tt> if this attribute is not a code
137: * attribute.
138: * @param maxStack the maximum stack size of the method corresponding to
139: * this code attribute, or -1 if this attribute is not a code
140: * attribute.
141: * @param maxLocals the maximum number of local variables of the method
142: * corresponding to this code attribute, or -1 if this attribute is
143: * not a code attribute.
144: * @return the byte array form of this attribute.
145: */
146: protected ByteVector write(ClassWriter cw, byte[] code, int len,
147: int maxStack, int maxLocals) {
148: ByteVector v = new ByteVector();
149: v.data = value;
150: v.length = value.length;
151: return v;
152: }
153:
154: /**
155: * Returns the length of the attribute list that begins with this attribute.
156: *
157: * @return the length of the attribute list that begins with this attribute.
158: */
159: final int getCount() {
160: int count = 0;
161: Attribute attr = this ;
162: while (attr != null) {
163: count += 1;
164: attr = attr.next;
165: }
166: return count;
167: }
168:
169: /**
170: * Returns the size of all the attributes in this attribute list.
171: *
172: * @param cw the class writer to be used to convert the attributes into byte
173: * arrays, with the {@link #write write} method.
174: * @param code the bytecode of the method corresponding to these code
175: * attributes, or <tt>null</tt> if these attributes are not code
176: * attributes.
177: * @param len the length of the bytecode of the method corresponding to
178: * these code attributes, or <tt>null</tt> if these attributes are
179: * not code attributes.
180: * @param maxStack the maximum stack size of the method corresponding to
181: * these code attributes, or -1 if these attributes are not code
182: * attributes.
183: * @param maxLocals the maximum number of local variables of the method
184: * corresponding to these code attributes, or -1 if these attributes
185: * are not code attributes.
186: * @return the size of all the attributes in this attribute list. This size
187: * includes the size of the attribute headers.
188: */
189: final int getSize(final ClassWriter cw, final byte[] code,
190: final int len, final int maxStack, final int maxLocals) {
191: Attribute attr = this ;
192: int size = 0;
193: while (attr != null) {
194: cw.newUTF8(attr.type);
195: size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
196: attr = attr.next;
197: }
198: return size;
199: }
200:
201: /**
202: * Writes all the attributes of this attribute list in the given byte
203: * vector.
204: *
205: * @param cw the class writer to be used to convert the attributes into byte
206: * arrays, with the {@link #write write} method.
207: * @param code the bytecode of the method corresponding to these code
208: * attributes, or <tt>null</tt> if these attributes are not code
209: * attributes.
210: * @param len the length of the bytecode of the method corresponding to
211: * these code attributes, or <tt>null</tt> if these attributes are
212: * not code attributes.
213: * @param maxStack the maximum stack size of the method corresponding to
214: * these code attributes, or -1 if these attributes are not code
215: * attributes.
216: * @param maxLocals the maximum number of local variables of the method
217: * corresponding to these code attributes, or -1 if these attributes
218: * are not code attributes.
219: * @param out where the attributes must be written.
220: */
221: final void put(final ClassWriter cw, final byte[] code,
222: final int len, final int maxStack, final int maxLocals,
223: final ByteVector out) {
224: Attribute attr = this ;
225: while (attr != null) {
226: ByteVector b = attr.write(cw, code, len, maxStack,
227: maxLocals);
228: out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
229: out.putByteArray(b.data, 0, b.length);
230: attr = attr.next;
231: }
232: }
233: }
|