KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > AttributeVector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.*;
28 import java.util.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31
32 /**
33  * A list of attributes within a class file.
34  * These lists occur in several places within a class file
35  * - at class level
36  * - at method level
37  * - at field level
38  * - at attribute level
39  */

40
41 public class AttributeVector {
42
43   /* Vector of ClassAttribute */
44   private ClassAttribute attributes[] = null;
45
46   /**
47    * Returns the i'th attribute in the array
48    */

49   private ClassAttribute attrAt(int i) {
50     return attributes[i];
51   }
52
53   /**
54    * Construct an empty AttributeVector
55    */

56   public AttributeVector() { }
57
58   /**
59    * Add an element to the vector
60    */

61   public void addElement(ClassAttribute attr) {
62     if (attributes == null)
63       attributes = new ClassAttribute[1];
64     else {
65       ClassAttribute newAttributes[] = new ClassAttribute[attributes.length+1];
66       System.arraycopy(attributes, 0, newAttributes, 0, attributes.length);
67       attributes = newAttributes;
68     }
69     attributes[attributes.length-1] = attr;
70   }
71
72   public Enumeration JavaDoc elements() {
73     class AttributeVectorEnumeration implements Enumeration JavaDoc {
74       private ClassAttribute[] attributes;
75       private int current = 0;
76
77       AttributeVectorEnumeration(ClassAttribute attrs[]) {
78     attributes = attrs;
79       }
80
81       public boolean hasMoreElements() {
82     return attributes != null && current < attributes.length;
83       }
84       public Object JavaDoc nextElement() {
85     if (!hasMoreElements())
86       throw new NoSuchElementException JavaDoc();
87     return attributes[current++];
88       }
89     }
90
91     return new AttributeVectorEnumeration(attributes);
92   }
93
94   /**
95    * Look for an attribute of a specific name
96    */

97   public ClassAttribute findAttribute(String JavaDoc attrName) {
98     Enumeration JavaDoc e = elements();
99     while (e.hasMoreElements()) {
100       ClassAttribute attr = (ClassAttribute) e.nextElement();
101       if (attr.attrName().asString().equals(attrName))
102     return attr;
103     }
104     return null;
105   }
106
107   /**
108    * General attribute reader
109    */

110   static AttributeVector readAttributes(
111     DataInputStream data, ConstantPool constantPool)
112     throws IOException {
113     AttributeVector attribs = new AttributeVector();
114     int n_attrs = data.readUnsignedShort();
115     while (n_attrs-- > 0) {
116       attribs.addElement(ClassAttribute.read(data, constantPool));
117     }
118     return attribs;
119   }
120
121   /**
122    * ClassMethod attribute reader
123    */

124   static AttributeVector readAttributes(
125     DataInputStream data, CodeEnv codeEnv)
126     throws IOException {
127     AttributeVector attribs = new AttributeVector();
128     int n_attrs = data.readUnsignedShort();
129     while (n_attrs-- > 0) {
130       attribs.addElement(ClassAttribute.read(data, codeEnv));
131     }
132     return attribs;
133   }
134
135   /**
136    * Write the attributes to the output stream
137    */

138   void write(DataOutputStream out) throws IOException {
139     if (attributes == null) {
140       out.writeShort(0);
141     } else {
142       out.writeShort(attributes.length);
143       for (int i=0; i<attributes.length; i++)
144     attributes[i].write(out);
145     }
146   }
147
148   /**
149    * Print a description of the attributes
150    */

151   void print(PrintStream out, int indent) {
152     if (attributes != null) {
153       for (int i=0; i<attributes.length; i++)
154     attributes[i].print(out, indent);
155     }
156   }
157
158   /**
159    * Print a brief summary of the attributes
160    */

161   void summarize() {
162     System.out.println((attributes == null ? 0 : attributes.length) +
163                " attributes");//NOI18N
164
}
165
166 }
167
168
Popular Tags