jtulach@144
|
1 |
/*
|
jtulach@144
|
2 |
* Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
|
jtulach@144
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@144
|
4 |
*
|
jtulach@144
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@144
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@144
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@144
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@144
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@144
|
10 |
*
|
jtulach@144
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@144
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@144
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@144
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@144
|
15 |
* accompanied this code).
|
jtulach@144
|
16 |
*
|
jtulach@144
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@144
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@144
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@144
|
20 |
*
|
jtulach@144
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@144
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@144
|
23 |
* questions.
|
jtulach@144
|
24 |
*/
|
jtulach@144
|
25 |
|
jtulach@144
|
26 |
|
jtulach@167
|
27 |
package org.apidesign.javap;
|
jtulach@144
|
28 |
|
jtulach@144
|
29 |
import java.util.*;
|
jtulach@144
|
30 |
import java.io.*;
|
jtulach@144
|
31 |
|
jtulach@144
|
32 |
/**
|
jtulach@144
|
33 |
* Returns java type signature.
|
jtulach@144
|
34 |
*
|
jtulach@144
|
35 |
* @author Sucheta Dambalkar
|
jtulach@144
|
36 |
*/
|
jtulach@144
|
37 |
public class TypeSignature {
|
jtulach@144
|
38 |
|
jtulach@144
|
39 |
String parameters = null;
|
jtulach@144
|
40 |
String returntype = null;
|
jtulach@144
|
41 |
String fieldtype = null;
|
jtulach@144
|
42 |
int argumentlength = 0;
|
jtulach@144
|
43 |
|
jtulach@144
|
44 |
public TypeSignature(String JVMSignature){
|
jtulach@144
|
45 |
|
jtulach@144
|
46 |
if(JVMSignature != null){
|
jtulach@144
|
47 |
if(JVMSignature.indexOf("(") == -1){
|
jtulach@144
|
48 |
//This is a field type.
|
jtulach@144
|
49 |
this.fieldtype = getFieldTypeSignature(JVMSignature);
|
jtulach@144
|
50 |
}else {
|
jtulach@144
|
51 |
String parameterdes = null;
|
jtulach@144
|
52 |
if((JVMSignature.indexOf(")")-1) > (JVMSignature.indexOf("("))){
|
jtulach@144
|
53 |
//Get parameter signature.
|
jtulach@144
|
54 |
parameterdes =
|
jtulach@144
|
55 |
JVMSignature.substring(JVMSignature.indexOf("(")+1,
|
jtulach@144
|
56 |
JVMSignature.indexOf(")"));
|
jtulach@144
|
57 |
this.parameters = getParametersHelper(parameterdes);
|
jtulach@144
|
58 |
}else this.parameters = "()";
|
jtulach@144
|
59 |
//Get return type signature.
|
jtulach@144
|
60 |
String returndes = JVMSignature.substring(JVMSignature.lastIndexOf(")")+1);
|
jtulach@144
|
61 |
this.returntype = getReturnTypeHelper(returndes);
|
jtulach@144
|
62 |
}
|
jtulach@144
|
63 |
}
|
jtulach@144
|
64 |
}
|
jtulach@144
|
65 |
|
jtulach@144
|
66 |
/**
|
jtulach@144
|
67 |
* Returns java type signature of a field.
|
jtulach@144
|
68 |
*/
|
jtulach@144
|
69 |
public String getFieldTypeSignature(String fielddes){
|
jtulach@144
|
70 |
if(fielddes.startsWith("L")){
|
jtulach@144
|
71 |
return(getObjectType(fielddes));
|
jtulach@144
|
72 |
}else if(fielddes.startsWith("[")){
|
jtulach@144
|
73 |
return(getArrayType(fielddes));
|
jtulach@144
|
74 |
}else
|
jtulach@144
|
75 |
return(getBaseType(fielddes));
|
jtulach@144
|
76 |
}
|
jtulach@144
|
77 |
|
jtulach@144
|
78 |
/**
|
jtulach@144
|
79 |
* Returns java type signature of a parameter.
|
jtulach@144
|
80 |
*/
|
jtulach@144
|
81 |
public String getParametersHelper(String parameterdes){
|
jtulach@144
|
82 |
Vector parameters = new Vector();
|
jtulach@144
|
83 |
int startindex = -1;
|
jtulach@144
|
84 |
int endindex = -1;
|
jtulach@144
|
85 |
String param = "";
|
jtulach@144
|
86 |
|
jtulach@144
|
87 |
while(parameterdes != null){
|
jtulach@144
|
88 |
|
jtulach@144
|
89 |
if(parameterdes.startsWith("L")){
|
jtulach@144
|
90 |
//parameter is a object.
|
jtulach@144
|
91 |
startindex = parameterdes.indexOf("L");
|
jtulach@144
|
92 |
endindex = parameterdes.indexOf(";");
|
jtulach@144
|
93 |
if(startindex < parameterdes.length()) {
|
jtulach@144
|
94 |
if(endindex == parameterdes.length()-1) {
|
jtulach@144
|
95 |
//last parameter
|
jtulach@144
|
96 |
param = parameterdes.substring(startindex);
|
jtulach@144
|
97 |
parameterdes = null;
|
jtulach@144
|
98 |
}else if(endindex+1 < parameterdes.length()){
|
jtulach@144
|
99 |
//rest parameters
|
jtulach@144
|
100 |
param = parameterdes.substring(startindex, endindex+1);
|
jtulach@144
|
101 |
parameterdes = parameterdes.substring(endindex+1);
|
jtulach@144
|
102 |
|
jtulach@144
|
103 |
}
|
jtulach@144
|
104 |
parameters.add(getObjectType(param));
|
jtulach@144
|
105 |
}
|
jtulach@144
|
106 |
}else if(parameterdes.startsWith("[")){
|
jtulach@144
|
107 |
//parameter is an array.
|
jtulach@144
|
108 |
String componentType = "";
|
jtulach@144
|
109 |
int enddim = -1;
|
jtulach@144
|
110 |
int st = 0;
|
jtulach@144
|
111 |
while(true){
|
jtulach@144
|
112 |
if(st < parameterdes.length()){
|
jtulach@144
|
113 |
if(parameterdes.charAt(st) == '['){
|
jtulach@144
|
114 |
|
jtulach@144
|
115 |
enddim = st;
|
jtulach@144
|
116 |
st++;
|
jtulach@144
|
117 |
}
|
jtulach@144
|
118 |
else break;
|
jtulach@144
|
119 |
}
|
jtulach@144
|
120 |
else break;
|
jtulach@144
|
121 |
}
|
jtulach@144
|
122 |
|
jtulach@144
|
123 |
if(enddim+1 < parameterdes.length()){
|
jtulach@144
|
124 |
/* Array dimension.*/
|
jtulach@144
|
125 |
param = parameterdes.substring(0,enddim+1);
|
jtulach@144
|
126 |
|
jtulach@144
|
127 |
}
|
jtulach@144
|
128 |
|
jtulach@144
|
129 |
int stotherparam = param.lastIndexOf("[")+1;
|
jtulach@144
|
130 |
|
jtulach@144
|
131 |
if(stotherparam < parameterdes.length()){
|
jtulach@144
|
132 |
componentType = parameterdes.substring(stotherparam);
|
jtulach@144
|
133 |
}
|
jtulach@144
|
134 |
|
jtulach@144
|
135 |
if(componentType.startsWith("L")){
|
jtulach@144
|
136 |
//parameter is array of objects.
|
jtulach@144
|
137 |
startindex = parameterdes.indexOf("L");
|
jtulach@144
|
138 |
endindex = parameterdes.indexOf(";");
|
jtulach@144
|
139 |
|
jtulach@144
|
140 |
if(endindex == parameterdes.length()-1){
|
jtulach@144
|
141 |
//last parameter
|
jtulach@144
|
142 |
param += parameterdes.substring(startindex);
|
jtulach@144
|
143 |
parameterdes = null;
|
jtulach@144
|
144 |
}else if(endindex+1 < parameterdes.length()){
|
jtulach@144
|
145 |
//rest parameters
|
jtulach@144
|
146 |
param += parameterdes.substring(startindex, endindex+1);
|
jtulach@144
|
147 |
parameterdes = parameterdes.substring(endindex+1);
|
jtulach@144
|
148 |
}
|
jtulach@144
|
149 |
}else{
|
jtulach@144
|
150 |
//parameter is array of base type.
|
jtulach@144
|
151 |
if(componentType.length() == 1){
|
jtulach@144
|
152 |
//last parameter.
|
jtulach@144
|
153 |
param += componentType;
|
jtulach@144
|
154 |
parameterdes = null;
|
jtulach@144
|
155 |
}
|
jtulach@144
|
156 |
else if (componentType.length() > 1) {
|
jtulach@144
|
157 |
//rest parameters.
|
jtulach@144
|
158 |
param += componentType.substring(0,1);
|
jtulach@144
|
159 |
parameterdes = componentType.substring(1);
|
jtulach@144
|
160 |
}
|
jtulach@144
|
161 |
}
|
jtulach@144
|
162 |
parameters.add(getArrayType(param));
|
jtulach@144
|
163 |
|
jtulach@144
|
164 |
|
jtulach@144
|
165 |
}else {
|
jtulach@144
|
166 |
|
jtulach@144
|
167 |
//parameter is of base type.
|
jtulach@144
|
168 |
if(parameterdes.length() == 1){
|
jtulach@144
|
169 |
//last parameter
|
jtulach@144
|
170 |
param = parameterdes;
|
jtulach@144
|
171 |
parameterdes = null;
|
jtulach@144
|
172 |
}
|
jtulach@144
|
173 |
else if (parameterdes.length() > 1) {
|
jtulach@144
|
174 |
//rest parameters.
|
jtulach@144
|
175 |
param = parameterdes.substring(0,1);
|
jtulach@144
|
176 |
parameterdes = parameterdes.substring(1);
|
jtulach@144
|
177 |
}
|
jtulach@144
|
178 |
parameters.add(getBaseType(param));
|
jtulach@144
|
179 |
}
|
jtulach@144
|
180 |
}
|
jtulach@144
|
181 |
|
jtulach@144
|
182 |
/* number of arguments of a method.*/
|
jtulach@144
|
183 |
argumentlength = parameters.size();
|
jtulach@144
|
184 |
|
jtulach@144
|
185 |
/* java type signature.*/
|
jtulach@144
|
186 |
String parametersignature = "(";
|
jtulach@144
|
187 |
int i;
|
jtulach@144
|
188 |
|
jtulach@144
|
189 |
for(i = 0; i < parameters.size(); i++){
|
jtulach@144
|
190 |
parametersignature += (String)parameters.elementAt(i);
|
jtulach@144
|
191 |
if(i != parameters.size()-1){
|
jtulach@144
|
192 |
parametersignature += ", ";
|
jtulach@144
|
193 |
}
|
jtulach@144
|
194 |
}
|
jtulach@144
|
195 |
parametersignature += ")";
|
jtulach@144
|
196 |
return parametersignature;
|
jtulach@144
|
197 |
}
|
jtulach@144
|
198 |
|
jtulach@144
|
199 |
/**
|
jtulach@144
|
200 |
* Returns java type signature for a return type.
|
jtulach@144
|
201 |
*/
|
jtulach@144
|
202 |
public String getReturnTypeHelper(String returndes){
|
jtulach@144
|
203 |
return getFieldTypeSignature(returndes);
|
jtulach@144
|
204 |
}
|
jtulach@144
|
205 |
|
jtulach@144
|
206 |
/**
|
jtulach@144
|
207 |
* Returns java type signature for a base type.
|
jtulach@144
|
208 |
*/
|
jtulach@144
|
209 |
public String getBaseType(String baseType){
|
jtulach@144
|
210 |
if(baseType != null){
|
jtulach@144
|
211 |
if(baseType.equals("B")) return "byte";
|
jtulach@144
|
212 |
else if(baseType.equals("C")) return "char";
|
jtulach@144
|
213 |
else if(baseType.equals("D")) return "double";
|
jtulach@144
|
214 |
else if(baseType.equals("F")) return "float";
|
jtulach@144
|
215 |
else if(baseType.equals("I")) return "int";
|
jtulach@144
|
216 |
else if(baseType.equals("J")) return "long";
|
jtulach@144
|
217 |
else if(baseType.equals("S")) return "short";
|
jtulach@144
|
218 |
else if(baseType.equals("Z")) return "boolean";
|
jtulach@144
|
219 |
else if(baseType.equals("V")) return "void";
|
jtulach@144
|
220 |
}
|
jtulach@144
|
221 |
return null;
|
jtulach@144
|
222 |
}
|
jtulach@144
|
223 |
|
jtulach@144
|
224 |
/**
|
jtulach@144
|
225 |
* Returns java type signature for a object type.
|
jtulach@144
|
226 |
*/
|
jtulach@144
|
227 |
public String getObjectType(String JVMobjectType) {
|
jtulach@144
|
228 |
String objectType = "";
|
jtulach@144
|
229 |
int startindex = JVMobjectType.indexOf("L")+1;
|
jtulach@144
|
230 |
int endindex = JVMobjectType.indexOf(";");
|
jtulach@144
|
231 |
if((startindex != -1) && (endindex != -1)){
|
jtulach@144
|
232 |
if((startindex < JVMobjectType.length()) && (endindex < JVMobjectType.length())){
|
jtulach@144
|
233 |
objectType = JVMobjectType.substring(startindex, endindex);
|
jtulach@144
|
234 |
}
|
jtulach@144
|
235 |
objectType = objectType.replace('/','.');
|
jtulach@144
|
236 |
return objectType;
|
jtulach@144
|
237 |
}
|
jtulach@144
|
238 |
return null;
|
jtulach@144
|
239 |
}
|
jtulach@144
|
240 |
|
jtulach@144
|
241 |
/**
|
jtulach@144
|
242 |
* Returns java type signature for array type.
|
jtulach@144
|
243 |
*/
|
jtulach@144
|
244 |
public String getArrayType(String arrayType) {
|
jtulach@144
|
245 |
if(arrayType != null){
|
jtulach@144
|
246 |
String dimention = "";
|
jtulach@144
|
247 |
|
jtulach@144
|
248 |
while(arrayType.indexOf("[") != -1){
|
jtulach@144
|
249 |
dimention += "[]";
|
jtulach@144
|
250 |
|
jtulach@144
|
251 |
int startindex = arrayType.indexOf("[")+1;
|
jtulach@144
|
252 |
if(startindex <= arrayType.length()){
|
jtulach@144
|
253 |
arrayType = arrayType.substring(startindex);
|
jtulach@144
|
254 |
}
|
jtulach@144
|
255 |
}
|
jtulach@144
|
256 |
|
jtulach@144
|
257 |
String componentType = "";
|
jtulach@144
|
258 |
if(arrayType.startsWith("L")){
|
jtulach@144
|
259 |
componentType = getObjectType(arrayType);
|
jtulach@144
|
260 |
}else {
|
jtulach@144
|
261 |
componentType = getBaseType(arrayType);
|
jtulach@144
|
262 |
}
|
jtulach@144
|
263 |
return componentType+dimention;
|
jtulach@144
|
264 |
}
|
jtulach@144
|
265 |
return null;
|
jtulach@144
|
266 |
}
|
jtulach@144
|
267 |
|
jtulach@144
|
268 |
/**
|
jtulach@144
|
269 |
* Returns java type signature for parameters.
|
jtulach@144
|
270 |
*/
|
jtulach@144
|
271 |
public String getParameters(){
|
jtulach@144
|
272 |
return parameters;
|
jtulach@144
|
273 |
}
|
jtulach@144
|
274 |
|
jtulach@144
|
275 |
/**
|
jtulach@144
|
276 |
* Returns java type signature for return type.
|
jtulach@144
|
277 |
*/
|
jtulach@144
|
278 |
public String getReturnType(){
|
jtulach@144
|
279 |
return returntype;
|
jtulach@144
|
280 |
}
|
jtulach@144
|
281 |
|
jtulach@144
|
282 |
/**
|
jtulach@144
|
283 |
* Returns java type signature for field type.
|
jtulach@144
|
284 |
*/
|
jtulach@144
|
285 |
public String getFieldType(){
|
jtulach@144
|
286 |
return fieldtype;
|
jtulach@144
|
287 |
}
|
jtulach@144
|
288 |
|
jtulach@144
|
289 |
/**
|
jtulach@144
|
290 |
* Return number of arguments of a method.
|
jtulach@144
|
291 |
*/
|
jtulach@144
|
292 |
public int getArgumentlength(){
|
jtulach@144
|
293 |
return argumentlength;
|
jtulach@144
|
294 |
}
|
jtulach@144
|
295 |
}
|