KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > classfile > AttrInfo


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at theit@gmx.de.
22  *
23  *
24  * $Id: AttrInfo.java,v 1.2 2002/05/08 11:53:42 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of an attribute. Specific attributes have their representations
32  * sub-classed from this.
33  *
34  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
35  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
36  */

37 public abstract class AttrInfo implements ClassConstants {
38   /** The size of the constant field. */
39   public static final int CONSTANT_FIELD_SIZE = 6;
40   
41   /** Holds the name index, stored as a 2-byte array in the class file. */
42   private int attrNameIndex;
43   /** Holds the length of the attribute info data. */
44   private int attrInfoLength;
45   /** Holds the info data. */
46   private byte attrInfo[];
47   /** Holds the owner of the attribute. */
48   protected ClassFile owner;
49   
50   
51   
52   
53   /** Create a new AttrInfo from the data passed.
54    * @param din the input stream
55    * @param cf the class file the element belongs to
56    * @return the attribute info object created from the input stream
57    * @throws IOException if the class file is corrupt or incomplete
58    * @throws IllegalStateException if there is an inconsistent reference to the
59    * constant pool
60    */

61   public static AttrInfo create(DataInput din, ClassFile cf)
62   throws IOException, IllegalStateException JavaDoc {
63     if (din == null) throw new IOException("No input stream was provided.");
64     
65     // Instantiate based on attribute name
66
AttrInfo ai = null;
67     int attrNameIndex = din.readUnsignedShort();
68     int attrLength = din.readInt();
69     CpInfo cpInfo = cf.getCpEntry(attrNameIndex);
70     if (cpInfo instanceof Utf8CpInfo) {
71       String JavaDoc attrName = ((Utf8CpInfo)cpInfo).getString();
72       if (attrName.equals(ATTR_Code)) {
73         ai = new CodeAttrInfo(cf, attrNameIndex, attrLength);
74       }
75       else if (attrName.equals(ATTR_ConstantValue)) {
76         ai = new ConstantValueAttrInfo(cf, attrNameIndex, attrLength);
77       }
78       else if (attrName.equals(ATTR_Exceptions)) {
79         ai = new ExceptionsAttrInfo(cf, attrNameIndex, attrLength);
80       }
81       else if (attrName.equals(ATTR_LineNumberTable)) {
82         ai = new LineNumberTableAttrInfo(cf, attrNameIndex, attrLength);
83       }
84       else if (attrName.equals(ATTR_SourceFile)) {
85         ai = new SourceFileAttrInfo(cf, attrNameIndex, attrLength);
86       }
87       else if (attrName.equals(ATTR_LocalVariableTable)) {
88         ai = new LocalVariableTableAttrInfo(cf, attrNameIndex, attrLength);
89       }
90       else if (attrName.equals(ATTR_InnerClasses)) {
91         ai = new InnerClassesAttrInfo(cf, attrNameIndex, attrLength);
92       }
93       else if (attrName.equals(ATTR_Synthetic)) {
94         ai = new SyntheticAttrInfo(cf, attrNameIndex, attrLength);
95       }
96       else {
97         ai = new UnknownAttrInfo(cf, attrNameIndex, attrLength);
98       }
99     }
100     else {
101       throw new IllegalStateException JavaDoc("Inconsistent reference to Constant Pool.");
102     }
103     ai.readInfo(din);
104     return ai;
105   }
106   
107   
108   
109   
110   /** Default constructor that creates an AttrInfo object.
111    * @param cf the class file the object belongs to
112    * @param attrNameIndex index into the constant pool
113    * @param attrLength the length of the additional info data in the class file
114    */

115   protected AttrInfo(ClassFile cf, int attrNameIndex, int attrLength) {
116     owner = cf;
117     setAttrNameIndex(attrNameIndex);
118     setAttrInfoLength(attrLength);
119   }
120   
121   
122   
123   
124   /** Set the length of the attribute info data.
125    * @param len the length of the data
126    * @see #getAttrInfoLength
127    */

128   protected void setAttrInfoLength(int len) {
129     attrInfoLength = len;
130   }
131   
132   
133   /** Return the length in bytes of the attribute info data. Should be
134    * overwritten in subclasses.
135    * @return length of the data
136    * @see #setAttrInfoLength
137    */

138   protected int getAttrInfoLength() {
139     return attrInfoLength;
140   }
141   
142   
143   
144   
145   /** Return the string name of the attribute.
146    * @return string name of the attribute
147    */

148   abstract protected String JavaDoc getAttrName();
149   
150   
151   
152   
153   /** Trim attributes from the classfile except those in the String[].
154    * @param keepAttrs the attributes to keep
155    */

156   protected void trimAttrsExcept(String JavaDoc[] keepAttrs) {
157   }
158   
159   
160   
161   
162   /** Check for Utf8 references to constant pool and mark them.
163    * @param pool the constant pool the element belongs to
164    */

165   protected void markUtf8Refs(ConstantPool pool) {
166     pool.incRefCount(getAttrNameIndex());
167     markUtf8RefsInInfo(pool);
168   }
169   
170   
171   
172   
173   /** Check for Utf8 references in the 'info' data to the constant pool and
174    * mark them. Should be overwritten in subclasses.
175    * @param pool the constant pool the element belongs to
176    */

177   protected void markUtf8RefsInInfo(ConstantPool pool) {
178   }
179   
180   
181   
182   
183   /** Sets the attribute name index.
184    * @param index index into the constant pool
185    * @see #getAttrNameIndex
186    */

187   protected void setAttrNameIndex(int index) {
188     this.attrNameIndex = index;
189   }
190   
191   
192   /** Returns the attribute name index
193    * @return index into the constant pool
194    * @see #setAttrNameIndex
195    */

196   protected int getAttrNameIndex() {
197     return attrNameIndex;
198   }
199   
200   
201   
202   
203   /** Sets the attribute info data.
204    * @param info byte array containing the attribute info data
205    * @see #getAttrInfo
206    */

207   protected void setAttrInfo(byte[] info) {
208     attrInfo = info;
209   }
210   
211   
212   /** Returns the attribute info data.
213    * @return attribute info data
214    * @see #setAttrInfo
215    */

216   protected byte[] getAttrInfo() {
217     return attrInfo;
218   }
219   
220   
221   
222   
223   /** Read the data following the header. Should be overwritten in subclasses.
224    * @param din the input stream
225    * @throws IOException if an I/O error occurs
226    */

227   protected void readInfo(DataInput din)
228   throws IOException {
229     byte[] info = new byte[getAttrInfoLength()];
230     din.readFully(info);
231     setAttrInfo(info);
232   }
233   
234   
235   /** Export the representation to a DataOutput stream.
236    * @param dout the output stream
237    * @throws IOException if an I/O error occurs
238    */

239   public final void write(DataOutput dout)
240   throws IOException {
241     if (dout == null) throw new IOException("No output stream was provided.");
242     dout.writeShort(getAttrNameIndex());
243     dout.writeInt(getAttrInfoLength());
244     writeInfo(dout);
245   }
246   
247   
248   /** Export data following the header to a DataOutput stream. Should be
249    * overwritten in subclasses.
250    * @param dout the output stream
251    * @throws IOException if an I/O error occurs
252    */

253   public void writeInfo(DataOutput dout)
254   throws IOException {
255     dout.write(getAttrInfo());
256   }
257   
258   
259   
260   
261   /** Dump the content of the entry to the specified file (used for debugging).
262    * @param pw the print writer
263    * @param cf the class file the element belongs to
264    */

265   abstract public void dump(PrintWriter pw, ClassFile cf);
266 }
267
Popular Tags