diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/asm/Attribute.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,253 @@
     1.4 +/***
     1.5 + * ASM: a very small and fast Java bytecode manipulation framework
     1.6 + * Copyright (c) 2000-2005 INRIA, France Telecom
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 3. Neither the name of the copyright holders nor the names of its
    1.18 + *    contributors may be used to endorse or promote products derived from
    1.19 + *    this software without specific prior written permission.
    1.20 + *
    1.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    1.31 + * THE POSSIBILITY OF SUCH DAMAGE.
    1.32 + */
    1.33 +package clojure.asm;
    1.34 +
    1.35 +/**
    1.36 + * A non standard class, field, method or code attribute.
    1.37 + *
    1.38 + * @author Eric Bruneton
    1.39 + * @author Eugene Kuleshov
    1.40 + */
    1.41 +public class Attribute{
    1.42 +
    1.43 +/**
    1.44 + * The type of this attribute.
    1.45 + */
    1.46 +public final String type;
    1.47 +
    1.48 +/**
    1.49 + * The raw value of this attribute, used only for unknown attributes.
    1.50 + */
    1.51 +byte[] value;
    1.52 +
    1.53 +/**
    1.54 + * The next attribute in this attribute list. May be <tt>null</tt>.
    1.55 + */
    1.56 +Attribute next;
    1.57 +
    1.58 +/**
    1.59 + * Constructs a new empty attribute.
    1.60 + *
    1.61 + * @param type the type of the attribute.
    1.62 + */
    1.63 +protected Attribute(final String type){
    1.64 +	this.type = type;
    1.65 +}
    1.66 +
    1.67 +/**
    1.68 + * Returns <tt>true</tt> if this type of attribute is unknown. The default
    1.69 + * implementation of this method always returns <tt>true</tt>.
    1.70 + *
    1.71 + * @return <tt>true</tt> if this type of attribute is unknown.
    1.72 + */
    1.73 +public boolean isUnknown(){
    1.74 +	return true;
    1.75 +}
    1.76 +
    1.77 +/**
    1.78 + * Returns <tt>true</tt> if this type of attribute is a code attribute.
    1.79 + *
    1.80 + * @return <tt>true</tt> if this type of attribute is a code attribute.
    1.81 + */
    1.82 +public boolean isCodeAttribute(){
    1.83 +	return false;
    1.84 +}
    1.85 +
    1.86 +/**
    1.87 + * Returns the labels corresponding to this attribute.
    1.88 + *
    1.89 + * @return the labels corresponding to this attribute, or <tt>null</tt> if
    1.90 + *         this attribute is not a code attribute that contains labels.
    1.91 + */
    1.92 +protected Label[] getLabels(){
    1.93 +	return null;
    1.94 +}
    1.95 +
    1.96 +/**
    1.97 + * Reads a {@link #type type} attribute. This method must return a <i>new</i>
    1.98 + * {@link Attribute} object, of type {@link #type type}, corresponding to
    1.99 + * the <tt>len</tt> bytes starting at the given offset, in the given class
   1.100 + * reader.
   1.101 + *
   1.102 + * @param cr      the class that contains the attribute to be read.
   1.103 + * @param off     index of the first byte of the attribute's content in {@link
   1.104 + *                ClassReader#b cr.b}. The 6 attribute header bytes, containing the
   1.105 + *                type and the length of the attribute, are not taken into account
   1.106 + *                here.
   1.107 + * @param len     the length of the attribute's content.
   1.108 + * @param buf     buffer to be used to call
   1.109 + *                {@link ClassReader#readUTF8 readUTF8},
   1.110 + *                {@link ClassReader#readClass(int,char[]) readClass} or
   1.111 + *                {@link ClassReader#readConst readConst}.
   1.112 + * @param codeOff index of the first byte of code's attribute content in
   1.113 + *                {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
   1.114 + *                not a code attribute. The 6 attribute header bytes, containing the
   1.115 + *                type and the length of the attribute, are not taken into account
   1.116 + *                here.
   1.117 + * @param labels  the labels of the method's code, or <tt>null</tt> if the
   1.118 + *                attribute to be read is not a code attribute.
   1.119 + * @return a <i>new</i> {@link Attribute} object corresponding to the given
   1.120 + *         bytes.
   1.121 + */
   1.122 +protected Attribute read(
   1.123 +		final ClassReader cr,
   1.124 +		final int off,
   1.125 +		final int len,
   1.126 +		final char[] buf,
   1.127 +		final int codeOff,
   1.128 +		final Label[] labels){
   1.129 +	Attribute attr = new Attribute(type);
   1.130 +	attr.value = new byte[len];
   1.131 +	System.arraycopy(cr.b, off, attr.value, 0, len);
   1.132 +	return attr;
   1.133 +}
   1.134 +
   1.135 +/**
   1.136 + * Returns the byte array form of this attribute.
   1.137 + *
   1.138 + * @param cw        the class to which this attribute must be added. This parameter
   1.139 + *                  can be used to add to the constant pool of this class the items
   1.140 + *                  that corresponds to this attribute.
   1.141 + * @param code      the bytecode of the method corresponding to this code
   1.142 + *                  attribute, or <tt>null</tt> if this attribute is not a code
   1.143 + *                  attributes.
   1.144 + * @param len       the length of the bytecode of the method corresponding to this
   1.145 + *                  code attribute, or <tt>null</tt> if this attribute is not a code
   1.146 + *                  attribute.
   1.147 + * @param maxStack  the maximum stack size of the method corresponding to
   1.148 + *                  this code attribute, or -1 if this attribute is not a code
   1.149 + *                  attribute.
   1.150 + * @param maxLocals the maximum number of local variables of the method
   1.151 + *                  corresponding to this code attribute, or -1 if this attribute is
   1.152 + *                  not a code attribute.
   1.153 + * @return the byte array form of this attribute.
   1.154 + */
   1.155 +protected ByteVector write(
   1.156 +		final ClassWriter cw,
   1.157 +		final byte[] code,
   1.158 +		final int len,
   1.159 +		final int maxStack,
   1.160 +		final int maxLocals){
   1.161 +	ByteVector v = new ByteVector();
   1.162 +	v.data = value;
   1.163 +	v.length = value.length;
   1.164 +	return v;
   1.165 +}
   1.166 +
   1.167 +/**
   1.168 + * Returns the length of the attribute list that begins with this attribute.
   1.169 + *
   1.170 + * @return the length of the attribute list that begins with this attribute.
   1.171 + */
   1.172 +final int getCount(){
   1.173 +	int count = 0;
   1.174 +	Attribute attr = this;
   1.175 +	while(attr != null)
   1.176 +		{
   1.177 +		count += 1;
   1.178 +		attr = attr.next;
   1.179 +		}
   1.180 +	return count;
   1.181 +}
   1.182 +
   1.183 +/**
   1.184 + * Returns the size of all the attributes in this attribute list.
   1.185 + *
   1.186 + * @param cw        the class writer to be used to convert the attributes into byte
   1.187 + *                  arrays, with the {@link #write write} method.
   1.188 + * @param code      the bytecode of the method corresponding to these code
   1.189 + *                  attributes, or <tt>null</tt> if these attributes are not code
   1.190 + *                  attributes.
   1.191 + * @param len       the length of the bytecode of the method corresponding to
   1.192 + *                  these code attributes, or <tt>null</tt> if these attributes are
   1.193 + *                  not code attributes.
   1.194 + * @param maxStack  the maximum stack size of the method corresponding to
   1.195 + *                  these code attributes, or -1 if these attributes are not code
   1.196 + *                  attributes.
   1.197 + * @param maxLocals the maximum number of local variables of the method
   1.198 + *                  corresponding to these code attributes, or -1 if these attributes
   1.199 + *                  are not code attributes.
   1.200 + * @return the size of all the attributes in this attribute list. This size
   1.201 + *         includes the size of the attribute headers.
   1.202 + */
   1.203 +final int getSize(
   1.204 +		final ClassWriter cw,
   1.205 +		final byte[] code,
   1.206 +		final int len,
   1.207 +		final int maxStack,
   1.208 +		final int maxLocals){
   1.209 +	Attribute attr = this;
   1.210 +	int size = 0;
   1.211 +	while(attr != null)
   1.212 +		{
   1.213 +		cw.newUTF8(attr.type);
   1.214 +		size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
   1.215 +		attr = attr.next;
   1.216 +		}
   1.217 +	return size;
   1.218 +}
   1.219 +
   1.220 +/**
   1.221 + * Writes all the attributes of this attribute list in the given byte
   1.222 + * vector.
   1.223 + *
   1.224 + * @param cw        the class writer to be used to convert the attributes into byte
   1.225 + *                  arrays, with the {@link #write write} method.
   1.226 + * @param code      the bytecode of the method corresponding to these code
   1.227 + *                  attributes, or <tt>null</tt> if these attributes are not code
   1.228 + *                  attributes.
   1.229 + * @param len       the length of the bytecode of the method corresponding to
   1.230 + *                  these code attributes, or <tt>null</tt> if these attributes are
   1.231 + *                  not code attributes.
   1.232 + * @param maxStack  the maximum stack size of the method corresponding to
   1.233 + *                  these code attributes, or -1 if these attributes are not code
   1.234 + *                  attributes.
   1.235 + * @param maxLocals the maximum number of local variables of the method
   1.236 + *                  corresponding to these code attributes, or -1 if these attributes
   1.237 + *                  are not code attributes.
   1.238 + * @param out       where the attributes must be written.
   1.239 + */
   1.240 +final void put(
   1.241 +		final ClassWriter cw,
   1.242 +		final byte[] code,
   1.243 +		final int len,
   1.244 +		final int maxStack,
   1.245 +		final int maxLocals,
   1.246 +		final ByteVector out){
   1.247 +	Attribute attr = this;
   1.248 +	while(attr != null)
   1.249 +		{
   1.250 +		ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);
   1.251 +		out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
   1.252 +		out.putByteArray(b.data, 0, b.length);
   1.253 +		attr = attr.next;
   1.254 +		}
   1.255 +}
   1.256 +}