1 /*
2 * @(#)MethodInfo.java 1.4 95/08/16 Chuck McManis
3 *
4 * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and distribute this software
7 * and its documentation for NON-COMMERCIAL purposes and without
8 * fee is hereby granted provided that this copyright notice
9 * appears in all copies.
10 *
11 * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
12 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
15 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
16 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
17 */
18
19 package ch.elca.dependency.core.classinfo;
20
21 import java.io.DataInputStream;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 /***
26 * This class describes a Method as it is stored in the class file.
27 * The attribute associated with method is the code that actually implements
28 * the method. Since we don't need to manipulate the byte codes directly
29 * we leave them as an opaque chunk in the attributes[] array. References
30 * in the code are all references into the constant table so when we are
31 * modifing a class to use a different object we needn't get into the code
32 * level.
33 *
34 * @version 1.4, 16 Aug 1995
35 * @author Chuck McManis
36 * @see ch.elca.dependency.rawmodel.classinfo.Reader
37 */
38
39 public class MethodInfo {
40 short accessFlags;
41 ConstantPoolInfo name;
42 ConstantPoolInfo signature;
43 AttributeInfo attributes[];
44
45 /***
46 * Read a method_info from the data stream.
47 */
48 public boolean read(DataInputStream di, ConstantPoolInfo pool[])
49 throws IOException {
50 int count;
51
52 accessFlags = di.readShort();
53 name = pool[di.readShort()];
54 signature = pool[di.readShort()];
55 count = di.readShort();
56 if ( count != 0 ) {
57 attributes = new AttributeInfo[count];
58 for ( int i = 0 ; i < count ; i++ ) {
59 attributes[i] = new AttributeInfo(); // "code"
60 if ( !attributes[i].read(di, pool) ) {
61 return (false);
62 }
63 }
64 }
65 return (true);
66 }
67
68 /***
69 * Write out a method_info, do constant table fixups on the write.
70 */
71 public void write(DataOutputStream dos, ConstantPoolInfo pool[])
72 throws IOException, Exception {
73
74 dos.writeShort(accessFlags);
75 dos.writeShort(ConstantPoolInfo.indexOf(name, pool));
76 dos.writeShort(ConstantPoolInfo.indexOf(signature, pool));
77 if ( attributes == null ) {
78 dos.writeShort(0);
79 } else {
80 dos.writeShort(attributes.length);
81 for ( int i = 0 ; i < attributes.length ; i++ )
82 attributes[i].write(dos, pool);
83 }
84 }
85
86 /***
87 * print out the method, much as you would see it in the source
88 * file. The string ClassName is substituted for <init> when
89 * printing.
90 */
91 public String toString(String className) {
92 StringBuffer x = new StringBuffer();
93 boolean isArray = false;
94 String paramSig;
95 String returnSig;
96 int ndx = 0;
97 StringBuffer parameterList = new StringBuffer();
98 char initialParameter = 'a';
99 StringBuffer varName = new StringBuffer();
100
101
102 String s = signature.strValue;
103 paramSig = s.substring(s.indexOf('(')+1, s.indexOf(')'));
104 returnSig = s.substring(s.indexOf(')')+1);
105
106 x.append(Reader.accessString(accessFlags));
107 /* catch constructors */
108 if ( (className != null ) && ( name.toString().startsWith("<init>")) )
109 parameterList.append(className);
110 else
111 parameterList.append(name.toString());
112 parameterList.append("(");
113
114 if ( (paramSig.length() > 0 ) && paramSig.charAt(0) != 'V' ) {
115 while ( paramSig.length() > 0 ) {
116 varName.setLength(0);
117 varName.append(initialParameter);
118 initialParameter++;
119 parameterList.append(
120 Reader.typeString(paramSig, varName.toString()));
121 paramSig = Reader.nextSig(paramSig);
122 if ( paramSig.length() > 0 ) {
123 parameterList.append(", ");
124 }
125 }
126
127 }
128 parameterList.append(")");
129 x.append(Reader.typeString(returnSig, parameterList.toString()));
130 x.append(";");
131 return (x.toString());
132 }
133
134 /***
135 * Generic toString method, init method is unchanged.
136 */
137 public String toString() {
138 return (toString((String)null));
139 }
140 }
This page was automatically generated by Maven