KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > elementvalues > AnnotationElementValue


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7 package org.gjt.jclasslib.structures.elementvalues;
8
9 import org.gjt.jclasslib.structures.InvalidByteCodeException;
10
11 import java.io.*;
12
13 /**
14  * Describes an <tt>Annotation</tt> attribute structure.
15  *
16  * @author <a HREF="mailto:vitor.carreira@gmail.com">Vitor Carreira</a>
17  * @version $Revision: 1.1 $ $Date: 2004/12/28 13:04:32 $
18  */

19 public class AnnotationElementValue extends ElementValue {
20
21     public final static String JavaDoc ENTRY_NAME = "Annotation";
22
23     private static final int INITIAL_LENGTH = 4;
24
25     private int typeIndex;
26     private ElementValuePair[] elementValuePairEntries;
27
28
29     public AnnotationElementValue() {
30         super(ANNOTATION_TAG);
31     }
32
33     public String JavaDoc getEntryName() {
34         return ENTRY_NAME;
35     }
36
37     /**
38      * Get the list of element value pair associations of the parent
39      * structure as an array of <tt>ElementValuePair</tt> structures.
40      *
41      * @return the array
42      */

43     public ElementValuePair[] getElementValuePairEntries() {
44         return elementValuePairEntries;
45     }
46
47     /**
48      * Set the list of element value pair associations of the parent
49      * structure as an array of <tt>ElementValuePair</tt> structures.
50      *
51      * @param elementValuePairEntries the array
52      */

53     public void setElementValuePairEntries(ElementValuePair[] elementValuePairEntries) {
54         this.elementValuePairEntries = elementValuePairEntries;
55     }
56
57     /**
58      * Get the <tt>type_index</tt> of this annotation.
59      *
60      * @return the <tt>type_index</tt>
61      */

62     public int getTypeIndex() {
63         return typeIndex;
64     }
65
66     /**
67      * Set the <tt>type_index</tt> of this annotation.
68      *
69      * @param typeIndex the <tt>type_index</tt>
70      */

71     public void setTypeIndex(int typeIndex) {
72         this.typeIndex = typeIndex;
73     }
74
75     public void read(DataInput in) throws InvalidByteCodeException, IOException {
76         super.read(in);
77
78         typeIndex = in.readUnsignedShort();
79         int elementValuePairEntriesLength = in.readUnsignedShort();
80
81         elementValuePairEntries = new ElementValuePair[elementValuePairEntriesLength];
82
83         for (int i = 0; i < elementValuePairEntriesLength; i++) {
84             elementValuePairEntries[i] = ElementValuePair.create(in, classFile);
85         }
86
87         if (debug) debug("read ");
88     }
89
90     public void write(DataOutput out) throws InvalidByteCodeException, IOException {
91         super.write(out);
92
93         out.writeShort(typeIndex);
94         int elementValuePairEntriesLength = getLength(elementValuePairEntries);
95
96         out.writeShort(elementValuePairEntriesLength);
97         for (int i = 0; i < elementValuePairEntriesLength; i++) {
98             elementValuePairEntries[i].write(out);
99         }
100
101         if (debug) debug("wrote ");
102     }
103
104     protected int getSpecificLength() {
105         int length = INITIAL_LENGTH;
106         for (int i = 0; i < elementValuePairEntries.length; i++) {
107             length += elementValuePairEntries[i].getLength();
108         }
109         return length;
110     }
111
112     protected void debug(String JavaDoc message) {
113         super.debug(message + "Annotation with " +
114                 getLength(elementValuePairEntries) + " value pair elements");
115     }
116 }
117
Popular Tags