KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > Attribute


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.io.*;
56
57 /******************************************************************************
58  * This class corresponds to the attribute_info structure defined in section
59  * 4.7 of <i>The Java Virtual Machine Specification</i>.
60  *
61  * @author Brian S O'Neill
62  * @version
63  * <!--$$Revision:--> 25 <!-- $-->, <!--$$JustDate:--> 00/11/27 <!-- $-->
64  * @see ClassFile
65  */

66 public abstract class Attribute {
67     final static Attribute[] NO_ATTRIBUTES = new Attribute[0];
68
69     final static String JavaDoc CODE = "Code";
70     final static String JavaDoc CONSTANT_VALUE = "ConstantValue";
71     final static String JavaDoc DEPRECATED = "Deprecated";
72     final static String JavaDoc EXCEPTIONS = "Exceptions";
73     final static String JavaDoc INNER_CLASSES = "InnerClasses";
74     final static String JavaDoc LINE_NUMBER_TABLE = "LineNumberTable";
75     final static String JavaDoc LOCAL_VARIABLE_TABLE = "LocalVariableTable";
76     final static String JavaDoc SOURCE_FILE = "SourceFile";
77     final static String JavaDoc SYNTHETIC = "Synthetic";
78
79     /** The ConstantPool that this attribute is defined against. */
80     protected final ConstantPool mCp;
81
82     private String JavaDoc mName;
83     private ConstantUTFInfo mNameConstant;
84     
85     protected Attribute(ConstantPool cp, String JavaDoc name) {
86         mCp = cp;
87         mName = name;
88         mNameConstant = ConstantUTFInfo.make(cp, name);
89     }
90
91     /**
92      * Returns the ConstantPool that this attribute is defined against.
93      */

94     public ConstantPool getConstantPool() {
95         return mCp;
96     }
97  
98     /**
99      * Returns the name of this attribute.
100      */

101     public String JavaDoc getName() {
102         return mName;
103     }
104     
105     public ConstantUTFInfo getNameConstant() {
106         return mNameConstant;
107     }
108     
109     /**
110      * Some attributes have sub-attributes. Default implementation returns an
111      * empty array.
112      */

113     public Attribute[] getAttributes() {
114         return NO_ATTRIBUTES;
115     }
116
117     /**
118      * Returns the length (in bytes) of this attribute in the class file.
119      */

120     public abstract int getLength();
121     
122     /**
123      * This method writes the 16 bit name constant index followed by the
124      * 32 bit attribute length, followed by the attribute specific data.
125      */

126     public final void writeTo(DataOutput dout) throws IOException {
127         dout.writeShort(mNameConstant.getIndex());
128         dout.writeInt(getLength());
129         writeDataTo(dout);
130     }
131
132     /**
133      * Write just the attribute specific data. The default implementation
134      * writes nothing.
135      */

136     public void writeDataTo(DataOutput dout) throws IOException {
137     }
138
139     /**
140      * @param attrFactory optional factory for reading custom attributes
141      */

142     public static Attribute readFrom(ConstantPool cp,
143                                      DataInput din,
144                                      AttributeFactory attrFactory)
145         throws IOException
146     {
147         int index = din.readUnsignedShort();
148         String JavaDoc name = ((ConstantUTFInfo)cp.getConstant(index)).getValue();
149         int length = din.readInt();
150
151         attrFactory = new Factory(attrFactory);
152         return attrFactory.createAttribute(cp, name, length, din);
153     }
154
155     private static class Factory implements AttributeFactory {
156         private final AttributeFactory mAttrFactory;
157
158         public Factory(AttributeFactory attrFactory) {
159             mAttrFactory = attrFactory;
160         }
161
162         public Attribute createAttribute(ConstantPool cp,
163                                          String JavaDoc name,
164                                          final int length,
165                                          DataInput din) throws IOException {
166             if (name.equals(CODE)) {
167                 return CodeAttr.define(cp, name, length, din, mAttrFactory);
168             }
169             else if (name.equals(CONSTANT_VALUE)) {
170                 return ConstantValueAttr.define(cp, name, length, din);
171             }
172             else if (name.equals(DEPRECATED)) {
173                 return DeprecatedAttr.define(cp, name, length, din);
174             }
175             else if (name.equals(EXCEPTIONS)) {
176                 return ExceptionsAttr.define(cp, name, length, din);
177             }
178             else if (name.equals(INNER_CLASSES)) {
179                 return InnerClassesAttr.define(cp, name, length, din);
180             }
181             else if (name.equals(LINE_NUMBER_TABLE)) {
182                 return LineNumberTableAttr.define(cp, name, length, din);
183             }
184             else if (name.equals(LOCAL_VARIABLE_TABLE)) {
185                 return LocalVariableTableAttr.define
186                     (cp, name, length, din);
187             }
188             else if (name.equals(SOURCE_FILE)) {
189                 return SourceFileAttr.define(cp, name, length, din);
190             }
191             else if (name.equals(SYNTHETIC)) {
192                 return SyntheticAttr.define(cp, name, length, din);
193             }
194
195             if (mAttrFactory != null) {
196                 Attribute attr =
197                     mAttrFactory.createAttribute(cp, name, length, din);
198                 if (attr != null) {
199                     return attr;
200                 }
201             }
202
203             // Default case, return attribute that captures the data, but
204
// doesn't decode it.
205

206             final byte[] data = new byte[length];
207             din.readFully(data);
208             
209             return new Attribute(cp, name) {
210                 public int getLength() {
211                     return length;
212                 }
213                 
214                 public void writeDataTo(DataOutput dout) throws IOException {
215                     dout.write(data);
216                 }
217             };
218         }
219     }
220 }
221
Popular Tags