KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > yworks > yguard > obf > classfile > ClassItemInfo


1 /**
2  * YGuard -- an obfuscation library for Java(TM) classfiles.
3  *
4  * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
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 yguard@yworks.com
22  *
23  * Java and all Java-based marks are trademarks or registered
24  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
25  */

26 package com.yworks.yguard.obf.classfile;
27
28 import java.io.*;
29 import java.util.*;
30 import com.yworks.yguard.obf.*;
31
32
33 /**
34  * Representation of a field or method from a class-file.
35  *
36  * @author Mark Welsh
37  */

38 abstract public class ClassItemInfo implements ClassConstants
39 {
40     // Constants -------------------------------------------------------------
41

42
43     // Fields ----------------------------------------------------------------
44
private int u2accessFlags;
45     private int u2nameIndex;
46     private int u2descriptorIndex;
47     protected int u2attributesCount;
48     protected AttrInfo attributes[];
49
50     private ClassFile cf;
51     private boolean isSynthetic = false;
52
53
54     // Class Methods ---------------------------------------------------------
55

56
57     // Instance Methods ------------------------------------------------------
58
protected ClassItemInfo(ClassFile cf) {this.cf = cf;}
59
60     /** Is the field or method 'Synthetic'? */
61     public boolean isSynthetic() {return isSynthetic;}
62
63     /** Return method/field name index into Constant Pool. */
64     protected int getNameIndex() {return u2nameIndex;}
65
66     /** Set the method/field name index. */
67     protected void setNameIndex(int index) {u2nameIndex = index;}
68
69     /** Return method/field descriptor index into Constant Pool. */
70     protected int getDescriptorIndex() {return u2descriptorIndex;}
71
72     /** Set the method/field descriptor index. */
73     protected void setDescriptorIndex(int index) {u2descriptorIndex = index;}
74
75     /** Return method/field string name. */
76     protected String JavaDoc getName()
77     {
78         return ((Utf8CpInfo)cf.getCpEntry(u2nameIndex)).getString();
79     }
80
81     /** Return descriptor string. */
82     protected String JavaDoc getDescriptor()
83     {
84         return ((Utf8CpInfo)cf.getCpEntry(u2descriptorIndex)).getString();
85     }
86
87     /** Return access flags. */
88     protected int getAccessFlags()
89     {
90         return u2accessFlags;
91     }
92
93     /**
94      * Trim attributes from the classfile ('Code', 'Exceptions', 'ConstantValue'
95      * are preserved, all others except the list in the String[] are killed).
96      */

97     protected void trimAttrsExcept(String JavaDoc[] keepAttrs)
98     {
99         // Traverse all attributes, removing all except those on 'keep' list
100
for (int i = 0; i < attributes.length; i++)
101         {
102             if (Tools.isInArray(attributes[i].getAttrName(), keepAttrs))
103             {
104                 attributes[i].trimAttrsExcept(keepAttrs);
105             }
106             else
107             {
108                 attributes[i] = null;
109             }
110         }
111
112         // Delete the marked attributes
113
AttrInfo[] left = new AttrInfo[attributes.length];
114         int j = 0;
115         for (int i = 0; i < attributes.length; i++)
116         {
117             if (attributes[i] != null)
118             {
119                 left[j++] = attributes[i];
120             }
121         }
122         attributes = new AttrInfo[j];
123         System.arraycopy(left, 0, attributes, 0, j);
124         u2attributesCount = j;
125     }
126
127     /** Check for Utf8 references to constant pool and mark them. */
128     protected void markUtf8Refs(ConstantPool pool)
129     {
130         pool.incRefCount(u2nameIndex);
131         pool.incRefCount(u2descriptorIndex);
132         for (int i = 0; i < attributes.length; i++)
133         {
134             attributes[i].markUtf8Refs(pool);
135         }
136     }
137
138     /** Import the field or method data to internal representation. */
139     protected void read(DataInput din) throws java.io.IOException JavaDoc
140     {
141         u2accessFlags = din.readUnsignedShort();
142         u2nameIndex = din.readUnsignedShort();
143         u2descriptorIndex = din.readUnsignedShort();
144         u2attributesCount = din.readUnsignedShort();
145         attributes = new AttrInfo[u2attributesCount];
146         for (int i = 0; i < u2attributesCount; i++)
147         {
148             attributes[i] = AttrInfo.create(din, cf);
149             if (attributes[i].getAttrName().equals(ATTR_Synthetic))
150             {
151                 isSynthetic = true;
152             }
153         }
154     }
155
156     /** Export the representation to a DataOutput stream. */
157     public void write(DataOutput dout) throws java.io.IOException JavaDoc
158     {
159         if (dout == null) throw new NullPointerException JavaDoc("No output stream was provided.");
160         dout.writeShort(u2accessFlags);
161         dout.writeShort(u2nameIndex);
162         dout.writeShort(u2descriptorIndex);
163         dout.writeShort(u2attributesCount);
164         for (int i = 0; i < u2attributesCount; i++)
165         {
166             attributes[i].write(dout);
167         }
168     }
169 }
170
Popular Tags