001: /* MethodInfo Copyright (C) 1998-2002 Jochen Hoenicke.
002: *
003: * This program is free software; you can redistribute it and/or modify
004: * it under the terms of the GNU Lesser General Public License as published by
005: * the Free Software Foundation; either version 2, or (at your option)
006: * any later version.
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program; see the file COPYING.LESSER. If not, write to
015: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
016: *
017: * $Id: MethodInfo.java,v 1.13.2.2 2002/05/28 17:34:00 hoenicke Exp $
018: */
019:
020: package jode.bytecode;
021:
022: import java.io.DataInputStream;
023: import java.io.DataOutputStream;
024: import java.io.IOException;
025: import java.lang.reflect.Modifier;
026:
027: public class MethodInfo extends BinaryInfo {
028:
029: ClassInfo clazzInfo;
030:
031: int modifier;
032: String name;
033: String typeSig;
034:
035: BytecodeInfo bytecode;
036: String[] exceptions;
037: boolean syntheticFlag;
038: boolean deprecatedFlag;
039:
040: public MethodInfo(ClassInfo ci) {
041: clazzInfo = ci;
042: }
043:
044: public MethodInfo(ClassInfo ci, String name, String typeSig,
045: int modifier) {
046: this .clazzInfo = ci;
047: this .name = name;
048: this .typeSig = typeSig;
049: this .modifier = modifier;
050: }
051:
052: protected void readAttribute(String name, int length,
053: ConstantPool cp, DataInputStream input, int howMuch)
054: throws IOException {
055: if ((howMuch & KNOWNATTRIBS) != 0 && name.equals("Code")) {
056: bytecode = new BytecodeInfo(this );
057: bytecode.read(cp, input);
058: } else if (name.equals("Exceptions")) {
059: int count = input.readUnsignedShort();
060: exceptions = new String[count];
061: for (int i = 0; i < count; i++)
062: exceptions[i] = cp.getClassName(input
063: .readUnsignedShort());
064: if (length != 2 * (count + 1))
065: throw new ClassFormatException(
066: "Exceptions attribute has wrong length");
067: } else if (name.equals("Synthetic")) {
068: syntheticFlag = true;
069: if (length != 0)
070: throw new ClassFormatException(
071: "Synthetic attribute has wrong length");
072: } else if (name.equals("Deprecated")) {
073: deprecatedFlag = true;
074: if (length != 0)
075: throw new ClassFormatException(
076: "Deprecated attribute has wrong length");
077: } else
078: super .readAttribute(name, length, cp, input, howMuch);
079: }
080:
081: public void read(ConstantPool constantPool, DataInputStream input,
082: int howMuch) throws IOException {
083: modifier = input.readUnsignedShort();
084: name = constantPool.getUTF8(input.readUnsignedShort());
085: typeSig = constantPool.getUTF8(input.readUnsignedShort());
086: readAttributes(constantPool, input, howMuch);
087: }
088:
089: public void reserveSmallConstants(GrowableConstantPool gcp) {
090: if (bytecode != null)
091: bytecode.reserveSmallConstants(gcp);
092: }
093:
094: public void prepareWriting(GrowableConstantPool gcp) {
095: gcp.putUTF8(name);
096: gcp.putUTF8(typeSig);
097: if (bytecode != null) {
098: gcp.putUTF8("Code");
099: bytecode.prepareWriting(gcp);
100: }
101: if (exceptions != null) {
102: gcp.putUTF8("Exceptions");
103: for (int i = 0; i < exceptions.length; i++)
104: gcp.putClassName(exceptions[i]);
105: }
106: if (syntheticFlag)
107: gcp.putUTF8("Synthetic");
108: if (deprecatedFlag)
109: gcp.putUTF8("Deprecated");
110: prepareAttributes(gcp);
111: }
112:
113: protected int getKnownAttributeCount() {
114: int count = 0;
115: if (bytecode != null)
116: count++;
117: if (exceptions != null)
118: count++;
119: if (syntheticFlag)
120: count++;
121: if (deprecatedFlag)
122: count++;
123: return count;
124: }
125:
126: public void writeKnownAttributes(GrowableConstantPool gcp,
127: DataOutputStream output) throws IOException {
128: if (bytecode != null) {
129: output.writeShort(gcp.putUTF8("Code"));
130: output.writeInt(bytecode.getSize());
131: bytecode.write(gcp, output);
132: }
133: if (exceptions != null) {
134: int count = exceptions.length;
135: output.writeShort(gcp.putUTF8("Exceptions"));
136: output.writeInt(2 + count * 2);
137: output.writeShort(count);
138: for (int i = 0; i < count; i++)
139: output.writeShort(gcp.putClassName(exceptions[i]));
140: }
141: if (syntheticFlag) {
142: output.writeShort(gcp.putUTF8("Synthetic"));
143: output.writeInt(0);
144: }
145: if (deprecatedFlag) {
146: output.writeShort(gcp.putUTF8("Deprecated"));
147: output.writeInt(0);
148: }
149: }
150:
151: public void write(GrowableConstantPool constantPool,
152: DataOutputStream output) throws IOException {
153: output.writeShort(modifier);
154: output.writeShort(constantPool.putUTF8(name));
155: output.writeShort(constantPool.putUTF8(typeSig));
156: writeAttributes(constantPool, output);
157: }
158:
159: public void dropInfo(int howMuch) {
160: if ((howMuch & KNOWNATTRIBS) != 0) {
161: bytecode = null;
162: exceptions = null;
163: }
164: if (bytecode != null)
165: bytecode.dropInfo(howMuch);
166: super .dropInfo(howMuch);
167: }
168:
169: public ClassInfo getClazzInfo() {
170: return clazzInfo;
171: }
172:
173: public String getName() {
174: return name;
175: }
176:
177: public String getType() {
178: return typeSig;
179: }
180:
181: public int getModifiers() {
182: return modifier;
183: }
184:
185: public boolean isStatic() {
186: return Modifier.isStatic(modifier);
187: }
188:
189: public boolean isSynthetic() {
190: return syntheticFlag;
191: }
192:
193: public boolean isDeprecated() {
194: return deprecatedFlag;
195: }
196:
197: public BytecodeInfo getBytecode() {
198: return bytecode;
199: }
200:
201: public String[] getExceptions() {
202: return exceptions;
203: }
204:
205: public void setName(String newName) {
206: name = newName;
207: }
208:
209: public void setType(String newType) {
210: typeSig = newType;
211: }
212:
213: public void setModifiers(int newModifier) {
214: modifier = newModifier;
215: }
216:
217: public void setSynthetic(boolean flag) {
218: syntheticFlag = flag;
219: }
220:
221: public void setDeprecated(boolean flag) {
222: deprecatedFlag = flag;
223: }
224:
225: public void setBytecode(BytecodeInfo newBytecode) {
226: clazzInfo.loadInfo(KNOWNATTRIBS);
227: bytecode = newBytecode;
228: }
229:
230: public void setExceptions(String[] newExceptions) {
231: clazzInfo.loadInfo(KNOWNATTRIBS);
232: exceptions = newExceptions;
233: }
234:
235: public String toString() {
236: return "Method " + Modifier.toString(modifier) + " " + typeSig
237: + " " + clazzInfo.getName() + "." + name;
238: }
239: }
|