src/share/classes/jdk/internal/org/objectweb/asm/FieldWriter.java

Print this page




  93      */
  94     private int signature;
  95 
  96     /**
  97      * The index of the constant pool item that contains the constant value of
  98      * this field.
  99      */
 100     private int value;
 101 
 102     /**
 103      * The runtime visible annotations of this field. May be <tt>null</tt>.
 104      */
 105     private AnnotationWriter anns;
 106 
 107     /**
 108      * The runtime invisible annotations of this field. May be <tt>null</tt>.
 109      */
 110     private AnnotationWriter ianns;
 111 
 112     /**











 113      * The non standard attributes of this field. May be <tt>null</tt>.
 114      */
 115     private Attribute attrs;
 116 
 117     // ------------------------------------------------------------------------
 118     // Constructor
 119     // ------------------------------------------------------------------------
 120 
 121     /**
 122      * Constructs a new {@link FieldWriter}.
 123      *
 124      * @param cw the class writer to which this field must be added.
 125      * @param access the field's access flags (see {@link Opcodes}).
 126      * @param name the field's name.
 127      * @param desc the field's descriptor (see {@link Type}).
 128      * @param signature the field's signature. May be <tt>null</tt>.
 129      * @param value the field's constant value. May be <tt>null</tt>.






 130      */
 131     FieldWriter(
 132         final ClassWriter cw,
 133         final int access,
 134         final String name,
 135         final String desc,
 136         final String signature,
 137         final Object value)
 138     {
 139         super(Opcodes.ASM4);
 140         if (cw.firstField == null) {
 141             cw.firstField = this;
 142         } else {
 143             cw.lastField.fv = this;
 144         }
 145         cw.lastField = this;
 146         this.cw = cw;
 147         this.access = access;
 148         this.name = cw.newUTF8(name);
 149         this.desc = cw.newUTF8(desc);
 150         if (ClassReader.SIGNATURES && signature != null) {
 151             this.signature = cw.newUTF8(signature);
 152         }
 153         if (value != null) {
 154             this.value = cw.newConstItem(value).index;
 155         }
 156     }
 157 
 158     // ------------------------------------------------------------------------
 159     // Implementation of the FieldVisitor abstract class
 160     // ------------------------------------------------------------------------
 161 
 162     @Override
 163     public AnnotationVisitor visitAnnotation(
 164         final String desc,
 165         final boolean visible)
 166     {
 167         if (!ClassReader.ANNOTATIONS) {
 168             return null;
 169         }
 170         ByteVector bv = new ByteVector();
 171         // write type, and reserve space for values count
 172         bv.putShort(cw.newUTF8(desc)).putShort(0);
 173         AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
 174         if (visible) {
 175             aw.next = anns;
 176             anns = aw;
 177         } else {
 178             aw.next = ianns;
 179             ianns = aw;
 180         }
 181         return aw;
 182     }
 183 
 184     @Override























 185     public void visitAttribute(final Attribute attr) {
 186         attr.next = attrs;
 187         attrs = attr;
 188     }
 189 
 190     @Override
 191     public void visitEnd() {
 192     }
 193 
 194     // ------------------------------------------------------------------------
 195     // Utility methods
 196     // ------------------------------------------------------------------------
 197 
 198     /**
 199      * Returns the size of this field.
 200      *
 201      * @return the size of this field.
 202      */
 203     int getSize() {
 204         int size = 8;
 205         if (value != 0) {
 206             cw.newUTF8("ConstantValue");
 207             size += 8;
 208         }
 209         if ((access & Opcodes.ACC_SYNTHETIC) != 0
 210                 && ((cw.version & 0xFFFF) < Opcodes.V1_5 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0))
 211         {
 212             cw.newUTF8("Synthetic");
 213             size += 6;
 214         }

 215         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 216             cw.newUTF8("Deprecated");
 217             size += 6;
 218         }
 219         if (ClassReader.SIGNATURES && signature != 0) {
 220             cw.newUTF8("Signature");
 221             size += 8;
 222         }
 223         if (ClassReader.ANNOTATIONS && anns != null) {
 224             cw.newUTF8("RuntimeVisibleAnnotations");
 225             size += 8 + anns.getSize();
 226         }
 227         if (ClassReader.ANNOTATIONS && ianns != null) {
 228             cw.newUTF8("RuntimeInvisibleAnnotations");
 229             size += 8 + ianns.getSize();
 230         }








 231         if (attrs != null) {
 232             size += attrs.getSize(cw, null, 0, -1, -1);
 233         }
 234         return size;
 235     }
 236 
 237     /**
 238      * Puts the content of this field into the given byte vector.
 239      *
 240      * @param out where the content of this field must be put.

 241      */
 242     void put(final ByteVector out) {
 243         int mask = Opcodes.ACC_DEPRECATED
 244                 | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
 245                 | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / (ClassWriter.ACC_SYNTHETIC_ATTRIBUTE / Opcodes.ACC_SYNTHETIC));
 246         out.putShort(access & ~mask).putShort(name).putShort(desc);
 247         int attributeCount = 0;
 248         if (value != 0) {
 249             ++attributeCount;
 250         }
 251         if ((access & Opcodes.ACC_SYNTHETIC) != 0
 252                 && ((cw.version & 0xFFFF) < Opcodes.V1_5 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0))
 253         {
 254             ++attributeCount;
 255         }

 256         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 257             ++attributeCount;
 258         }
 259         if (ClassReader.SIGNATURES && signature != 0) {
 260             ++attributeCount;
 261         }
 262         if (ClassReader.ANNOTATIONS && anns != null) {
 263             ++attributeCount;
 264         }
 265         if (ClassReader.ANNOTATIONS && ianns != null) {
 266             ++attributeCount;
 267         }






 268         if (attrs != null) {
 269             attributeCount += attrs.getCount();
 270         }
 271         out.putShort(attributeCount);
 272         if (value != 0) {
 273             out.putShort(cw.newUTF8("ConstantValue"));
 274             out.putInt(2).putShort(value);
 275         }
 276         if ((access & Opcodes.ACC_SYNTHETIC) != 0
 277                 && ((cw.version & 0xFFFF) < Opcodes.V1_5 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0))
 278         {
 279             out.putShort(cw.newUTF8("Synthetic")).putInt(0);
 280         }

 281         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 282             out.putShort(cw.newUTF8("Deprecated")).putInt(0);
 283         }
 284         if (ClassReader.SIGNATURES && signature != 0) {
 285             out.putShort(cw.newUTF8("Signature"));
 286             out.putInt(2).putShort(signature);
 287         }
 288         if (ClassReader.ANNOTATIONS && anns != null) {
 289             out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
 290             anns.put(out);
 291         }
 292         if (ClassReader.ANNOTATIONS && ianns != null) {
 293             out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
 294             ianns.put(out);
 295         }








 296         if (attrs != null) {
 297             attrs.put(cw, null, 0, -1, -1, out);
 298         }
 299     }
 300 }


  93      */
  94     private int signature;
  95 
  96     /**
  97      * The index of the constant pool item that contains the constant value of
  98      * this field.
  99      */
 100     private int value;
 101 
 102     /**
 103      * The runtime visible annotations of this field. May be <tt>null</tt>.
 104      */
 105     private AnnotationWriter anns;
 106 
 107     /**
 108      * The runtime invisible annotations of this field. May be <tt>null</tt>.
 109      */
 110     private AnnotationWriter ianns;
 111 
 112     /**
 113      * The runtime visible type annotations of this field. May be <tt>null</tt>.
 114      */
 115     private AnnotationWriter tanns;
 116 
 117     /**
 118      * The runtime invisible type annotations of this field. May be
 119      * <tt>null</tt>.
 120      */
 121     private AnnotationWriter itanns;
 122 
 123     /**
 124      * The non standard attributes of this field. May be <tt>null</tt>.
 125      */
 126     private Attribute attrs;
 127 
 128     // ------------------------------------------------------------------------
 129     // Constructor
 130     // ------------------------------------------------------------------------
 131 
 132     /**
 133      * Constructs a new {@link FieldWriter}.
 134      *
 135      * @param cw
 136      *            the class writer to which this field must be added.
 137      * @param access
 138      *            the field's access flags (see {@link Opcodes}).
 139      * @param name
 140      *            the field's name.
 141      * @param desc
 142      *            the field's descriptor (see {@link Type}).
 143      * @param signature
 144      *            the field's signature. May be <tt>null</tt>.
 145      * @param value
 146      *            the field's constant value. May be <tt>null</tt>.
 147      */
 148     FieldWriter(final ClassWriter cw, final int access, final String name,
 149             final String desc, final String signature, final Object value) {
 150         super(Opcodes.ASM5);






 151         if (cw.firstField == null) {
 152             cw.firstField = this;
 153         } else {
 154             cw.lastField.fv = this;
 155         }
 156         cw.lastField = this;
 157         this.cw = cw;
 158         this.access = access;
 159         this.name = cw.newUTF8(name);
 160         this.desc = cw.newUTF8(desc);
 161         if (ClassReader.SIGNATURES && signature != null) {
 162             this.signature = cw.newUTF8(signature);
 163         }
 164         if (value != null) {
 165             this.value = cw.newConstItem(value).index;
 166         }
 167     }
 168 
 169     // ------------------------------------------------------------------------
 170     // Implementation of the FieldVisitor abstract class
 171     // ------------------------------------------------------------------------
 172 
 173     @Override
 174     public AnnotationVisitor visitAnnotation(final String desc,
 175             final boolean visible) {


 176         if (!ClassReader.ANNOTATIONS) {
 177             return null;
 178         }
 179         ByteVector bv = new ByteVector();
 180         // write type, and reserve space for values count
 181         bv.putShort(cw.newUTF8(desc)).putShort(0);
 182         AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
 183         if (visible) {
 184             aw.next = anns;
 185             anns = aw;
 186         } else {
 187             aw.next = ianns;
 188             ianns = aw;
 189         }
 190         return aw;
 191     }
 192 
 193     @Override
 194     public AnnotationVisitor visitTypeAnnotation(final int typeRef,
 195             final TypePath typePath, final String desc, final boolean visible) {
 196         if (!ClassReader.ANNOTATIONS) {
 197             return null;
 198         }
 199         ByteVector bv = new ByteVector();
 200         // write target_type and target_info
 201         AnnotationWriter.putTarget(typeRef, typePath, bv);
 202         // write type, and reserve space for values count
 203         bv.putShort(cw.newUTF8(desc)).putShort(0);
 204         AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv,
 205                 bv.length - 2);
 206         if (visible) {
 207             aw.next = tanns;
 208             tanns = aw;
 209         } else {
 210             aw.next = itanns;
 211             itanns = aw;
 212         }
 213         return aw;
 214     }
 215 
 216     @Override
 217     public void visitAttribute(final Attribute attr) {
 218         attr.next = attrs;
 219         attrs = attr;
 220     }
 221 
 222     @Override
 223     public void visitEnd() {
 224     }
 225 
 226     // ------------------------------------------------------------------------
 227     // Utility methods
 228     // ------------------------------------------------------------------------
 229 
 230     /**
 231      * Returns the size of this field.
 232      *
 233      * @return the size of this field.
 234      */
 235     int getSize() {
 236         int size = 8;
 237         if (value != 0) {
 238             cw.newUTF8("ConstantValue");
 239             size += 8;
 240         }
 241         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
 242             if ((cw.version & 0xFFFF) < Opcodes.V1_5
 243                     || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
 244                 cw.newUTF8("Synthetic");
 245                 size += 6;
 246             }
 247         }
 248         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 249             cw.newUTF8("Deprecated");
 250             size += 6;
 251         }
 252         if (ClassReader.SIGNATURES && signature != 0) {
 253             cw.newUTF8("Signature");
 254             size += 8;
 255         }
 256         if (ClassReader.ANNOTATIONS && anns != null) {
 257             cw.newUTF8("RuntimeVisibleAnnotations");
 258             size += 8 + anns.getSize();
 259         }
 260         if (ClassReader.ANNOTATIONS && ianns != null) {
 261             cw.newUTF8("RuntimeInvisibleAnnotations");
 262             size += 8 + ianns.getSize();
 263         }
 264         if (ClassReader.ANNOTATIONS && tanns != null) {
 265             cw.newUTF8("RuntimeVisibleTypeAnnotations");
 266             size += 8 + tanns.getSize();
 267         }
 268         if (ClassReader.ANNOTATIONS && itanns != null) {
 269             cw.newUTF8("RuntimeInvisibleTypeAnnotations");
 270             size += 8 + itanns.getSize();
 271         }
 272         if (attrs != null) {
 273             size += attrs.getSize(cw, null, 0, -1, -1);
 274         }
 275         return size;
 276     }
 277 
 278     /**
 279      * Puts the content of this field into the given byte vector.
 280      *
 281      * @param out
 282      *            where the content of this field must be put.
 283      */
 284     void put(final ByteVector out) {
 285         final int FACTOR = ClassWriter.TO_ACC_SYNTHETIC;
 286         int mask = Opcodes.ACC_DEPRECATED | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
 287                 | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / FACTOR);
 288         out.putShort(access & ~mask).putShort(name).putShort(desc);
 289         int attributeCount = 0;
 290         if (value != 0) {
 291             ++attributeCount;
 292         }
 293         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
 294             if ((cw.version & 0xFFFF) < Opcodes.V1_5
 295                     || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
 296                 ++attributeCount;
 297             }
 298         }
 299         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 300             ++attributeCount;
 301         }
 302         if (ClassReader.SIGNATURES && signature != 0) {
 303             ++attributeCount;
 304         }
 305         if (ClassReader.ANNOTATIONS && anns != null) {
 306             ++attributeCount;
 307         }
 308         if (ClassReader.ANNOTATIONS && ianns != null) {
 309             ++attributeCount;
 310         }
 311         if (ClassReader.ANNOTATIONS && tanns != null) {
 312             ++attributeCount;
 313         }
 314         if (ClassReader.ANNOTATIONS && itanns != null) {
 315             ++attributeCount;
 316         }
 317         if (attrs != null) {
 318             attributeCount += attrs.getCount();
 319         }
 320         out.putShort(attributeCount);
 321         if (value != 0) {
 322             out.putShort(cw.newUTF8("ConstantValue"));
 323             out.putInt(2).putShort(value);
 324         }
 325         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
 326             if ((cw.version & 0xFFFF) < Opcodes.V1_5
 327                     || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
 328                 out.putShort(cw.newUTF8("Synthetic")).putInt(0);
 329             }
 330         }
 331         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
 332             out.putShort(cw.newUTF8("Deprecated")).putInt(0);
 333         }
 334         if (ClassReader.SIGNATURES && signature != 0) {
 335             out.putShort(cw.newUTF8("Signature"));
 336             out.putInt(2).putShort(signature);
 337         }
 338         if (ClassReader.ANNOTATIONS && anns != null) {
 339             out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
 340             anns.put(out);
 341         }
 342         if (ClassReader.ANNOTATIONS && ianns != null) {
 343             out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
 344             ianns.put(out);
 345         }
 346         if (ClassReader.ANNOTATIONS && tanns != null) {
 347             out.putShort(cw.newUTF8("RuntimeVisibleTypeAnnotations"));
 348             tanns.put(out);
 349         }
 350         if (ClassReader.ANNOTATIONS && itanns != null) {
 351             out.putShort(cw.newUTF8("RuntimeInvisibleTypeAnnotations"));
 352             itanns.put(out);
 353         }
 354         if (attrs != null) {
 355             attrs.put(cw, null, 0, -1, -1, out);
 356         }
 357     }
 358 }