KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > AbstractStructureWithAttributes


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
8 package org.gjt.jclasslib.structures;
9
10 import java.io.*;
11
12 /**
13     Base class for all structures with attributes.
14  
15     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
16     @version $Revision: 1.4 $ $Date: 2003/08/18 07:52:54 $
17 */

18 public abstract class AbstractStructureWithAttributes extends AbstractStructure {
19
20     /** Attributes of this structure. */
21     protected AttributeInfo[] attributes;
22
23     /**
24         Get the attributes of this structure.
25         @return the attributes
26      */

27     public AttributeInfo[] getAttributes() {
28         return attributes;
29     }
30     
31     /**
32         Set the attributes of this structure.
33         @param attributes the new attributes
34      */

35     public void setAttributes(AttributeInfo[] attributes) {
36         this.attributes = attributes;
37     }
38     
39     /**
40         Find an attribute of a certain class.
41         @param attributeClass the class of the attribute
42         @return the found attribute, <tt>null</tt> if not found
43      */

44     public AttributeInfo findAttribute(Class JavaDoc attributeClass) {
45         AttributeInfo foundAttribute = null;
46         for (int i = 0; i < attributes.length; i++) {
47             if (attributes[i].getClass() == attributeClass) {
48                 foundAttribute = attributes[i];
49                 break;
50             }
51         }
52         return foundAttribute;
53     }
54
55     /**
56         Read the attributes of this structure from the given <tt>DataInput</tt>. <p>
57      
58         Excpects <tt>DataInput</tt> to be in JVM class file format and just
59         before an attribute length field.
60         @param in the <tt>DataInput</tt> from which to read
61         @throws InvalidByteCodeException if the byte code is invalid
62         @throws IOException if an exception occurs with the <tt>DataInput</tt>
63      */

64     protected void readAttributes(DataInput in)
65         throws InvalidByteCodeException, IOException {
66             
67         int attributesCount = in.readUnsignedShort();
68         attributes = new AttributeInfo[attributesCount];
69         
70         for (int i = 0; i < attributesCount; i++) {
71             attributes[i] = AttributeInfo.createOrSkip(in, classFile);
72         }
73     }
74
75     /**
76         Write the attributes of this structure to the given <tt>DataOutput</tt>. <p>
77      
78         The written bytes are in JVM class file format.
79         @param out the <tt>DataOutput</tt> to which to write
80         @throws InvalidByteCodeException if the structure is internally inconsistent
81         @throws IOException if an exception occurs with the <tt>DataOutput</tt>
82      */

83     protected void writeAttributes(DataOutput out)
84         throws InvalidByteCodeException, IOException {
85             
86         int attributesCount = getLength(attributes);
87
88         out.writeShort(attributesCount);
89
90         for (int i = 0; i < attributesCount; i++) {
91             if (attributes[i] == null) {
92                 throw new InvalidByteCodeException("attribute " + i + " is null");
93             }
94             attributes[i].write(out);
95         }
96     }
97     
98     /**
99         Get the length of all attributes as a number of bytes.
100         @return the length
101      */

102     protected int getTotalAttributesLength() {
103         int totalLength = 0;
104         int attributesCount = getLength(attributes);
105         for (int i = 0; i < attributesCount; i++) {
106             if (attributes[i] != null) {
107                 totalLength += attributes[i].getAttributeLength();
108             }
109         }
110         return totalLength;
111     }
112 }
113
Popular Tags