diff src/clojure/asm/FieldWriter.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/FieldWriter.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,290 @@
     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 + * An {@link FieldVisitor} that generates Java fields in bytecode form.
    1.37 + *
    1.38 + * @author Eric Bruneton
    1.39 + */
    1.40 +final class FieldWriter implements FieldVisitor{
    1.41 +
    1.42 +/**
    1.43 + * Next field writer (see {@link ClassWriter#firstField firstField}).
    1.44 + */
    1.45 +FieldWriter next;
    1.46 +
    1.47 +/**
    1.48 + * The class writer to which this field must be added.
    1.49 + */
    1.50 +private ClassWriter cw;
    1.51 +
    1.52 +/**
    1.53 + * Access flags of this field.
    1.54 + */
    1.55 +private int access;
    1.56 +
    1.57 +/**
    1.58 + * The index of the constant pool item that contains the name of this
    1.59 + * method.
    1.60 + */
    1.61 +private int name;
    1.62 +
    1.63 +/**
    1.64 + * The index of the constant pool item that contains the descriptor of this
    1.65 + * field.
    1.66 + */
    1.67 +private int desc;
    1.68 +
    1.69 +/**
    1.70 + * The index of the constant pool item that contains the signature of this
    1.71 + * field.
    1.72 + */
    1.73 +private int signature;
    1.74 +
    1.75 +/**
    1.76 + * The index of the constant pool item that contains the constant value of
    1.77 + * this field.
    1.78 + */
    1.79 +private int value;
    1.80 +
    1.81 +/**
    1.82 + * The runtime visible annotations of this field. May be <tt>null</tt>.
    1.83 + */
    1.84 +private AnnotationWriter anns;
    1.85 +
    1.86 +/**
    1.87 + * The runtime invisible annotations of this field. May be <tt>null</tt>.
    1.88 + */
    1.89 +private AnnotationWriter ianns;
    1.90 +
    1.91 +/**
    1.92 + * The non standard attributes of this field. May be <tt>null</tt>.
    1.93 + */
    1.94 +private Attribute attrs;
    1.95 +
    1.96 +// ------------------------------------------------------------------------
    1.97 +// Constructor
    1.98 +// ------------------------------------------------------------------------
    1.99 +
   1.100 +/**
   1.101 + * Constructs a new {@link FieldWriter}.
   1.102 + *
   1.103 + * @param cw        the class writer to which this field must be added.
   1.104 + * @param access    the field's access flags (see {@link Opcodes}).
   1.105 + * @param name      the field's name.
   1.106 + * @param desc      the field's descriptor (see {@link Type}).
   1.107 + * @param signature the field's signature. May be <tt>null</tt>.
   1.108 + * @param value     the field's constant value. May be <tt>null</tt>.
   1.109 + */
   1.110 +protected FieldWriter(
   1.111 +		final ClassWriter cw,
   1.112 +		final int access,
   1.113 +		final String name,
   1.114 +		final String desc,
   1.115 +		final String signature,
   1.116 +		final Object value){
   1.117 +	if(cw.firstField == null)
   1.118 +		{
   1.119 +		cw.firstField = this;
   1.120 +		}
   1.121 +	else
   1.122 +		{
   1.123 +		cw.lastField.next = this;
   1.124 +		}
   1.125 +	cw.lastField = this;
   1.126 +	this.cw = cw;
   1.127 +	this.access = access;
   1.128 +	this.name = cw.newUTF8(name);
   1.129 +	this.desc = cw.newUTF8(desc);
   1.130 +	if(signature != null)
   1.131 +		{
   1.132 +		this.signature = cw.newUTF8(signature);
   1.133 +		}
   1.134 +	if(value != null)
   1.135 +		{
   1.136 +		this.value = cw.newConstItem(value).index;
   1.137 +		}
   1.138 +}
   1.139 +
   1.140 +// ------------------------------------------------------------------------
   1.141 +// Implementation of the FieldVisitor interface
   1.142 +// ------------------------------------------------------------------------
   1.143 +
   1.144 +public AnnotationVisitor visitAnnotation(
   1.145 +		final String desc,
   1.146 +		final boolean visible){
   1.147 +	ByteVector bv = new ByteVector();
   1.148 +	// write type, and reserve space for values count
   1.149 +	bv.putShort(cw.newUTF8(desc)).putShort(0);
   1.150 +	AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
   1.151 +	if(visible)
   1.152 +		{
   1.153 +		aw.next = anns;
   1.154 +		anns = aw;
   1.155 +		}
   1.156 +	else
   1.157 +		{
   1.158 +		aw.next = ianns;
   1.159 +		ianns = aw;
   1.160 +		}
   1.161 +	return aw;
   1.162 +}
   1.163 +
   1.164 +public void visitAttribute(final Attribute attr){
   1.165 +	attr.next = attrs;
   1.166 +	attrs = attr;
   1.167 +}
   1.168 +
   1.169 +public void visitEnd(){
   1.170 +}
   1.171 +
   1.172 +// ------------------------------------------------------------------------
   1.173 +// Utility methods
   1.174 +// ------------------------------------------------------------------------
   1.175 +
   1.176 +/**
   1.177 + * Returns the size of this field.
   1.178 + *
   1.179 + * @return the size of this field.
   1.180 + */
   1.181 +int getSize(){
   1.182 +	int size = 8;
   1.183 +	if(value != 0)
   1.184 +		{
   1.185 +		cw.newUTF8("ConstantValue");
   1.186 +		size += 8;
   1.187 +		}
   1.188 +	if((access & Opcodes.ACC_SYNTHETIC) != 0
   1.189 +	   && (cw.version & 0xffff) < Opcodes.V1_5)
   1.190 +		{
   1.191 +		cw.newUTF8("Synthetic");
   1.192 +		size += 6;
   1.193 +		}
   1.194 +	if((access & Opcodes.ACC_DEPRECATED) != 0)
   1.195 +		{
   1.196 +		cw.newUTF8("Deprecated");
   1.197 +		size += 6;
   1.198 +		}
   1.199 +	if(signature != 0)
   1.200 +		{
   1.201 +		cw.newUTF8("Signature");
   1.202 +		size += 8;
   1.203 +		}
   1.204 +	if(anns != null)
   1.205 +		{
   1.206 +		cw.newUTF8("RuntimeVisibleAnnotations");
   1.207 +		size += 8 + anns.getSize();
   1.208 +		}
   1.209 +	if(ianns != null)
   1.210 +		{
   1.211 +		cw.newUTF8("RuntimeInvisibleAnnotations");
   1.212 +		size += 8 + ianns.getSize();
   1.213 +		}
   1.214 +	if(attrs != null)
   1.215 +		{
   1.216 +		size += attrs.getSize(cw, null, 0, -1, -1);
   1.217 +		}
   1.218 +	return size;
   1.219 +}
   1.220 +
   1.221 +/**
   1.222 + * Puts the content of this field into the given byte vector.
   1.223 + *
   1.224 + * @param out where the content of this field must be put.
   1.225 + */
   1.226 +void put(final ByteVector out){
   1.227 +	out.putShort(access).putShort(name).putShort(desc);
   1.228 +	int attributeCount = 0;
   1.229 +	if(value != 0)
   1.230 +		{
   1.231 +		++attributeCount;
   1.232 +		}
   1.233 +	if((access & Opcodes.ACC_SYNTHETIC) != 0
   1.234 +	   && (cw.version & 0xffff) < Opcodes.V1_5)
   1.235 +		{
   1.236 +		++attributeCount;
   1.237 +		}
   1.238 +	if((access & Opcodes.ACC_DEPRECATED) != 0)
   1.239 +		{
   1.240 +		++attributeCount;
   1.241 +		}
   1.242 +	if(signature != 0)
   1.243 +		{
   1.244 +		++attributeCount;
   1.245 +		}
   1.246 +	if(anns != null)
   1.247 +		{
   1.248 +		++attributeCount;
   1.249 +		}
   1.250 +	if(ianns != null)
   1.251 +		{
   1.252 +		++attributeCount;
   1.253 +		}
   1.254 +	if(attrs != null)
   1.255 +		{
   1.256 +		attributeCount += attrs.getCount();
   1.257 +		}
   1.258 +	out.putShort(attributeCount);
   1.259 +	if(value != 0)
   1.260 +		{
   1.261 +		out.putShort(cw.newUTF8("ConstantValue"));
   1.262 +		out.putInt(2).putShort(value);
   1.263 +		}
   1.264 +	if((access & Opcodes.ACC_SYNTHETIC) != 0
   1.265 +	   && (cw.version & 0xffff) < Opcodes.V1_5)
   1.266 +		{
   1.267 +		out.putShort(cw.newUTF8("Synthetic")).putInt(0);
   1.268 +		}
   1.269 +	if((access & Opcodes.ACC_DEPRECATED) != 0)
   1.270 +		{
   1.271 +		out.putShort(cw.newUTF8("Deprecated")).putInt(0);
   1.272 +		}
   1.273 +	if(signature != 0)
   1.274 +		{
   1.275 +		out.putShort(cw.newUTF8("Signature"));
   1.276 +		out.putInt(2).putShort(signature);
   1.277 +		}
   1.278 +	if(anns != null)
   1.279 +		{
   1.280 +		out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
   1.281 +		anns.put(out);
   1.282 +		}
   1.283 +	if(ianns != null)
   1.284 +		{
   1.285 +		out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
   1.286 +		ianns.put(out);
   1.287 +		}
   1.288 +	if(attrs != null)
   1.289 +		{
   1.290 +		attrs.put(cw, null, 0, -1, -1, out);
   1.291 +		}
   1.292 +}
   1.293 +}