KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Representation of an attribute.
34  *
35  * @author Mark Welsh
36  */

37 public class CodeAttrInfo extends AttrInfo
38 {
39     // Constants -------------------------------------------------------------
40
public static final int CONSTANT_FIELD_SIZE = 12;
41
42
43     // Fields ----------------------------------------------------------------
44
private int u2maxStack;
45     private int u2maxLocals;
46     private int u4codeLength;
47     private byte[] code;
48     private int u2exceptionTableLength;
49     private ExceptionInfo[] exceptionTable;
50     protected int u2attributesCount;
51     protected AttrInfo[] attributes;
52
53
54     // Class Methods ---------------------------------------------------------
55

56
57     // Instance Methods ------------------------------------------------------
58
protected CodeAttrInfo(ClassFile cf, int attrNameIndex, int attrLength)
59     {
60         super(cf, attrNameIndex, attrLength);
61     }
62
63     /** Return the length in bytes of the attribute. */
64     protected int getAttrInfoLength()
65     {
66         int length = CONSTANT_FIELD_SIZE + u4codeLength +
67                         u2exceptionTableLength * ExceptionInfo.CONSTANT_FIELD_SIZE;
68         for (int i = 0; i < u2attributesCount; i++)
69         {
70             length += AttrInfo.CONSTANT_FIELD_SIZE + attributes[i].getAttrInfoLength();
71         }
72         return length;
73     }
74
75     /** Return the String name of the attribute; over-ride this in sub-classes. */
76     protected String JavaDoc getAttrName()
77     {
78         return ATTR_Code;
79     }
80
81     /**
82      * Trim attributes from the classfile ('Code', 'Exceptions', 'ConstantValue'
83      * are preserved, all others except the list in the String[] are killed).
84      */

85     protected void trimAttrsExcept(String JavaDoc[] keepAttrs)
86     {
87         // Traverse all attributes, removing all except those on 'keep' list
88
for (int i = 0; i < attributes.length; i++)
89         {
90             if (Tools.isInArray(attributes[i].getAttrName(), keepAttrs))
91             {
92                 attributes[i].trimAttrsExcept(keepAttrs);
93             }
94             else
95             {
96                 attributes[i] = null;
97             }
98         }
99
100         // Delete the marked attributes
101
AttrInfo[] left = new AttrInfo[attributes.length];
102         int j = 0;
103         for (int i = 0; i < attributes.length; i++)
104         {
105             if (attributes[i] != null)
106             {
107                 left[j++] = attributes[i];
108             }
109         }
110         attributes = new AttrInfo[j];
111         System.arraycopy(left, 0, attributes, 0, j);
112         u2attributesCount = j;
113     }
114
115     /** Check for references in the 'info' data to the constant pool and mark them. */
116     protected void markUtf8RefsInInfo(ConstantPool pool)
117     {
118         for (int i = 0; i < attributes.length; i++)
119         {
120             attributes[i].markUtf8Refs(pool);
121         }
122     }
123
124     /** Read the data following the header. */
125     protected void readInfo(DataInput din) throws java.io.IOException JavaDoc
126     {
127         u2maxStack = din.readUnsignedShort();
128         u2maxLocals = din.readUnsignedShort();
129         u4codeLength = din.readInt();
130         code = new byte[u4codeLength];
131         din.readFully(code);
132         u2exceptionTableLength = din.readUnsignedShort();
133         exceptionTable = new ExceptionInfo[u2exceptionTableLength];
134         for (int i = 0; i < u2exceptionTableLength; i++)
135         {
136             exceptionTable[i] = ExceptionInfo.create(din);
137         }
138         u2attributesCount = din.readUnsignedShort();
139         attributes = new AttrInfo[u2attributesCount];
140         for (int i = 0; i < u2attributesCount; i++)
141         {
142             attributes[i] = AttrInfo.create(din, owner);
143         }
144     }
145
146     /** Export data following the header to a DataOutput stream. */
147     public void writeInfo(DataOutput dout) throws java.io.IOException JavaDoc
148     {
149         dout.writeShort(u2maxStack);
150         dout.writeShort(u2maxLocals);
151         dout.writeInt(u4codeLength);
152         dout.write(code);
153         dout.writeShort(u2exceptionTableLength);
154         for (int i = 0; i < u2exceptionTableLength; i++)
155         {
156             exceptionTable[i].write(dout);
157         }
158         dout.writeShort(u2attributesCount);
159         for (int i = 0; i < u2attributesCount; i++)
160         {
161             attributes[i].write(dout);
162         }
163     }
164 }
165
166
Popular Tags