annotate 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
rev   line source
rlm@10 1 /***
rlm@10 2 * ASM: a very small and fast Java bytecode manipulation framework
rlm@10 3 * Copyright (c) 2000-2005 INRIA, France Telecom
rlm@10 4 * All rights reserved.
rlm@10 5 *
rlm@10 6 * Redistribution and use in source and binary forms, with or without
rlm@10 7 * modification, are permitted provided that the following conditions
rlm@10 8 * are met:
rlm@10 9 * 1. Redistributions of source code must retain the above copyright
rlm@10 10 * notice, this list of conditions and the following disclaimer.
rlm@10 11 * 2. Redistributions in binary form must reproduce the above copyright
rlm@10 12 * notice, this list of conditions and the following disclaimer in the
rlm@10 13 * documentation and/or other materials provided with the distribution.
rlm@10 14 * 3. Neither the name of the copyright holders nor the names of its
rlm@10 15 * contributors may be used to endorse or promote products derived from
rlm@10 16 * this software without specific prior written permission.
rlm@10 17 *
rlm@10 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rlm@10 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rlm@10 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
rlm@10 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
rlm@10 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
rlm@10 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
rlm@10 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
rlm@10 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
rlm@10 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
rlm@10 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
rlm@10 28 * THE POSSIBILITY OF SUCH DAMAGE.
rlm@10 29 */
rlm@10 30 package clojure.asm;
rlm@10 31
rlm@10 32 /**
rlm@10 33 * An {@link FieldVisitor} that generates Java fields in bytecode form.
rlm@10 34 *
rlm@10 35 * @author Eric Bruneton
rlm@10 36 */
rlm@10 37 final class FieldWriter implements FieldVisitor{
rlm@10 38
rlm@10 39 /**
rlm@10 40 * Next field writer (see {@link ClassWriter#firstField firstField}).
rlm@10 41 */
rlm@10 42 FieldWriter next;
rlm@10 43
rlm@10 44 /**
rlm@10 45 * The class writer to which this field must be added.
rlm@10 46 */
rlm@10 47 private ClassWriter cw;
rlm@10 48
rlm@10 49 /**
rlm@10 50 * Access flags of this field.
rlm@10 51 */
rlm@10 52 private int access;
rlm@10 53
rlm@10 54 /**
rlm@10 55 * The index of the constant pool item that contains the name of this
rlm@10 56 * method.
rlm@10 57 */
rlm@10 58 private int name;
rlm@10 59
rlm@10 60 /**
rlm@10 61 * The index of the constant pool item that contains the descriptor of this
rlm@10 62 * field.
rlm@10 63 */
rlm@10 64 private int desc;
rlm@10 65
rlm@10 66 /**
rlm@10 67 * The index of the constant pool item that contains the signature of this
rlm@10 68 * field.
rlm@10 69 */
rlm@10 70 private int signature;
rlm@10 71
rlm@10 72 /**
rlm@10 73 * The index of the constant pool item that contains the constant value of
rlm@10 74 * this field.
rlm@10 75 */
rlm@10 76 private int value;
rlm@10 77
rlm@10 78 /**
rlm@10 79 * The runtime visible annotations of this field. May be <tt>null</tt>.
rlm@10 80 */
rlm@10 81 private AnnotationWriter anns;
rlm@10 82
rlm@10 83 /**
rlm@10 84 * The runtime invisible annotations of this field. May be <tt>null</tt>.
rlm@10 85 */
rlm@10 86 private AnnotationWriter ianns;
rlm@10 87
rlm@10 88 /**
rlm@10 89 * The non standard attributes of this field. May be <tt>null</tt>.
rlm@10 90 */
rlm@10 91 private Attribute attrs;
rlm@10 92
rlm@10 93 // ------------------------------------------------------------------------
rlm@10 94 // Constructor
rlm@10 95 // ------------------------------------------------------------------------
rlm@10 96
rlm@10 97 /**
rlm@10 98 * Constructs a new {@link FieldWriter}.
rlm@10 99 *
rlm@10 100 * @param cw the class writer to which this field must be added.
rlm@10 101 * @param access the field's access flags (see {@link Opcodes}).
rlm@10 102 * @param name the field's name.
rlm@10 103 * @param desc the field's descriptor (see {@link Type}).
rlm@10 104 * @param signature the field's signature. May be <tt>null</tt>.
rlm@10 105 * @param value the field's constant value. May be <tt>null</tt>.
rlm@10 106 */
rlm@10 107 protected FieldWriter(
rlm@10 108 final ClassWriter cw,
rlm@10 109 final int access,
rlm@10 110 final String name,
rlm@10 111 final String desc,
rlm@10 112 final String signature,
rlm@10 113 final Object value){
rlm@10 114 if(cw.firstField == null)
rlm@10 115 {
rlm@10 116 cw.firstField = this;
rlm@10 117 }
rlm@10 118 else
rlm@10 119 {
rlm@10 120 cw.lastField.next = this;
rlm@10 121 }
rlm@10 122 cw.lastField = this;
rlm@10 123 this.cw = cw;
rlm@10 124 this.access = access;
rlm@10 125 this.name = cw.newUTF8(name);
rlm@10 126 this.desc = cw.newUTF8(desc);
rlm@10 127 if(signature != null)
rlm@10 128 {
rlm@10 129 this.signature = cw.newUTF8(signature);
rlm@10 130 }
rlm@10 131 if(value != null)
rlm@10 132 {
rlm@10 133 this.value = cw.newConstItem(value).index;
rlm@10 134 }
rlm@10 135 }
rlm@10 136
rlm@10 137 // ------------------------------------------------------------------------
rlm@10 138 // Implementation of the FieldVisitor interface
rlm@10 139 // ------------------------------------------------------------------------
rlm@10 140
rlm@10 141 public AnnotationVisitor visitAnnotation(
rlm@10 142 final String desc,
rlm@10 143 final boolean visible){
rlm@10 144 ByteVector bv = new ByteVector();
rlm@10 145 // write type, and reserve space for values count
rlm@10 146 bv.putShort(cw.newUTF8(desc)).putShort(0);
rlm@10 147 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
rlm@10 148 if(visible)
rlm@10 149 {
rlm@10 150 aw.next = anns;
rlm@10 151 anns = aw;
rlm@10 152 }
rlm@10 153 else
rlm@10 154 {
rlm@10 155 aw.next = ianns;
rlm@10 156 ianns = aw;
rlm@10 157 }
rlm@10 158 return aw;
rlm@10 159 }
rlm@10 160
rlm@10 161 public void visitAttribute(final Attribute attr){
rlm@10 162 attr.next = attrs;
rlm@10 163 attrs = attr;
rlm@10 164 }
rlm@10 165
rlm@10 166 public void visitEnd(){
rlm@10 167 }
rlm@10 168
rlm@10 169 // ------------------------------------------------------------------------
rlm@10 170 // Utility methods
rlm@10 171 // ------------------------------------------------------------------------
rlm@10 172
rlm@10 173 /**
rlm@10 174 * Returns the size of this field.
rlm@10 175 *
rlm@10 176 * @return the size of this field.
rlm@10 177 */
rlm@10 178 int getSize(){
rlm@10 179 int size = 8;
rlm@10 180 if(value != 0)
rlm@10 181 {
rlm@10 182 cw.newUTF8("ConstantValue");
rlm@10 183 size += 8;
rlm@10 184 }
rlm@10 185 if((access & Opcodes.ACC_SYNTHETIC) != 0
rlm@10 186 && (cw.version & 0xffff) < Opcodes.V1_5)
rlm@10 187 {
rlm@10 188 cw.newUTF8("Synthetic");
rlm@10 189 size += 6;
rlm@10 190 }
rlm@10 191 if((access & Opcodes.ACC_DEPRECATED) != 0)
rlm@10 192 {
rlm@10 193 cw.newUTF8("Deprecated");
rlm@10 194 size += 6;
rlm@10 195 }
rlm@10 196 if(signature != 0)
rlm@10 197 {
rlm@10 198 cw.newUTF8("Signature");
rlm@10 199 size += 8;
rlm@10 200 }
rlm@10 201 if(anns != null)
rlm@10 202 {
rlm@10 203 cw.newUTF8("RuntimeVisibleAnnotations");
rlm@10 204 size += 8 + anns.getSize();
rlm@10 205 }
rlm@10 206 if(ianns != null)
rlm@10 207 {
rlm@10 208 cw.newUTF8("RuntimeInvisibleAnnotations");
rlm@10 209 size += 8 + ianns.getSize();
rlm@10 210 }
rlm@10 211 if(attrs != null)
rlm@10 212 {
rlm@10 213 size += attrs.getSize(cw, null, 0, -1, -1);
rlm@10 214 }
rlm@10 215 return size;
rlm@10 216 }
rlm@10 217
rlm@10 218 /**
rlm@10 219 * Puts the content of this field into the given byte vector.
rlm@10 220 *
rlm@10 221 * @param out where the content of this field must be put.
rlm@10 222 */
rlm@10 223 void put(final ByteVector out){
rlm@10 224 out.putShort(access).putShort(name).putShort(desc);
rlm@10 225 int attributeCount = 0;
rlm@10 226 if(value != 0)
rlm@10 227 {
rlm@10 228 ++attributeCount;
rlm@10 229 }
rlm@10 230 if((access & Opcodes.ACC_SYNTHETIC) != 0
rlm@10 231 && (cw.version & 0xffff) < Opcodes.V1_5)
rlm@10 232 {
rlm@10 233 ++attributeCount;
rlm@10 234 }
rlm@10 235 if((access & Opcodes.ACC_DEPRECATED) != 0)
rlm@10 236 {
rlm@10 237 ++attributeCount;
rlm@10 238 }
rlm@10 239 if(signature != 0)
rlm@10 240 {
rlm@10 241 ++attributeCount;
rlm@10 242 }
rlm@10 243 if(anns != null)
rlm@10 244 {
rlm@10 245 ++attributeCount;
rlm@10 246 }
rlm@10 247 if(ianns != null)
rlm@10 248 {
rlm@10 249 ++attributeCount;
rlm@10 250 }
rlm@10 251 if(attrs != null)
rlm@10 252 {
rlm@10 253 attributeCount += attrs.getCount();
rlm@10 254 }
rlm@10 255 out.putShort(attributeCount);
rlm@10 256 if(value != 0)
rlm@10 257 {
rlm@10 258 out.putShort(cw.newUTF8("ConstantValue"));
rlm@10 259 out.putInt(2).putShort(value);
rlm@10 260 }
rlm@10 261 if((access & Opcodes.ACC_SYNTHETIC) != 0
rlm@10 262 && (cw.version & 0xffff) < Opcodes.V1_5)
rlm@10 263 {
rlm@10 264 out.putShort(cw.newUTF8("Synthetic")).putInt(0);
rlm@10 265 }
rlm@10 266 if((access & Opcodes.ACC_DEPRECATED) != 0)
rlm@10 267 {
rlm@10 268 out.putShort(cw.newUTF8("Deprecated")).putInt(0);
rlm@10 269 }
rlm@10 270 if(signature != 0)
rlm@10 271 {
rlm@10 272 out.putShort(cw.newUTF8("Signature"));
rlm@10 273 out.putInt(2).putShort(signature);
rlm@10 274 }
rlm@10 275 if(anns != null)
rlm@10 276 {
rlm@10 277 out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
rlm@10 278 anns.put(out);
rlm@10 279 }
rlm@10 280 if(ianns != null)
rlm@10 281 {
rlm@10 282 out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
rlm@10 283 ianns.put(out);
rlm@10 284 }
rlm@10 285 if(attrs != null)
rlm@10 286 {
rlm@10 287 attrs.put(cw, null, 0, -1, -1, out);
rlm@10 288 }
rlm@10 289 }
rlm@10 290 }