KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.yworks.yguard.ParseException;
30 import com.yworks.yguard.Conversion;
31
32 /**
33  * Representation of an attribute. Specific attributes have their representations
34  * sub-classed from this.
35  *
36  * @author Mark Welsh
37  */

38 public class AttrInfo implements ClassConstants
39 {
40     // Constants -------------------------------------------------------------
41
public static final int CONSTANT_FIELD_SIZE = 6;
42
43
44     // Fields ----------------------------------------------------------------
45
private int u2attrNameIndex;
46     protected int u4attrLength;
47     private byte info[];
48
49     protected ClassFile owner;
50
51
52     // Class Methods ---------------------------------------------------------
53
/**
54      * Create a new AttrInfo from the data passed.
55      *
56      * @throws IOException if class file is corrupt or incomplete
57      */

58     public static AttrInfo create(DataInput din, ClassFile cf) throws java.io.IOException JavaDoc
59     {
60         if (din == null) throw new NullPointerException JavaDoc("No input stream was provided.");
61
62         // Instantiate based on attribute name
63
AttrInfo ai = null;
64         int attrNameIndex = din.readUnsignedShort();
65         int attrLength = din.readInt();
66 // byte[] buffer = new byte[attrLength];
67
// din.readFully(buffer);
68

69         CpInfo cpInfo = cf.getCpEntry(attrNameIndex);
70         if (cpInfo instanceof Utf8CpInfo)
71         {
72             String JavaDoc attrName = ((Utf8CpInfo)cpInfo).getString();
73             if (attrName.equals(ATTR_Code))
74             {
75                 ai = new CodeAttrInfo(cf, attrNameIndex, attrLength);
76             }
77             else if (attrName.equals(ATTR_ConstantValue))
78             {
79                 ai = new ConstantValueAttrInfo(cf, attrNameIndex, attrLength);
80             }
81             else if (attrName.equals(ATTR_Exceptions))
82             {
83                 ai = new ExceptionsAttrInfo(cf, attrNameIndex, attrLength);
84             }
85             else if (attrName.equals(ATTR_StackMapTable))
86             {
87                 ai = new StackMapTableAttrInfo(cf, attrNameIndex, attrLength);
88             }
89             else if (attrName.equals(ATTR_LineNumberTable))
90             {
91                 ai = new LineNumberTableAttrInfo(cf, attrNameIndex, attrLength);
92             }
93             else if (attrName.equals(ATTR_SourceFile))
94             {
95                 ai = new SourceFileAttrInfo(cf, attrNameIndex, attrLength);
96             }
97             else if (attrName.equals(ATTR_LocalVariableTable))
98             {
99                 ai = new LocalVariableTableAttrInfo(cf, attrNameIndex, attrLength);
100             }
101             else if (attrName.equals(ATTR_InnerClasses))
102             {
103                 ai = new InnerClassesAttrInfo(cf, attrNameIndex, attrLength);
104             }
105             else if (attrName.equals(ATTR_Synthetic))
106             {
107                 ai = new SyntheticAttrInfo(cf, attrNameIndex, attrLength);
108             }
109             else if (attrName.equals(ATTR_Deprecated))
110             {
111                 ai = new DeprecatedAttrInfo(cf, attrNameIndex, attrLength);
112             }
113             else if (attrName.equals(ATTR_Signature)){
114                 ai = new SignatureAttrInfo(cf, attrNameIndex, attrLength);
115             }
116             else if (attrName.equals(ATTR_LocalVariableTypeTable)){
117                 ai = new LocalVariableTypeTableAttrInfo(cf, attrNameIndex, attrLength);
118             }
119             else if (attrName.equals(ATTR_EnclosingMethod)){
120                 ai = new EnclosingMethodAttrInfo(cf, attrNameIndex, attrLength);
121             }
122             else if (attrName.equals(ATTR_RuntimeVisibleAnnotations)){
123                 ai = new RuntimeVisibleAnnotationsAttrInfo(cf, attrNameIndex, attrLength);
124             }
125             else if (attrName.equals(ATTR_RuntimeInvisibleAnnotations)){
126                 ai = new RuntimeInvisibleAnnotationsAttrInfo(cf, attrNameIndex, attrLength);
127             }
128             else if (attrName.equals(ATTR_RuntimeVisibleParameterAnnotations)){
129                 ai = new RuntimeVisibleParameterAnnotationsAttrInfo(cf, attrNameIndex, attrLength);
130             }
131             else if (attrName.equals(ATTR_RuntimeInvisibleParameterAnnotations)){
132                 ai = new RuntimeInvisibleParameterAnnotationsAttrInfo(cf, attrNameIndex, attrLength);
133             }
134             else if (attrName.equals(ATTR_AnnotationDefault)){
135                 ai = new AnnotationDefaultAttrInfo(cf, attrNameIndex, attrLength);
136             }
137             else if (attrName.equals(ATTR_Bridge) && (attrLength==0)){
138                 ai = new AttrInfo(cf, attrNameIndex, attrLength);
139             }
140             else if (attrName.equals(ATTR_Enum) && (attrLength==0)){
141                 ai = new AttrInfo(cf, attrNameIndex, attrLength);
142             }
143             else if (attrName.equals(ATTR_Varargs) && (attrLength==0)){
144                 ai = new AttrInfo(cf, attrNameIndex, attrLength);
145             }
146             else {
147               if ( attrLength > 0 ) {
148                 Logger.getInstance().warning( "Unrecognized attribute '" + attrName + "' in " + Conversion.toJavaClass( cf.getName() ) );
149               }
150               ai = new AttrInfo( cf, attrNameIndex, attrLength );
151             }
152         }
153         else
154         {
155             throw new ParseException("Inconsistent reference to Constant Pool.");
156         }
157
158 // ai.readInfo(new DataInputStream(new ByteArrayInputStream(buffer)));
159
ai.readInfo(din);
160         return ai;
161     }
162
163
164     // Instance Methods ------------------------------------------------------
165
protected AttrInfo(ClassFile cf, int attrNameIndex, int attrLength)
166     {
167         owner = cf;
168         u2attrNameIndex = attrNameIndex;
169         u4attrLength = attrLength;
170     }
171
172     protected int getAttrNameIndex(){
173       return u2attrNameIndex;
174     }
175
176     /** Return the length in bytes of the attribute; over-ride this in sub-classes. */
177     protected int getAttrInfoLength()
178     {
179         return u4attrLength;
180     }
181
182     /** Return the String name of the attribute; over-ride this in sub-classes. */
183     protected String JavaDoc getAttrName()
184     {
185         return ATTR_Unknown;
186     }
187
188     /**
189      * Trim attributes from the classfile except those in the String[].
190      */

191     protected void trimAttrsExcept(String JavaDoc[] keepAttrs) {}
192
193     /** Check for Utf8 references to constant pool and mark them. */
194     protected void markUtf8Refs(ConstantPool pool)
195     {
196         pool.incRefCount(u2attrNameIndex);
197         markUtf8RefsInInfo(pool);
198     }
199
200     /**
201      * Check for Utf8 references in the 'info' data to the constant pool and
202      * mark them; over-ride this in sub-classes.
203      */

204     protected void markUtf8RefsInInfo(ConstantPool pool) {}
205
206     /** Read the data following the header; over-ride this in sub-classes. */
207     protected void readInfo(DataInput din) throws java.io.IOException JavaDoc
208     {
209         info = new byte[u4attrLength];
210         din.readFully(info);
211     }
212
213     /** Export the representation to a DataOutput stream. */
214     public final void write(DataOutput dout) throws java.io.IOException JavaDoc
215     {
216         if (dout == null) throw new IOException("No output stream was provided.");
217         dout.writeShort(u2attrNameIndex);
218         dout.writeInt(getAttrInfoLength());
219         writeInfo(dout);
220     }
221
222     /** Export data following the header to a DataOutput stream; over-ride this in sub-classes. */
223     public void writeInfo(DataOutput dout) throws java.io.IOException JavaDoc
224     {
225         dout.write(info);
226     }
227
228     public String JavaDoc toString(){
229       return getAttrName() + "[" + getAttrInfoLength() + "]";
230     }
231 }
232
Popular Tags