author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5841 | 7a8448425bb7 |
child 6930 | b6fea484cbb2 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
||
27 |
package com.sun.tools.javah; |
|
28 |
||
29 |
import java.util.*; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
30 |
import javax.lang.model.element.Name; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
31 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
32 |
import javax.lang.model.type.ArrayType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
33 |
import javax.lang.model.type.DeclaredType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
34 |
import javax.lang.model.type.NoType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
35 |
import javax.lang.model.type.PrimitiveType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
36 |
import javax.lang.model.type.TypeKind; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
37 |
import javax.lang.model.type.TypeMirror; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
38 |
import javax.lang.model.type.TypeVariable; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
39 |
import javax.lang.model.type.TypeVisitor; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
40 |
import javax.lang.model.util.Elements; |
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
41 |
import javax.lang.model.util.SimpleTypeVisitor7; |
10 | 42 |
|
43 |
/** |
|
44 |
* Returns internal type signature. |
|
45 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5841
diff
changeset
|
46 |
* <p><b>This is NOT part of any supported API. |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
47 |
* If you write code that depends on this, you do so at your own |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
48 |
* risk. This code and its internal interfaces are subject to change |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
49 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
50 |
* |
10 | 51 |
* @author Sucheta Dambalkar |
52 |
*/ |
|
53 |
||
54 |
public class TypeSignature{ |
|
55 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
56 |
Elements elems; |
10 | 57 |
|
58 |
/* Signature Characters */ |
|
59 |
||
60 |
private static final String SIG_VOID = "V"; |
|
61 |
private static final String SIG_BOOLEAN = "Z"; |
|
62 |
private static final String SIG_BYTE = "B"; |
|
63 |
private static final String SIG_CHAR = "C"; |
|
64 |
private static final String SIG_SHORT = "S"; |
|
65 |
private static final String SIG_INT = "I"; |
|
66 |
private static final String SIG_LONG = "J"; |
|
67 |
private static final String SIG_FLOAT = "F"; |
|
68 |
private static final String SIG_DOUBLE = "D"; |
|
69 |
private static final String SIG_ARRAY = "["; |
|
70 |
private static final String SIG_CLASS = "L"; |
|
71 |
||
72 |
||
73 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
74 |
public TypeSignature(Elements elems){ |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
75 |
this.elems = elems; |
10 | 76 |
} |
77 |
||
78 |
/* |
|
79 |
* Returns the type signature of a field according to JVM specs |
|
80 |
*/ |
|
81 |
public String getTypeSignature(String javasignature){ |
|
82 |
return getParamJVMSignature(javasignature); |
|
83 |
} |
|
84 |
||
85 |
/* |
|
86 |
* Returns the type signature of a method according to JVM specs |
|
87 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
88 |
public String getTypeSignature(String javasignature, TypeMirror returnType){ |
10 | 89 |
String signature = null; //Java type signature. |
90 |
String typeSignature = null; //Internal type signature. |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
91 |
List<String> params = new ArrayList<String>(); //List of parameters. |
10 | 92 |
String paramsig = null; //Java parameter signature. |
93 |
String paramJVMSig = null; //Internal parameter signature. |
|
94 |
String returnSig = null; //Java return type signature. |
|
95 |
String returnJVMType = null; //Internal return type signature. |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
96 |
int dimensions = 0; //Array dimension. |
10 | 97 |
|
98 |
int startIndex = -1; |
|
99 |
int endIndex = -1; |
|
100 |
StringTokenizer st = null; |
|
101 |
int i = 0; |
|
102 |
||
103 |
// Gets the actual java signature without parentheses. |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
104 |
if (javasignature != null) { |
10 | 105 |
startIndex = javasignature.indexOf("("); |
106 |
endIndex = javasignature.indexOf(")"); |
|
107 |
} |
|
108 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
109 |
if (((startIndex != -1) && (endIndex != -1)) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
110 |
&&(startIndex+1 < javasignature.length()) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
111 |
&&(endIndex < javasignature.length())) { |
10 | 112 |
signature = javasignature.substring(startIndex+1, endIndex); |
113 |
} |
|
114 |
||
115 |
// Separates parameters. |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
116 |
if (signature != null) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
117 |
if (signature.indexOf(",") != -1) { |
10 | 118 |
st = new StringTokenizer(signature, ","); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
119 |
if (st != null) { |
10 | 120 |
while (st.hasMoreTokens()) { |
121 |
params.add(st.nextToken()); |
|
122 |
} |
|
123 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
124 |
} else { |
10 | 125 |
params.add(signature); |
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/* JVM type signature. */ |
|
130 |
typeSignature = "("; |
|
131 |
||
132 |
// Gets indivisual internal parameter signature. |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
133 |
while (params.isEmpty() != true) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
134 |
paramsig = params.remove(i).trim(); |
10 | 135 |
paramJVMSig = getParamJVMSignature(paramsig); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
136 |
if (paramJVMSig != null) { |
10 | 137 |
typeSignature += paramJVMSig; |
138 |
} |
|
139 |
} |
|
140 |
||
141 |
typeSignature += ")"; |
|
142 |
||
143 |
// Get internal return type signature. |
|
144 |
||
145 |
returnJVMType = ""; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
146 |
if (returnType != null) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
147 |
dimensions = dimensions(returnType); |
10 | 148 |
} |
149 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
150 |
//Gets array dimension of return type. |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
151 |
while (dimensions-- > 0) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
152 |
returnJVMType += "["; |
10 | 153 |
} |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
154 |
if (returnType != null) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
155 |
returnSig = qualifiedTypeName(returnType); |
10 | 156 |
returnJVMType += getComponentType(returnSig); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
157 |
} else { |
10 | 158 |
System.out.println("Invalid return type."); |
159 |
} |
|
160 |
||
161 |
typeSignature += returnJVMType; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
162 |
|
10 | 163 |
return typeSignature; |
164 |
} |
|
165 |
||
166 |
/* |
|
167 |
* Returns internal signature of a parameter. |
|
168 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
169 |
private String getParamJVMSignature(String paramsig) { |
10 | 170 |
String paramJVMSig = ""; |
171 |
String componentType =""; |
|
172 |
||
173 |
if(paramsig != null){ |
|
174 |
||
175 |
if(paramsig.indexOf("[]") != -1) { |
|
176 |
// Gets array dimension. |
|
177 |
int endindex = paramsig.indexOf("[]"); |
|
178 |
componentType = paramsig.substring(0, endindex); |
|
179 |
String dimensionString = paramsig.substring(endindex); |
|
180 |
if(dimensionString != null){ |
|
181 |
while(dimensionString.indexOf("[]") != -1){ |
|
182 |
paramJVMSig += "["; |
|
183 |
int beginindex = dimensionString.indexOf("]") + 1; |
|
184 |
if(beginindex < dimensionString.length()){ |
|
185 |
dimensionString = dimensionString.substring(beginindex); |
|
186 |
}else |
|
187 |
dimensionString = ""; |
|
188 |
} |
|
189 |
} |
|
190 |
} else componentType = paramsig; |
|
191 |
||
192 |
paramJVMSig += getComponentType(componentType); |
|
193 |
} |
|
194 |
return paramJVMSig; |
|
195 |
} |
|
196 |
||
197 |
/* |
|
198 |
* Returns internal signature of a component. |
|
199 |
*/ |
|
200 |
private String getComponentType(String componentType){ |
|
201 |
||
202 |
String JVMSig = ""; |
|
203 |
||
204 |
if(componentType != null){ |
|
205 |
if(componentType.equals("void")) JVMSig += SIG_VOID ; |
|
206 |
else if(componentType.equals("boolean")) JVMSig += SIG_BOOLEAN ; |
|
207 |
else if(componentType.equals("byte")) JVMSig += SIG_BYTE ; |
|
208 |
else if(componentType.equals("char")) JVMSig += SIG_CHAR ; |
|
209 |
else if(componentType.equals("short")) JVMSig += SIG_SHORT ; |
|
210 |
else if(componentType.equals("int")) JVMSig += SIG_INT ; |
|
211 |
else if(componentType.equals("long")) JVMSig += SIG_LONG ; |
|
212 |
else if(componentType.equals("float")) JVMSig += SIG_FLOAT ; |
|
213 |
else if(componentType.equals("double")) JVMSig += SIG_DOUBLE ; |
|
214 |
else { |
|
215 |
if(!componentType.equals("")){ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
216 |
TypeElement classNameDoc = elems.getTypeElement(componentType); |
10 | 217 |
|
218 |
if(classNameDoc == null){ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
219 |
System.out.println("Invalid class type for " + componentType); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
220 |
new Exception().printStackTrace(); |
10 | 221 |
}else { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
222 |
String classname = classNameDoc.getQualifiedName().toString(); |
10 | 223 |
String newclassname = classname.replace('.', '/'); |
224 |
JVMSig += "L"; |
|
225 |
JVMSig += newclassname; |
|
226 |
JVMSig += ";"; |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
return JVMSig; |
|
232 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
233 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
234 |
int dimensions(TypeMirror t) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
235 |
if (t.getKind() != TypeKind.ARRAY) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
236 |
return 0; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
237 |
return 1 + dimensions(((ArrayType) t).getComponentType()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
238 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
239 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
240 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
241 |
String qualifiedTypeName(TypeMirror type) { |
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
242 |
TypeVisitor<Name, Void> v = new SimpleTypeVisitor7<Name, Void>() { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
243 |
@Override |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
244 |
public Name visitArray(ArrayType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
245 |
return t.getComponentType().accept(this, p); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
246 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
247 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
248 |
@Override |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
249 |
public Name visitDeclared(DeclaredType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
250 |
return ((TypeElement) t.asElement()).getQualifiedName(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
251 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
252 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
253 |
@Override |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
254 |
public Name visitPrimitive(PrimitiveType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
255 |
return elems.getName(t.toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
256 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
257 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
258 |
@Override |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
259 |
public Name visitNoType(NoType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
260 |
if (t.getKind() == TypeKind.VOID) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
261 |
return elems.getName("void"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
262 |
return defaultAction(t, p); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
263 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
264 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
265 |
@Override |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
266 |
public Name visitTypeVariable(TypeVariable t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
267 |
return t.getUpperBound().accept(this, p); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
268 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
269 |
}; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
270 |
return v.visit(type).toString(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
271 |
} |
10 | 272 |
} |