Mercurial > lasercutter
view src/clojure/asm/Attribute.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 /***2 * ASM: a very small and fast Java bytecode manipulation framework3 * Copyright (c) 2000-2005 INRIA, France Telecom4 * All rights reserved.5 *6 * Redistribution and use in source and binary forms, with or without7 * modification, are permitted provided that the following conditions8 * are met:9 * 1. Redistributions of source code must retain the above copyright10 * notice, this list of conditions and the following disclaimer.11 * 2. Redistributions in binary form must reproduce the above copyright12 * notice, this list of conditions and the following disclaimer in the13 * documentation and/or other materials provided with the distribution.14 * 3. Neither the name of the copyright holders nor the names of its15 * contributors may be used to endorse or promote products derived from16 * 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, THE20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF28 * THE POSSIBILITY OF SUCH DAMAGE.29 */30 package clojure.asm;32 /**33 * A non standard class, field, method or code attribute.34 *35 * @author Eric Bruneton36 * @author Eugene Kuleshov37 */38 public class Attribute{40 /**41 * The type of this attribute.42 */43 public final String type;45 /**46 * The raw value of this attribute, used only for unknown attributes.47 */48 byte[] value;50 /**51 * The next attribute in this attribute list. May be <tt>null</tt>.52 */53 Attribute next;55 /**56 * Constructs a new empty attribute.57 *58 * @param type the type of the attribute.59 */60 protected Attribute(final String type){61 this.type = type;62 }64 /**65 * Returns <tt>true</tt> if this type of attribute is unknown. The default66 * implementation of this method always returns <tt>true</tt>.67 *68 * @return <tt>true</tt> if this type of attribute is unknown.69 */70 public boolean isUnknown(){71 return true;72 }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 */79 public boolean isCodeAttribute(){80 return false;81 }83 /**84 * Returns the labels corresponding to this attribute.85 *86 * @return the labels corresponding to this attribute, or <tt>null</tt> if87 * this attribute is not a code attribute that contains labels.88 */89 protected Label[] getLabels(){90 return null;91 }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 to96 * the <tt>len</tt> bytes starting at the given offset, in the given class97 * 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 {@link101 * ClassReader#b cr.b}. The 6 attribute header bytes, containing the102 * type and the length of the attribute, are not taken into account103 * here.104 * @param len the length of the attribute's content.105 * @param buf buffer to be used to call106 * {@link ClassReader#readUTF8 readUTF8},107 * {@link ClassReader#readClass(int,char[]) readClass} or108 * {@link ClassReader#readConst readConst}.109 * @param codeOff index of the first byte of code's attribute content in110 * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is111 * not a code attribute. The 6 attribute header bytes, containing the112 * type and the length of the attribute, are not taken into account113 * here.114 * @param labels the labels of the method's code, or <tt>null</tt> if the115 * attribute to be read is not a code attribute.116 * @return a <i>new</i> {@link Attribute} object corresponding to the given117 * bytes.118 */119 protected Attribute read(120 final ClassReader cr,121 final int off,122 final int len,123 final char[] buf,124 final int codeOff,125 final Label[] labels){126 Attribute attr = new Attribute(type);127 attr.value = new byte[len];128 System.arraycopy(cr.b, off, attr.value, 0, len);129 return attr;130 }132 /**133 * Returns the byte array form of this attribute.134 *135 * @param cw the class to which this attribute must be added. This parameter136 * can be used to add to the constant pool of this class the items137 * that corresponds to this attribute.138 * @param code the bytecode of the method corresponding to this code139 * attribute, or <tt>null</tt> if this attribute is not a code140 * attributes.141 * @param len the length of the bytecode of the method corresponding to this142 * code attribute, or <tt>null</tt> if this attribute is not a code143 * attribute.144 * @param maxStack the maximum stack size of the method corresponding to145 * this code attribute, or -1 if this attribute is not a code146 * attribute.147 * @param maxLocals the maximum number of local variables of the method148 * corresponding to this code attribute, or -1 if this attribute is149 * not a code attribute.150 * @return the byte array form of this attribute.151 */152 protected ByteVector write(153 final ClassWriter cw,154 final byte[] code,155 final int len,156 final int maxStack,157 final int maxLocals){158 ByteVector v = new ByteVector();159 v.data = value;160 v.length = value.length;161 return v;162 }164 /**165 * Returns the length of the attribute list that begins with this attribute.166 *167 * @return the length of the attribute list that begins with this attribute.168 */169 final int getCount(){170 int count = 0;171 Attribute attr = this;172 while(attr != null)173 {174 count += 1;175 attr = attr.next;176 }177 return count;178 }180 /**181 * Returns the size of all the attributes in this attribute list.182 *183 * @param cw the class writer to be used to convert the attributes into byte184 * arrays, with the {@link #write write} method.185 * @param code the bytecode of the method corresponding to these code186 * attributes, or <tt>null</tt> if these attributes are not code187 * attributes.188 * @param len the length of the bytecode of the method corresponding to189 * these code attributes, or <tt>null</tt> if these attributes are190 * not code attributes.191 * @param maxStack the maximum stack size of the method corresponding to192 * these code attributes, or -1 if these attributes are not code193 * attributes.194 * @param maxLocals the maximum number of local variables of the method195 * corresponding to these code attributes, or -1 if these attributes196 * are not code attributes.197 * @return the size of all the attributes in this attribute list. This size198 * includes the size of the attribute headers.199 */200 final int getSize(201 final ClassWriter cw,202 final byte[] code,203 final int len,204 final int maxStack,205 final int maxLocals){206 Attribute attr = this;207 int size = 0;208 while(attr != null)209 {210 cw.newUTF8(attr.type);211 size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;212 attr = attr.next;213 }214 return size;215 }217 /**218 * Writes all the attributes of this attribute list in the given byte219 * vector.220 *221 * @param cw the class writer to be used to convert the attributes into byte222 * arrays, with the {@link #write write} method.223 * @param code the bytecode of the method corresponding to these code224 * attributes, or <tt>null</tt> if these attributes are not code225 * attributes.226 * @param len the length of the bytecode of the method corresponding to227 * these code attributes, or <tt>null</tt> if these attributes are228 * not code attributes.229 * @param maxStack the maximum stack size of the method corresponding to230 * these code attributes, or -1 if these attributes are not code231 * attributes.232 * @param maxLocals the maximum number of local variables of the method233 * corresponding to these code attributes, or -1 if these attributes234 * are not code attributes.235 * @param out where the attributes must be written.236 */237 final void put(238 final ClassWriter cw,239 final byte[] code,240 final int len,241 final int maxStack,242 final int maxLocals,243 final ByteVector out){244 Attribute attr = this;245 while(attr != null)246 {247 ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);248 out.putShort(cw.newUTF8(attr.type)).putInt(b.length);249 out.putByteArray(b.data, 0, b.length);250 attr = attr.next;251 }252 }253 }