KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > SignatureHelper


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38 import org.apache.oro.text.perl.*;
39
40 public final class SignatureHelper {
41     private static final Perl5Util perl = new Perl5Util();
42
43     private static Map conversion = new HashMap();
44
45     static {
46         conversion.put("B", "byte");
47         conversion.put("C", "char");
48         conversion.put("D", "double");
49         conversion.put("F", "float");
50         conversion.put("I", "int");
51         conversion.put("J", "long");
52         conversion.put("S", "short");
53         conversion.put("V", "void");
54         conversion.put("Z", "boolean");
55     }
56
57     static String JavaDoc convert(String JavaDoc type) {
58         String JavaDoc result = null;
59
60         Logger.getLogger(SignatureHelper.class).debug("Begin Convert(\"" + type + "\")");
61
62         if (type.length() == 1) {
63             result = (String JavaDoc) conversion.get(type);
64         } else if (type.charAt(0) == 'L') {
65             result = path2ClassName(type.substring(1, type.indexOf(';')));
66         } else if (type.charAt(0) == '[') {
67             result = convert(type.substring(1)) + "[]";
68         }
69
70         Logger.getLogger(SignatureHelper.class).debug("End Convert(\"" + type + "\"): \"" + result + "\"");
71
72         return result;
73     }
74
75     public static String JavaDoc path2ClassName(String JavaDoc path) {
76         return perl.substitute("s/\\//./g", path);
77     }
78     
79     public static String JavaDoc getSignature(String JavaDoc descriptor) {
80         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
81
82         Logger.getLogger(SignatureHelper.class).debug("Begin Signature(\"" + descriptor + "\")");
83
84         result.append("(");
85
86         int start = descriptor.indexOf("(") + 1;
87         int end = descriptor.indexOf(")");
88
89         SignatureIterator i = new SignatureIterator(descriptor.substring(start, end));
90         while (i.hasNext()) {
91             result.append(i.next());
92             if (i.hasNext()) {
93                 result.append(", ");
94             }
95         }
96
97         result.append(")");
98
99         Logger.getLogger(SignatureHelper.class).debug("End Signature(\"" + descriptor + "\"): \"" + result + "\"");
100
101         return result.toString();
102     }
103
104     public static int getParameterCount(String JavaDoc descriptor) {
105         int result = 0;
106
107         Logger.getLogger(SignatureHelper.class).debug("Begin ParameterCount(\"" + descriptor + "\")");
108
109         int start = descriptor.indexOf("(") + 1;
110         int end = descriptor.indexOf(")");
111
112         SignatureIterator i = new SignatureIterator(descriptor.substring(start, end));
113         while (i.hasNext()) {
114             i.next();
115             result++;
116         }
117
118         Logger.getLogger(SignatureHelper.class).debug("End ParameterCount(\"" + descriptor + "\"): \"" + result + "\"");
119
120         return result;
121     }
122
123     public static String JavaDoc getReturnType(String JavaDoc descriptor) {
124         return convert(descriptor.substring(descriptor.lastIndexOf(")") + 1));
125     }
126
127     public static String JavaDoc getType(String JavaDoc descriptor) {
128         return convert(descriptor);
129     }
130 }
131
132 class SignatureIterator implements Iterator {
133     private String JavaDoc descriptor;
134     private int currentPos = 0;
135
136     public SignatureIterator(String JavaDoc descriptor) {
137         this.descriptor = descriptor;
138     }
139
140     public boolean hasNext() {
141         return currentPos < descriptor.length();
142     }
143
144     public Object JavaDoc next() {
145         String JavaDoc result;
146
147         if (hasNext()) {
148             int nextPos = currentPos;
149
150             while (descriptor.charAt(nextPos) == '[') {
151                 nextPos++;
152             }
153
154             if (descriptor.charAt(nextPos) == 'L') {
155                 nextPos = descriptor.indexOf(";", nextPos);
156             }
157
158             result = SignatureHelper.convert(descriptor.substring(currentPos, nextPos + 1));
159
160             currentPos = nextPos + 1;
161         } else {
162             throw new NoSuchElementException();
163         }
164
165         return result;
166     }
167
168     public void remove() {
169         throw new UnsupportedOperationException JavaDoc();
170     }
171 }
172
Popular Tags