Blame contrib/asm/src/org/objectweb/asm/FieldWriter.java

Packit 5e46da
/***
Packit 5e46da
 * ASM: a very small and fast Java bytecode manipulation framework
Packit 5e46da
 * Copyright (c) 2000-2011 INRIA, France Telecom
Packit 5e46da
 * All rights reserved.
Packit 5e46da
 *
Packit 5e46da
 * Redistribution and use in source and binary forms, with or without
Packit 5e46da
 * modification, are permitted provided that the following conditions
Packit 5e46da
 * are met:
Packit 5e46da
 * 1. Redistributions of source code must retain the above copyright
Packit 5e46da
 *    notice, this list of conditions and the following disclaimer.
Packit 5e46da
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 5e46da
 *    notice, this list of conditions and the following disclaimer in the
Packit 5e46da
 *    documentation and/or other materials provided with the distribution.
Packit 5e46da
 * 3. Neither the name of the copyright holders nor the names of its
Packit 5e46da
 *    contributors may be used to endorse or promote products derived from
Packit 5e46da
 *    this software without specific prior written permission.
Packit 5e46da
 *
Packit 5e46da
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 5e46da
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 5e46da
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 5e46da
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit 5e46da
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 5e46da
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 5e46da
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 5e46da
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 5e46da
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 5e46da
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
Packit 5e46da
 * THE POSSIBILITY OF SUCH DAMAGE.
Packit 5e46da
 */
Packit 5e46da
package org.objectweb.asm;
Packit 5e46da
Packit 5e46da
/**
Packit 5e46da
 * An {@link FieldVisitor} that generates Java fields in bytecode form.
Packit 5e46da
 * 
Packit 5e46da
 * @author Eric Bruneton
Packit 5e46da
 */
