author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 14263 | 473b1eaede64 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14263 | 2 |
* Copyright (c) 2002, 2012, 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 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
29 |
import javax.lang.model.element.ExecutableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
30 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
31 |
import javax.lang.model.element.VariableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
32 |
import javax.lang.model.util.Elements; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
33 |
import javax.lang.model.util.Types; |
10 | 34 |
|
35 |
/** |
|
36 |
* A utility for mangling java identifiers into C names. Should make |
|
37 |
* this more fine grained and distribute the functionality to the |
|
38 |
* generators. |
|
39 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* <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:
10
diff
changeset
|
41 |
* 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:
10
diff
changeset
|
42 |
* 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:
10
diff
changeset
|
43 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
44 |
* |
10 | 45 |
* @author Sucheta Dambalkar(Revised) |
46 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
47 |
public class Mangle { |
10 | 48 |
|
49 |
public static class Type { |
|
50 |
public static final int CLASS = 1; |
|
51 |
public static final int FIELDSTUB = 2; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
52 |
public static final int FIELD = 3; |
10 | 53 |
public static final int JNI = 4; |
54 |
public static final int SIGNATURE = 5; |
|
55 |
public static final int METHOD_JDK_1 = 6; |
|
56 |
public static final int METHOD_JNI_SHORT = 7; |
|
57 |
public static final int METHOD_JNI_LONG = 8; |
|
22163 | 58 |
} |
10 | 59 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
60 |
private Elements elems; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
61 |
private Types types; |
10 | 62 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
63 |
Mangle(Elements elems, Types types) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
64 |
this.elems = elems; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
65 |
this.types = types; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
66 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
67 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
68 |
public final String mangle(CharSequence name, int mtype) { |
14263 | 69 |
StringBuilder result = new StringBuilder(100); |
10 | 70 |
int length = name.length(); |
71 |
||
72 |
for (int i = 0; i < length; i++) { |
|
73 |
char ch = name.charAt(i); |
|
74 |
if (isalnum(ch)) { |
|
75 |
result.append(ch); |
|
76 |
} else if ((ch == '.') && |
|
77 |
mtype == Mangle.Type.CLASS) { |
|
78 |
result.append('_'); |
|
79 |
} else if (( ch == '$') && |
|
80 |
mtype == Mangle.Type.CLASS) { |
|
81 |
result.append('_'); |
|
82 |
result.append('_'); |
|
83 |
} else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) { |
|
84 |
result.append('_'); |
|
85 |
} else if (ch == '_' && mtype == Mangle.Type.CLASS) { |
|
86 |
result.append('_'); |
|
87 |
} else if (mtype == Mangle.Type.JNI) { |
|
88 |
String esc = null; |
|
89 |
if (ch == '_') |
|
90 |
esc = "_1"; |
|
91 |
else if (ch == '.') |
|
92 |
esc = "_"; |
|
93 |
else if (ch == ';') |
|
94 |
esc = "_2"; |
|
95 |
else if (ch == '[') |
|
96 |
esc = "_3"; |
|
97 |
if (esc != null) { |
|
98 |
result.append(esc); |
|
99 |
} else { |
|
100 |
result.append(mangleChar(ch)); |
|
101 |
} |
|
102 |
} else if (mtype == Mangle.Type.SIGNATURE) { |
|
103 |
if (isprint(ch)) { |
|
104 |
result.append(ch); |
|
105 |
} else { |
|
106 |
result.append(mangleChar(ch)); |
|
107 |
} |
|
108 |
} else { |
|
109 |
result.append(mangleChar(ch)); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
return result.toString(); |
|
114 |
} |
|
115 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
116 |
public String mangleMethod(ExecutableElement method, TypeElement clazz, |
6930 | 117 |
int mtype) throws TypeSignature.SignatureException { |
14263 | 118 |
StringBuilder result = new StringBuilder(100); |
10 | 119 |
result.append("Java_"); |
120 |
||
121 |
if (mtype == Mangle.Type.METHOD_JDK_1) { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
122 |
result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS)); |
10 | 123 |
result.append('_'); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
124 |
result.append(mangle(method.getSimpleName(), |
10 | 125 |
Mangle.Type.FIELD)); |
126 |
result.append("_stub"); |
|
127 |
return result.toString(); |
|
128 |
} |
|
129 |
||
130 |
/* JNI */ |
|
131 |
result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI)); |
|
132 |
result.append('_'); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
133 |
result.append(mangle(method.getSimpleName(), |
10 | 134 |
Mangle.Type.JNI)); |
135 |
if (mtype == Mangle.Type.METHOD_JNI_LONG) { |
|
136 |
result.append("__"); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
137 |
String typesig = signature(method); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
138 |
TypeSignature newTypeSig = new TypeSignature(elems); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
139 |
String sig = newTypeSig.getTypeSignature(typesig, method.getReturnType()); |
10 | 140 |
sig = sig.substring(1); |
141 |
sig = sig.substring(0, sig.lastIndexOf(')')); |
|
142 |
sig = sig.replace('/', '.'); |
|
143 |
result.append(mangle(sig, Mangle.Type.JNI)); |
|
144 |
} |
|
145 |
||
146 |
return result.toString(); |
|
147 |
} |
|
148 |
//where |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
149 |
private String getInnerQualifiedName(TypeElement clazz) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
150 |
return elems.getBinaryName(clazz).toString(); |
10 | 151 |
} |
152 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
153 |
public final String mangleChar(char ch) { |
10 | 154 |
String s = Integer.toHexString(ch); |
155 |
int nzeros = 5 - s.length(); |
|
156 |
char[] result = new char[6]; |
|
157 |
result[0] = '_'; |
|
158 |
for (int i = 1; i <= nzeros; i++) |
|
159 |
result[i] = '0'; |
|
160 |
for (int i = nzeros+1, j = 0; i < 6; i++, j++) |
|
161 |
result[i] = s.charAt(j); |
|
162 |
return new String(result); |
|
163 |
} |
|
164 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
165 |
// Warning: duplicated in Gen |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
166 |
private String signature(ExecutableElement e) { |
14263 | 167 |
StringBuilder sb = new StringBuilder(); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
168 |
String sep = "("; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
169 |
for (VariableElement p: e.getParameters()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
170 |
sb.append(sep); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
171 |
sb.append(types.erasure(p.asType()).toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
172 |
sep = ","; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
173 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
174 |
sb.append(")"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
175 |
return sb.toString(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
176 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
177 |
|
10 | 178 |
/* Warning: Intentional ASCII operation. */ |
22163 | 179 |
private static boolean isalnum(char ch) { |
10 | 180 |
return ch <= 0x7f && /* quick test */ |
181 |
((ch >= 'A' && ch <= 'Z') || |
|
182 |
(ch >= 'a' && ch <= 'z') || |
|
183 |
(ch >= '0' && ch <= '9')); |
|
184 |
} |
|
185 |
||
186 |
/* Warning: Intentional ASCII operation. */ |
|
22163 | 187 |
private static boolean isprint(char ch) { |
10 | 188 |
return ch >= 32 && ch <= 126; |
189 |
} |
|
190 |
} |