Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2007 INRIA, France Telecom All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package org.objectweb.asm;

An AnnotationVisitor that generates annotations in bytecode form.

Author(s):
Eric Bruneton
Eugene Kuleshov
 
 final class AnnotationWriter implements AnnotationVisitor {

    
The class writer to which this annotation must be added.
 
     private final ClassWriter cw;

    
The number of values in this annotation.
 
     private int size;

    
true if values are named, false otherwise. Annotation writers used for annotation default and annotation arrays use unnamed values.
 
     private final boolean named;

    
The annotation values in bytecode form. This byte vector only contains the values themselves, i.e. the number of values must be stored as a unsigned short just before these bytes.
 
     private final ByteVector bv;

    
The byte vector to be used to store the number of values of this annotation. See bv.
 
     private final ByteVector parent;

    
Where the number of values of this annotation must be stored in parent.
 
     private final int offset;

    
Next annotation writer. This field is used to store annotation lists.
 
     AnnotationWriter next;

    
Previous annotation writer. This field is used to store annotation lists.
 
 
     // ------------------------------------------------------------------------
     // Constructor
     // ------------------------------------------------------------------------
 
    
Constructs a new AnnotationWriter.

Parameters:
cw the class writer to which this annotation must be added.
named true if values are named, false otherwise.
bv where the annotation values must be stored.
parent where the number of annotation values must be stored.
offset where in parent the number of annotation values must be stored.
 
        final ClassWriter cw,
        final boolean named,
        final ByteVector bv,
        final ByteVector parent,
        final int offset)
    {
        this. = cw;
        this. = named;
        this. = bv;
        this. = parent;
        this. = offset;
    }
    // ------------------------------------------------------------------------
    // Implementation of the AnnotationVisitor interface
    // ------------------------------------------------------------------------
    public void visit(final String namefinal Object value) {
        ++;
        if () {
            .putShort(.newUTF8(name));
        }
        if (value instanceof String) {
            .put12('s'.newUTF8((Stringvalue));
        } else if (value instanceof Byte) {
            .put12('B'.newInteger(((Bytevalue).byteValue()).);
        } else if (value instanceof Boolean) {
            int v = ((Booleanvalue).booleanValue() ? 1 : 0;
            .put12('Z'.newInteger(v).);
        } else if (value instanceof Character) {
            .put12('C'.newInteger(((Charactervalue).charValue()).);
        } else if (value instanceof Short) {
            .put12('S'.newInteger(((Shortvalue).shortValue()).);
        } else if (value instanceof Type) {
            .put12('c'.newUTF8(((Typevalue).getDescriptor()));
        } else if (value instanceof byte[]) {
            byte[] v = (byte[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('B'.newInteger(v[i]).);
            }
        } else if (value instanceof boolean[]) {
            boolean[] v = (boolean[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('Z'.newInteger(v[i] ? 1 : 0).);
            }
        } else if (value instanceof short[]) {
            short[] v = (short[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('S'.newInteger(v[i]).);
            }
        } else if (value instanceof char[]) {
            char[] v = (char[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('C'.newInteger(v[i]).);
            }
        } else if (value instanceof int[]) {
            int[] v = (int[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('I'.newInteger(v[i]).);
            }
        } else if (value instanceof long[]) {
            long[] v = (long[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('J'.newLong(v[i]).);
            }
        } else if (value instanceof float[]) {
            float[] v = (float[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('F'.newFloat(v[i]).);
            }
        } else if (value instanceof double[]) {
            double[] v = (double[]) value;
            .put12('['v.length);
            for (int i = 0; i < v.lengthi++) {
                .put12('D'.newDouble(v[i]).);
            }
        } else {
            Item i = .newConstItem(value);
            .put12(".s.IFJDCS".charAt(i.type), i.index);
        }
    }
    public void visitEnum(
        final String name,
        final String desc,
        final String value)
    {
        ++;
        if () {
            .putShort(.newUTF8(name));
        }
        .put12('e'.newUTF8(desc)).putShort(.newUTF8(value));
    }
        final String name,
        final String desc)
    {
        ++;
        if () {
            .putShort(.newUTF8(name));
        }
        // write tag and type, and reserve space for values count
        .put12('@'.newUTF8(desc)).putShort(0);
        return new AnnotationWriter(true. - 2);
    }
    public AnnotationVisitor visitArray(final String name) {
        ++;
        if () {
            .putShort(.newUTF8(name));
        }
        // write tag, and reserve space for array size
        .put12('[', 0);
        return new AnnotationWriter(false. - 2);
    }
    public void visitEnd() {
        if ( != null) {
            byte[] data = .;
            data[] = (byte) ( >>> 8);
            data[ + 1] = (byte;
        }
    }
    // ------------------------------------------------------------------------
    // Utility methods
    // ------------------------------------------------------------------------

    
Returns the size of this annotation writer list.

Returns:
the size of this annotation writer list.
    int getSize() {
        int size = 0;
        AnnotationWriter aw = this;
        while (aw != null) {
            size += aw.bv.length;
            aw = aw.next;
        }
        return size;
    }

    
Puts the annotations of this annotation writer list into the given byte vector.

Parameters:
out where the annotations must be put.
    void put(final ByteVector out) {
        int n = 0;
        int size = 2;
        AnnotationWriter aw = this;
        AnnotationWriter last = null;
        while (aw != null) {
            ++n;
            size += aw.bv.length;
            aw.visitEnd(); // in case user forgot to call visitEnd
            aw.prev = last;
            last = aw;
            aw = aw.next;
        }
        out.putInt(size);
        out.putShort(n);
        aw = last;
        while (aw != null) {
            out.putByteArray(aw.bv.data, 0, aw.bv.length);
            aw = aw.prev;
        }
    }

    
Puts the given annotation lists into the given byte vector.

Parameters:
panns an array of annotation writer lists.
off index of the first annotation to be written.
out where the annotations must be put.
    static void put(
        final AnnotationWriter[] panns,
        final int off,
        final ByteVector out)
    {
        int size = 1 + 2 * (panns.length - off);
        for (int i = offi < panns.length; ++i) {
            size += panns[i] == null ? 0 : panns[i].getSize();
        }
        out.putInt(size).putByte(panns.length - off);
        for (int i = offi < panns.length; ++i) {
            AnnotationWriter aw = panns[i];
            AnnotationWriter last = null;
            int n = 0;
            while (aw != null) {
                ++n;
                aw.visitEnd(); // in case user forgot to call visitEnd
                aw.prev = last;
                last = aw;
                aw = aw.next;
            }
            out.putShort(n);
            aw = last;
            while (aw != null) {
                out.putByteArray(aw.bv.data, 0, aw.bv.length);
                aw = aw.prev;
            }
        }
    }
New to GrepCode? Check out our FAQ X