Packit 5e46da
final class FieldWriter extends FieldVisitor {
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The class writer to which this field must be added.
Packit 5e46da
     */
Packit 5e46da
    private final ClassWriter cw;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * Access flags of this field.
Packit 5e46da
     */
Packit 5e46da
    private final int access;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The index of the constant pool item that contains the name of this
Packit 5e46da
     * method.
Packit 5e46da
     */
Packit 5e46da
    private final int name;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The index of the constant pool item that contains the descriptor of this
Packit 5e46da
     * field.
Packit 5e46da
     */
Packit 5e46da
    private final int desc;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The index of the constant pool item that contains the signature of this
Packit 5e46da
     * field.
Packit 5e46da
     */
Packit 5e46da
    private int signature;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The index of the constant pool item that contains the constant value of
Packit 5e46da
     * this field.
Packit 5e46da
     */
Packit 5e46da
    private int value;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The runtime visible annotations of this field. May be <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    private AnnotationWriter anns;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The runtime invisible annotations of this field. May be <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    private AnnotationWriter ianns;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The runtime visible type annotations of this field. May be <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    private AnnotationWriter tanns;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The runtime invisible type annotations of this field. May be
Packit 5e46da
     * <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    private AnnotationWriter itanns;
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * The non standard attributes of this field. May be <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    private Attribute attrs;
Packit 5e46da
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
    // Constructor
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * Constructs a new {@link FieldWriter}.
Packit 5e46da
     * 
Packit 5e46da
     * @param cw
Packit 5e46da
     *            the class writer to which this field must be added.
Packit 5e46da
     * @param access
Packit 5e46da
     *            the field's access flags (see {@link Opcodes}).
Packit 5e46da
     * @param name
Packit 5e46da
     *            the field's name.
Packit 5e46da
     * @param desc
Packit 5e46da
     *            the field's descriptor (see {@link Type}).
Packit 5e46da
     * @param signature
Packit 5e46da
     *            the field's signature. May be <tt>null</tt>.
Packit 5e46da
     * @param value
Packit 5e46da
     *            the field's constant value. May be <tt>null</tt>.
Packit 5e46da
     */
Packit 5e46da
    FieldWriter(final ClassWriter cw, final int access, final String name,
Packit 5e46da
            final String desc, final String signature, final Object value) {
Packit 5e46da
        super(Opcodes.ASM5);
Packit 5e46da
        if (cw.firstField == null) {
Packit 5e46da
            cw.firstField = this;
Packit 5e46da
        } else {
Packit 5e46da
            cw.lastField.fv = this;
Packit 5e46da
        }
Packit 5e46da
        cw.lastField = this;
Packit 5e46da
        this.cw = cw;
Packit 5e46da
        this.access = access;
Packit 5e46da
        this.name = cw.newUTF8(name);
Packit 5e46da
        this.desc = cw.newUTF8(desc);
Packit 5e46da
        if (ClassReader.SIGNATURES && signature != null) {
Packit 5e46da
            this.signature = cw.newUTF8(signature);
Packit 5e46da
        }
Packit 5e46da
        if (value != null) {
Packit 5e46da
            this.value = cw.newConstItem(value).index;
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
    // Implementation of the FieldVisitor abstract class
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
Packit 5e46da
    @Override
Packit 5e46da
    public AnnotationVisitor visitAnnotation(final String desc,
Packit 5e46da
            final boolean visible) {
Packit 5e46da
        if (!ClassReader.ANNOTATIONS) {
Packit 5e46da
            return null;
Packit 5e46da
        }
Packit 5e46da
        ByteVector bv = new ByteVector();
Packit 5e46da
        // write type, and reserve space for values count
Packit 5e46da
        bv.putShort(cw.newUTF8(desc)).putShort(0);
Packit 5e46da
        AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
Packit 5e46da
        if (visible) {
Packit 5e46da
            aw.next = anns;
Packit 5e46da
            anns = aw;
Packit 5e46da
        } else {
Packit 5e46da
            aw.next = ianns;
Packit 5e46da
            ianns = aw;
Packit 5e46da
        }
Packit 5e46da
        return aw;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    @Override
Packit 5e46da
    public AnnotationVisitor visitTypeAnnotation(final int typeRef,
Packit 5e46da
            final TypePath typePath, final String desc, final boolean visible) {
Packit 5e46da
        if (!ClassReader.ANNOTATIONS) {
Packit 5e46da
            return null;
Packit 5e46da
        }
Packit 5e46da
        ByteVector bv = new ByteVector();
Packit 5e46da
        // write target_type and target_info
Packit 5e46da
        AnnotationWriter.putTarget(typeRef, typePath, bv);
Packit 5e46da
        // write type, and reserve space for values count
Packit 5e46da
        bv.putShort(cw.newUTF8(desc)).putShort(0);
Packit 5e46da
        AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv,
Packit 5e46da
                bv.length - 2);
Packit 5e46da
        if (visible) {
Packit 5e46da
            aw.next = tanns;
Packit 5e46da
            tanns = aw;
Packit 5e46da
        } else {
Packit 5e46da
            aw.next = itanns;
Packit 5e46da
            itanns = aw;
Packit 5e46da
        }
Packit 5e46da
        return aw;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    @Override
Packit 5e46da
    public void visitAttribute(final Attribute attr) {
Packit 5e46da
        attr.next = attrs;
Packit 5e46da
        attrs = attr;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    @Override
Packit 5e46da
    public void visitEnd() {
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
    // Utility methods
Packit 5e46da
    // ------------------------------------------------------------------------
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * Returns the size of this field.
Packit 5e46da
     * 
Packit 5e46da
     * @return the size of this field.
Packit 5e46da
     */
Packit 5e46da
    int getSize() {
Packit 5e46da
        int size = 8;
Packit 5e46da
        if (value != 0) {
Packit 5e46da
            cw.newUTF8("ConstantValue");
Packit 5e46da
            size += 8;
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
Packit 5e46da
            if ((cw.version & 0xFFFF) < Opcodes.V1_5
Packit 5e46da
                    || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
Packit 5e46da
                cw.newUTF8("Synthetic");
Packit 5e46da
                size += 6;
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_DEPRECATED) != 0) {
Packit 5e46da
            cw.newUTF8("Deprecated");
Packit 5e46da
            size += 6;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.SIGNATURES && signature != 0) {
Packit 5e46da
            cw.newUTF8("Signature");
Packit 5e46da
            size += 8;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && anns != null) {
Packit 5e46da
            cw.newUTF8("RuntimeVisibleAnnotations");
Packit 5e46da
            size += 8 + anns.getSize();
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && ianns != null) {
Packit 5e46da
            cw.newUTF8("RuntimeInvisibleAnnotations");
Packit 5e46da
            size += 8 + ianns.getSize();
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && tanns != null) {
Packit 5e46da
            cw.newUTF8("RuntimeVisibleTypeAnnotations");
Packit 5e46da
            size += 8 + tanns.getSize();
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && itanns != null) {
Packit 5e46da
            cw.newUTF8("RuntimeInvisibleTypeAnnotations");
Packit 5e46da
            size += 8 + itanns.getSize();
Packit 5e46da
        }
Packit 5e46da
        if (attrs != null) {
Packit 5e46da
            size += attrs.getSize(cw, null, 0, -1, -1);
Packit 5e46da
        }
Packit 5e46da
        return size;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    /**
Packit 5e46da
     * Puts the content of this field into the given byte vector.
Packit 5e46da
     * 
Packit 5e46da
     * @param out
Packit 5e46da
     *            where the content of this field must be put.
Packit 5e46da
     */
Packit 5e46da
    void put(final ByteVector out) {
Packit 5e46da
        final int FACTOR = ClassWriter.TO_ACC_SYNTHETIC;
Packit 5e46da
        int mask = Opcodes.ACC_DEPRECATED | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
Packit 5e46da
                | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / FACTOR);
Packit 5e46da
        out.putShort(access & ~mask).putShort(name).putShort(desc);
Packit 5e46da
        int attributeCount = 0;
Packit 5e46da
        if (value != 0) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
Packit 5e46da
            if ((cw.version & 0xFFFF) < Opcodes.V1_5
Packit 5e46da
                    || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
Packit 5e46da
                ++attributeCount;
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_DEPRECATED) != 0) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.SIGNATURES && signature != 0) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && anns != null) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && ianns != null) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && tanns != null) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && itanns != null) {
Packit 5e46da
            ++attributeCount;
Packit 5e46da
        }
Packit 5e46da
        if (attrs != null) {
Packit 5e46da
            attributeCount += attrs.getCount();
Packit 5e46da
        }
Packit 5e46da
        out.putShort(attributeCount);
Packit 5e46da
        if (value != 0) {
Packit 5e46da
            out.putShort(cw.newUTF8("ConstantValue"));
Packit 5e46da
            out.putInt(2).putShort(value);
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
Packit 5e46da
            if ((cw.version & 0xFFFF) < Opcodes.V1_5
Packit 5e46da
                    || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
Packit 5e46da
                out.putShort(cw.newUTF8("Synthetic")).putInt(0);
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
        if ((access & Opcodes.ACC_DEPRECATED) != 0) {
Packit 5e46da
            out.putShort(cw.newUTF8("Deprecated")).putInt(0);
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.SIGNATURES && signature != 0) {
Packit 5e46da
            out.putShort(cw.newUTF8("Signature"));
Packit 5e46da
            out.putInt(2).putShort(signature);
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && anns != null) {
Packit 5e46da
            out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
Packit 5e46da
            anns.put(out);
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && ianns != null) {
Packit 5e46da
            out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
Packit 5e46da
            ianns.put(out);
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && tanns != null) {
Packit 5e46da
            out.putShort(cw.newUTF8("RuntimeVisibleTypeAnnotations"));
Packit 5e46da
            tanns.put(out);
Packit 5e46da
        }
Packit 5e46da
        if (ClassReader.ANNOTATIONS && itanns != null) {
Packit 5e46da
            out.putShort(cw.newUTF8("RuntimeInvisibleTypeAnnotations"));
Packit 5e46da
            itanns.put(out);
Packit 5e46da
        }
Packit 5e46da
        if (attrs != null) {
Packit 5e46da
            attrs.put(cw, null, 0, -1, -1, out);
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
}