KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > SignatureParser


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.InvokeInstruction;
27
28 /**
29  * A simple class to parse method signatures.
30  *
31  * @author David Hovemeyer
32  */

33 public class SignatureParser {
34     private class ParameterSignatureIterator implements Iterator JavaDoc<String JavaDoc> {
35         private int index = 1;
36
37         public boolean hasNext() {
38             return index < signature.length()
39                     && signature.charAt(index) != ')';
40         }
41
42         public String JavaDoc next() {
43             if (!hasNext()) throw new NoSuchElementException JavaDoc();
44             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
45             boolean done;
46             do {
47                 done = true;
48                 int ch = signature.charAt(index);
49                 switch (ch) {
50                 case 'B':
51                 case 'C':
52                 case 'D':
53                 case 'F':
54                 case 'I':
55                 case 'J':
56                 case 'S':
57                 case 'Z':
58                     result.append(signature.charAt(index));
59                     ++index;
60                     break;
61                     
62                 case 'L':
63                     int semi = signature.indexOf(';', index + 1);
64                     if (semi < 0)
65                         throw new IllegalStateException JavaDoc("Invalid method signature: " + signature);
66                     result.append(signature.substring(index, semi + 1));
67                     index = semi + 1;
68                     break;
69                     
70                 case '[':
71                     result.append('[');
72                     ++index;
73                     done = false;
74                     break;
75                     
76                 case 'V':
77                 default:
78                     throw new IllegalStateException JavaDoc("Invalid method signature: " + signature);
79                 }
80             } while (!done);
81
82             return result.toString();
83         }
84
85         public void remove() {
86             throw new UnsupportedOperationException JavaDoc();
87         }
88     }
89
90     private final String JavaDoc signature;
91
92     /**
93      * Constructor.
94      *
95      * @param signature the method signature to be parsed
96      */

97     public SignatureParser(String JavaDoc signature) {
98         if (!signature.startsWith("("))
99             throw new IllegalArgumentException JavaDoc("Bad method signature: " + signature);
100         this.signature = signature;
101     }
102
103     /**
104      * Get an Iterator over signatures of the method parameters.
105      *
106      * @return Iterator which returns the parameter type signatures in order
107      */

108     public Iterator JavaDoc<String JavaDoc> parameterSignatureIterator() {
109         return new ParameterSignatureIterator();
110     }
111     
112     /**
113      * Get the method return type signature.
114      *
115      * @return the method return type signature
116      */

117     public String JavaDoc getReturnTypeSignature() {
118         int endOfParams = signature.lastIndexOf(')');
119         if (endOfParams < 0)
120             throw new IllegalArgumentException JavaDoc("Bad method signature: " + signature);
121         return signature.substring(endOfParams + 1);
122     }
123     
124     /**
125      * Get the number of parameters in the signature.
126      *
127      * @return the number of parameters
128      */

129     public int getNumParameters() {
130         int count = 0;
131         for (Iterator JavaDoc<String JavaDoc> i = parameterSignatureIterator(); i.hasNext();) {
132             i.next();
133             ++count;
134         }
135         return count;
136     }
137     
138     /**
139      * Get the number of parameters passed to method invocation.
140      *
141      * @param inv
142      * @param cpg
143      * @return int number of parameters
144      */

145     public static int getNumParametersForInvocation(InvokeInstruction inv, ConstantPoolGen cpg) {
146         SignatureParser sigParser = new SignatureParser(inv.getSignature(cpg));
147         return sigParser.getNumParameters();
148     }
149     
150     public static void main(String JavaDoc[] args) {
151         if (args.length != 1) {
152             System.err.println("Usage: " + SignatureParser.class.getName() + " '<method signature>'");
153             System.exit(1);
154         }
155         SignatureParser parser = new SignatureParser(args[0]);
156         for (Iterator JavaDoc<String JavaDoc> i = parser.parameterSignatureIterator(); i.hasNext();){
157             System.out.println(i.next());
158         }
159         System.out.println(parser.getNumParameters() + " parameter(s)");
160     }
161 }
162
163 // vim:ts=4
164
Popular Tags