KickJava   Java API By Example, From Geeks To Geeks.

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


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: ClassItemInfo.java,v 1.4 2002/05/11 22:35:52 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29 import net.sf.javaguard.Tools;
30
31
32 /** Representation of a field or method from a class-file.
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 abstract public class ClassItemInfo implements ClassConstants {
38   /** The access flags. */
39   private int accessFlags;
40   /** The index of the name in the constant pool. */
41   private int nameIndex;
42   /** The index of the descriptor in the constant pool. */
43   private int descriptorIndex;
44   /** The number of attributes. */
45   private int attributesLength;
46   /** Array that contains the attributes for the element. */
47   private AttrInfo attributesInfo[];
48   /** The class file the element belongs to. */
49   private ClassFile classFile;
50   /** Holds whether the element is synthetic or not. */
51   private boolean synthetic = false;
52   
53   
54   
55   
56   /** Creates a new ClassItemInfo object for the given class file.
57    * @param cf the class file
58    */

59   protected ClassItemInfo(ClassFile cf) {
60     setClassFile(cf);
61   }
62   
63   
64   
65   
66   /** Sets the class file the element belongs to.
67    * @param cf the class file the element belongs to
68    * @see #getClassFile
69    */

70   private void setClassFile(ClassFile cf) {
71     this.classFile = cf;
72   }
73   
74   
75   /** Returns the class file the element belongs to.
76    * @return the class file the element belongs to
77    * @see #setClassFile
78    */

79   private ClassFile getClassFile() {
80     return classFile;
81   }
82   
83   
84   
85   
86   /** Sets whether the field or method is marked as being 'synthetic'.
87    * @param isSynthetic true if the field or method is synthetic; false else
88    * @see #isSynthetic
89    */

90   protected void setSynthetic(boolean isSynthetic) {
91     this.synthetic = isSynthetic;
92   }
93   
94   
95   /** Returns whether the field or method is marked as being 'synthetic'.
96    * @return true if the element is synthetic; false else
97    */

98   public boolean isSynthetic() {
99     return synthetic;
100   }
101   
102   
103   
104   
105   /** Set the method/field name index.
106    * @param index the index into the constant pool
107    * @see #getNameIndex
108    */

109   protected void setNameIndex(int index) {
110     nameIndex = index;
111   }
112   
113   
114   /** Return method/field name index into the constant pool.
115    * @return index into the constant pool
116    * @see #setNameIndex
117    */

118   protected int getNameIndex() {
119     return nameIndex;
120   }
121   
122   
123   
124   
125   /** Set the method/field descriptor index.
126    * @param index the index into the constant pool
127    * @see #getDescriptorIndex
128    */

129   protected void setDescriptorIndex(int index) {
130     descriptorIndex = index;
131   }
132   
133   
134   /** Return method/field descriptor index into constant pool.
135    * @return index into the constant pool
136    * @see #setDescriptorIndex
137    */

138   protected int getDescriptorIndex() {
139     return descriptorIndex;
140   }
141   
142   
143   
144   
145   /** Sets the length of the attributes array.
146    * @param len the length of the attribute sarray
147    * @see #getAttributesLength
148    */

149   protected void setAttributesLength(int len) {
150     attributesLength = len;
151   }
152   
153   
154   /** Returns the length of the attributes array.
155    * @return the length of the attributes array
156    * @see #setAttributesLength
157    */

158   protected int getAttributesLength() {
159     return attributesLength;
160   }
161   
162   
163   
164   
165   /** Sets the attributes array for the element.
166    * @param attributes array with attributes
167    * @see #getAttributes
168    */

169   protected void setAttributes(AttrInfo[] attributes) {
170     attributesInfo = attributes;
171   }
172   
173   
174   /** Returns the attributes array for the element.
175    * @return array with attributes
176    * @see #setAttributes
177    */

178   protected AttrInfo[] getAttributes() {
179     return attributesInfo;
180   }
181   
182   
183   
184   
185   /** Replaces the attribute at the given index in the attribute array by a
186    * new one.
187    * @param index the index of the attribute in the attributes array
188    * @param attr the new attribute
189    * @see #getAttribute
190    * @see #setAttributes
191    */

