KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > bytecode > MethodInfo


1 /* MethodInfo Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: MethodInfo.java,v 1.13.2.2 2002/05/28 17:34:00 hoenicke Exp $
18  */

19
20 package jode.bytecode;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25
26 public class MethodInfo extends BinaryInfo {
27
28     ClassInfo clazzInfo;
29
30     int modifier;
31     String JavaDoc name;
32     String JavaDoc typeSig;
33
34     BytecodeInfo bytecode;
35     String JavaDoc[] exceptions;
36     boolean syntheticFlag;
37     boolean deprecatedFlag;
38
39     public MethodInfo(ClassInfo ci) {
40     clazzInfo = ci;
41     }
42
43     public MethodInfo(ClassInfo ci,
44               String JavaDoc name, String JavaDoc typeSig, int modifier) {
45     this.clazzInfo = ci;
46     this.name = name;
47     this.typeSig = typeSig;
48     this.modifier = modifier;
49     }
50
51     protected void readAttribute(String JavaDoc name, int length, ConstantPool cp,
52                  DataInputStream JavaDoc input,
53                  int howMuch) throws IOException JavaDoc {
54     if ((howMuch & KNOWNATTRIBS) != 0 && name.equals("Code")) {
55         bytecode = new BytecodeInfo(this);
56         bytecode.read(cp, input);
57     } else if (name.equals("Exceptions")) {
58         int count = input.readUnsignedShort();
59         exceptions = new String JavaDoc[count];
60         for (int i=0; i< count; i++)
61         exceptions[i] = cp.getClassName(input.readUnsignedShort());
62         if (length != 2 * (count + 1))
63         throw new ClassFormatException
64             ("Exceptions attribute has wrong length");
65     } else if (name.equals("Synthetic")) {
66         syntheticFlag = true;
67         if (length != 0)
68         throw new ClassFormatException
69             ("Synthetic attribute has wrong length");
70     } else if (name.equals("Deprecated")) {
71         deprecatedFlag = true;
72         if (length != 0)
73         throw new ClassFormatException
74             ("Deprecated attribute has wrong length");
75     } else
76         super.readAttribute(name, length, cp, input, howMuch);
77     }
78
79     public void read(ConstantPool constantPool,
80                      DataInputStream JavaDoc input, int howMuch) throws IOException JavaDoc {
81     modifier = input.readUnsignedShort();
82     name = constantPool.getUTF8(input.readUnsignedShort());
83         typeSig = constantPool.getUTF8(input.readUnsignedShort());
84         readAttributes(constantPool, input, howMuch);
85     }
86
87     public void reserveSmallConstants(GrowableConstantPool gcp) {
88     if (bytecode != null)
89         bytecode.reserveSmallConstants(gcp);
90     }
91
92     public void prepareWriting(GrowableConstantPool gcp) {
93     gcp.putUTF8(name);
94     gcp.putUTF8(typeSig);
95     if (bytecode != null) {
96         gcp.putUTF8("Code");
97         bytecode.prepareWriting(gcp);
98     }
99     if (exceptions != null) {
100         gcp.putUTF8("Exceptions");
101         for (int i=0; i< exceptions.length; i++)
102         gcp.putClassName(exceptions[i]);
103     }
104     if (syntheticFlag)
105         gcp.putUTF8("Synthetic");
106     if (deprecatedFlag)
107         gcp.putUTF8("Deprecated");
108     prepareAttributes(gcp);
109     }
110
111     protected int getKnownAttributeCount() {
112     int count = 0;
113     if (bytecode != null)
114         count++;
115     if (exceptions != null)
116         count++;
117     if (syntheticFlag)
118         count++;
119     if (deprecatedFlag)
120         count++;
121     return count;
122     }
123
124     public void writeKnownAttributes(GrowableConstantPool gcp,
125                      DataOutputStream JavaDoc output)
126     throws IOException JavaDoc {
127     if (bytecode != null) {
128         output.writeShort(gcp.putUTF8("Code"));
129         output.writeInt(bytecode.getSize());
130         bytecode.write(gcp, output);
131     }
132     if (exceptions != null) {
133         int count = exceptions.length;
134         output.writeShort(gcp.putUTF8("Exceptions"));
135         output.writeInt(2 + count * 2);
136         output.writeShort(count);
137         for (int i=0; i< count; i++)
138         output.writeShort(gcp.putClassName(exceptions[i]));
139     }
140     if (syntheticFlag) {
141         output.writeShort(gcp.putUTF8("Synthetic"));
142         output.writeInt(0);
143     }
144     if (deprecatedFlag) {
145         output.writeShort(gcp.putUTF8("Deprecated"));
146         output.writeInt(0);
147     }
148     }
149
150     public void write(GrowableConstantPool constantPool,
151               DataOutputStream JavaDoc output) throws IOException JavaDoc {
152     output.writeShort(modifier);
153     output.writeShort(constantPool.putUTF8(name));
154     output.writeShort(constantPool.putUTF8(typeSig));
155         writeAttributes(constantPool, output);
156     }
157
158     public void dropInfo(int howMuch) {
159     if ((howMuch & KNOWNATTRIBS) != 0) {
160         bytecode = null;
161         exceptions = null;
162     }
163     if (bytecode != null)
164         bytecode.dropInfo(howMuch);
165     super.dropInfo(howMuch);
166     }
167
168     public ClassInfo getClazzInfo() {
169     return clazzInfo;
170     }
171
172     public String JavaDoc getName() {
173         return name;
174     }
175
176     public String JavaDoc getType() {
177         return typeSig;
178     }
179
180     public int getModifiers() {
181         return modifier;
182     }
183
184     public boolean isStatic() {
185     return Modifier.isStatic(modifier);
186     }
187
188     public boolean isSynthetic() {
189     return syntheticFlag;
190     }
191
192     public boolean isDeprecated() {
193     return deprecatedFlag;
194     }
195
196     public BytecodeInfo getBytecode() {
197     return bytecode;
198     }
199
200     public String JavaDoc[] getExceptions() {
201     return exceptions;
202     }
203     
204     public void setName(String JavaDoc newName) {
205         name = newName;
206     }
207     
208     public void setType(String JavaDoc newType) {
209         typeSig = newType;
210     }
211
212     public void setModifiers(int newModifier) {
213     modifier = newModifier;
214     }
215
216     public void setSynthetic(boolean flag) {
217     syntheticFlag = flag;
218     }
219
220     public void setDeprecated(boolean flag) {
221     deprecatedFlag = flag;
222     }
223
224     public void setBytecode(BytecodeInfo newBytecode) {
225     clazzInfo.loadInfo(KNOWNATTRIBS);
226     bytecode = newBytecode;
227     }
228
229     public void setExceptions(String JavaDoc[] newExceptions) {
230     clazzInfo.loadInfo(KNOWNATTRIBS);
231     exceptions = newExceptions;
232     }
233
234     public String JavaDoc toString() {
235         return "Method "+Modifier.toString(modifier)+" "+
236             typeSig + " " + clazzInfo.getName() + "."+ name;
237     }
238 }
239
Popular Tags