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 org.objectweb.asm; 31 32 /** 33 * A non standard class, field, method or code attribute. 34 * 35 * @author Eric Bruneton 36 * @author Eugene Kuleshov 37 */ 38 public class Attribute { 39 40 /** 41 * The type of this attribute. 42 */ 43 public final String type; 44 45 /** 46 * The raw value of this attribute, used only for unknown attributes. 47 */ 48 byte[] value; 49 50 /** 51 * The next attribute in this attribute list. May be <tt>null</tt>. 52 */ 53 Attribute next; 54 55 /** 56 * Constructs a new empty attribute. 57 * 58 * @param type the type of the attribute. 59 */ Attribute(final String type)60 protected Attribute(final String type) { 61 this.type = type; 62 } 63 64 /** 65 * Returns <tt>true</tt> if this type of attribute is unknown. The default 66 * implementation of this method always returns <tt>true</tt>. 67 * 68 * @return <tt>true</tt> if this type of attribute is unknown. 69 */ isUnknown()70 public boolean isUnknown() { 71 return true; 72 } 73 74 /** 75 * Returns <tt>true</tt> if this type of attribute is a code attribute. 76 * 77 * @return <tt>true</tt> if this type of attribute is a code attribute. 78 */ isCodeAttribute()79 public boolean isCodeAttribute() { 80 return false; 81 } 82 83 /** 84 * Returns the labels corresponding to this attribute. 85 * 86 * @return the labels corresponding to this attribute, or <tt>null</tt> if 87 * this attribute is not a code attribute that contains labels. 88 */ getLabels()89 protected Label[] getLabels() { 90 return null; 91 } 92 93 /** 94 * Reads a {@link #type type} attribute. This method must return a <i>new</i> 95 * {@link Attribute} object, of type {@link #type type}, corresponding to 96 * the <tt>len</tt> bytes starting at the given offset, in the given class 97 * reader. 98 * 99 * @param cr the class that contains the attribute to be read. 100 * @param off index of the first byte of the attribute's content in {@link 101 * ClassReader#b cr.b}. The 6 attribute header bytes, containing the 102 * type and the length of the attribute, are not taken into account 103 * here. 104 * @param len the length of the attribute's content. 105 * @param buf buffer to be used to call 106 * {@link ClassReader#readUTF8 readUTF8}, 107 * {@link ClassReader#readClass(int,char[]) readClass} or 108 * {@link ClassReader#readConst readConst}. 109 * @param codeOff index of the first byte of code's attribute content in 110 * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is 111 * not a code attribute. The 6 attribute header bytes, containing the 112 * type and the length of the attribute, are not taken into account 113 * here. 114 * @param labels the labels of the method's code, or <tt>null</tt> if the 115 * attribute to be read is not a code attribute. 116 * @return a <i>new</i> {@link Attribute} object corresponding to the given 117 * bytes. 118 */ read( ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels)119 protected Attribute read( 120 ClassReader cr, 121 int off, 122 int len, 123 char[] buf, 124 int codeOff, 125 Label[] labels) 126 { 127 Attribute attr = new Attribute(type); 128 attr.value = new byte[len]; 129 System.arraycopy(cr.b, off, attr.value, 0, len); 130 return attr; 131 } 132 133 /** 134 * Returns the byte array form of this attribute. 135 * 136 * @param cw the class to which this attribute must be added. This parameter 137 * can be used to add to the constant pool of this class the items 138 * that corresponds to this attribute. 139 * @param code the bytecode of the method corresponding to this code 140 * attribute, or <tt>null</tt> if this attribute is not a code 141 * attributes. 142 * @param len the length of the bytecode of the method corresponding to this 143 * code attribute, or <tt>null</tt> if this attribute is not a code 144 * attribute. 145 * @param maxStack the maximum stack size of the method corresponding to 146 * this code attribute, or -1 if this attribute is not a code 147 * attribute. 148 * @param maxLocals the maximum number of local variables of the method 149 * corresponding to this code attribute, or -1 if this attribute is 150 * not a code attribute. 151 * @return the byte array form of this attribute. 152 */ write( ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals)153 protected ByteVector write( 154 ClassWriter cw, 155 byte[] code, 156 int len, 157 int maxStack, 158 int maxLocals) 159 { 160 ByteVector v = new ByteVector(); 161 v.data = value; 162 v.length = value.length; 163 return v; 164 } 165 166 /** 167 * Returns the length of the attribute list that begins with this attribute. 168 * 169 * @return the length of the attribute list that begins with this attribute. 170 */ getCount()171 final int getCount() { 172 int count = 0; 173 Attribute attr = this; 174 while (attr != null) { 175 count += 1; 176 attr = attr.next; 177 } 178 return count; 179 } 180 181 /** 182 * Returns the size of all the attributes in this attribute list. 183 * 184 * @param cw the class writer to be used to convert the attributes into byte 185 * arrays, with the {@link #write write} method. 186 * @param code the bytecode of the method corresponding to these code 187 * attributes, or <tt>null</tt> if these attributes are not code 188 * attributes. 189 * @param len the length of the bytecode of the method corresponding to 190 * these code attributes, or <tt>null</tt> if these attributes are 191 * not code attributes. 192 * @param maxStack the maximum stack size of the method corresponding to 193 * these code attributes, or -1 if these attributes are not code 194 * attributes. 195 * @param maxLocals the maximum number of local variables of the method 196 * corresponding to these code attributes, or -1 if these attributes 197 * are not code attributes. 198 * @return the size of all the attributes in this attribute list. This size 199 * includes the size of the attribute headers. 200 */ getSize( final ClassWriter cw, final byte[] code, final int len, final int maxStack, final int maxLocals)201 final int getSize( 202 final ClassWriter cw, 203 final byte[] code, 204 final int len, 205 final int maxStack, 206 final int maxLocals) 207 { 208 Attribute attr = this; 209 int size = 0; 210 while (attr != null) { 211 cw.newUTF8(attr.type); 212 size += attr.write(cw, code, len, maxStack, maxLocals).length + 6; 213 attr = attr.next; 214 } 215 return size; 216 } 217 218 /** 219 * Writes all the attributes of this attribute list in the given byte 220 * vector. 221 * 222 * @param cw the class writer to be used to convert the attributes into byte 223 * arrays, with the {@link #write write} method. 224 * @param code the bytecode of the method corresponding to these code 225 * attributes, or <tt>null</tt> if these attributes are not code 226 * attributes. 227 * @param len the length of the bytecode of the method corresponding to 228 * these code attributes, or <tt>null</tt> if these attributes are 229 * not code attributes. 230 * @param maxStack the maximum stack size of the method corresponding to 231 * these code attributes, or -1 if these attributes are not code 232 * attributes. 233 * @param maxLocals the maximum number of local variables of the method 234 * corresponding to these code attributes, or -1 if these attributes 235 * are not code attributes. 236 * @param out where the attributes must be written. 237 */ put( final ClassWriter cw, final byte[] code, final int len, final int maxStack, final int maxLocals, final ByteVector out)238 final void put( 239 final ClassWriter cw, 240 final byte[] code, 241 final int len, 242 final int maxStack, 243 final int maxLocals, 244 final ByteVector out) 245 { 246 Attribute attr = this; 247 while (attr != null) { 248 ByteVector b = attr.write(cw, code, len, maxStack, maxLocals); 249 out.putShort(cw.newUTF8(attr.type)).putInt(b.length); 250 out.putByteArray(b.data, 0, b.length); 251 attr = attr.next; 252 } 253 } 254 } 255