192   protected void setAttribute(int index, AttrInfo attr) {
193     attributesInfo[index] = attr;
194   }
195   
196   
197   /** Returns the specified attribute from the attribute array.
198    * @param pos the position in the attributes array
199    * @return attribute from the attribute array
200    * @see #setAttribute
201    * @see #getAttributes
202    */

203   protected AttrInfo getAttribute(int pos) {
204     return attributesInfo[pos];
205   }
206   
207   
208   
209   
210   /** Return method/field string name.
211    * @return the method/field name as a string
212    */

213   public String JavaDoc getName() {
214     return ((Utf8CpInfo) getClassFile().getCpEntry(getNameIndex())).getString();
215   }
216   
217   
218   /** Return descriptor string.
219    * @return the descriptor name as a string
220    */

221   public String JavaDoc getDescriptor() {
222     return ((Utf8CpInfo) getClassFile().getCpEntry(getDescriptorIndex())).getString();
223   }
224   
225   
226   
227   
228   /** Sets the access flags.
229    * @param flags the access flags
230    * @see #getAccessFlags
231    */

232   protected void setAccessFlags(int flags) {
233     accessFlags = flags;
234   }
235   
236   
237   /** Returns the access flags.
238    * @return access flags
239    * @see #setAccessFlags
240    */

241   public int getAccessFlags() {
242     return accessFlags;
243   }
244   
245   
246   
247   
248   /** Trim attributes from the classfile ('Code', 'Exceptions', 'ConstantValue'
249    * are preserved, all others except the list in the String[] are killed).
250    * @param keepAttrs the attributes to keep
251    */

252   protected void trimAttrsExcept(String JavaDoc[] keepAttrs) {
253     // Traverse all attributes, removing all except those on 'keep' list
254
for (int i = 0; i < getAttributesLength(); i++) {
255       if (Tools.isInArray(getAttribute(i).getAttrName(), keepAttrs)) {
256         getAttribute(i).trimAttrsExcept(keepAttrs);
257       } else {
258         setAttribute(i, null);
259       }
260     }
261     
262     // Delete the marked attributes
263
AttrInfo[] left = new AttrInfo[getAttributesLength()];
264     int j = 0;
265     for (int i = 0; i < getAttributesLength(); i++) {
266       if (null != getAttribute(i)) {
267         left[j++] = getAttribute(i);
268       }
269     }
270     AttrInfo[] attributes = new AttrInfo[j];
271     System.arraycopy(left, 0, attributes, 0, j);
272     setAttributesLength(j);
273     setAttributes(attributes);
274   }
275   
276   
277   
278   
279   /** Check for Utf8 references to constant pool and mark them.
280    * @param pool the constant pool the element belongs to
281    */

282   protected void markUtf8Refs(ConstantPool pool) {
283     pool.incRefCount(getNameIndex());
284     pool.incRefCount(getDescriptorIndex());
285     for (int i = 0; i < getAttributesLength(); i++) {
286       getAttribute(i).markUtf8Refs(pool);
287     }
288   }
289   
290   
291   
292   
293   /** Import the field or method data to internal representation.
294    * @param din the input stream
295    * @throws IOException if an I/O error occurs
296    */

297   protected void read(DataInput din)
298   throws IOException {
299     setAccessFlags(din.readUnsignedShort());
300     setNameIndex(din.readUnsignedShort());
301     setDescriptorIndex(din.readUnsignedShort());
302     setAttributesLength(din.readUnsignedShort());
303     AttrInfo[] attrs = new AttrInfo[getAttributesLength()];
304     for (int i = 0; i < getAttributesLength(); i++) {
305       attrs[i] = AttrInfo.create(din, getClassFile());
306       if (attrs[i].getAttrName().equals(ATTR_Synthetic)) {
307         setSynthetic(true);
308       }
309     }
310     setAttributes(attrs);
311   }
312   
313   
314   /** Export the representation to an output stream.
315    * @param dout the output stream
316    * @throws IOException if an I/O error occurs
317    */

318   public void write(DataOutput dout)
319   throws IOException {
320     if (null == dout) throw new IOException("No output stream was provided.");
321     dout.writeShort(getAccessFlags());
322     dout.writeShort(getNameIndex());
323     dout.writeShort(getDescriptorIndex());
324     dout.writeShort(getAttributesLength());
325     for (int i = 0; i < getAttributesLength(); i++) {
326       getAttribute(i).write(dout);
327     }
328   }
329 }
330
Popular Tags