001: /*
002: This library is free software; you can redistribute it and/or
003: modify it under the terms of the GNU General Public
004: License as published by the Free Software Foundation; either
005: version 2 of the license, or (at your option) any later version.
006: */
007: package org.gjt.jclasslib.structures.elementvalues;
008:
009: import org.gjt.jclasslib.structures.InvalidByteCodeException;
010:
011: import java.io.*;
012:
013: /**
014: * Describes an <tt>Annotation</tt> attribute structure.
015: *
016: * @author <a href="mailto:vitor.carreira@gmail.com">Vitor Carreira</a>
017: * @version $Revision: 1.1 $ $Date: 2004/12/28 13:04:32 $
018: */
019: public class AnnotationElementValue extends ElementValue {
020:
021: public final static String ENTRY_NAME = "Annotation";
022:
023: private static final int INITIAL_LENGTH = 4;
024:
025: private int typeIndex;
026: private ElementValuePair[] elementValuePairEntries;
027:
028: public AnnotationElementValue() {
029: super (ANNOTATION_TAG);
030: }
031:
032: public String getEntryName() {
033: return ENTRY_NAME;
034: }
035:
036: /**
037: * Get the list of element value pair associations of the parent
038: * structure as an array of <tt>ElementValuePair</tt> structures.
039: *
040: * @return the array
041: */
042: public ElementValuePair[] getElementValuePairEntries() {
043: return elementValuePairEntries;
044: }
045:
046: /**
047: * Set the list of element value pair associations of the parent
048: * structure as an array of <tt>ElementValuePair</tt> structures.
049: *
050: * @param elementValuePairEntries the array
051: */
052: public void setElementValuePairEntries(
053: ElementValuePair[] elementValuePairEntries) {
054: this .elementValuePairEntries = elementValuePairEntries;
055: }
056:
057: /**
058: * Get the <tt>type_index</tt> of this annotation.
059: *
060: * @return the <tt>type_index</tt>
061: */
062: public int getTypeIndex() {
063: return typeIndex;
064: }
065:
066: /**
067: * Set the <tt>type_index</tt> of this annotation.
068: *
069: * @param typeIndex the <tt>type_index</tt>
070: */
071: public void setTypeIndex(int typeIndex) {
072: this .typeIndex = typeIndex;
073: }
074:
075: public void read(DataInput in) throws InvalidByteCodeException,
076: IOException {
077: super .read(in);
078:
079: typeIndex = in.readUnsignedShort();
080: int elementValuePairEntriesLength = in.readUnsignedShort();
081:
082: elementValuePairEntries = new ElementValuePair[elementValuePairEntriesLength];
083:
084: for (int i = 0; i < elementValuePairEntriesLength; i++) {
085: elementValuePairEntries[i] = ElementValuePair.create(in,
086: classFile);
087: }
088:
089: if (debug)
090: debug("read ");
091: }
092:
093: public void write(DataOutput out) throws InvalidByteCodeException,
094: IOException {
095: super .write(out);
096:
097: out.writeShort(typeIndex);
098: int elementValuePairEntriesLength = getLength(elementValuePairEntries);
099:
100: out.writeShort(elementValuePairEntriesLength);
101: for (int i = 0; i < elementValuePairEntriesLength; i++) {
102: elementValuePairEntries[i].write(out);
103: }
104:
105: if (debug)
106: debug("wrote ");
107: }
108:
109: protected int getSpecificLength() {
110: int length = INITIAL_LENGTH;
111: for (int i = 0; i < elementValuePairEntries.length; i++) {
112: length += elementValuePairEntries[i].getLength();
113: }
114: return length;
115: }
116:
117: protected void debug(String message) {
118: super .debug(message + "Annotation with "
119: + getLength(elementValuePairEntries)
120: + " value pair elements");
121: }
122: }
|