author | jjg |
Tue, 12 Oct 2010 13:19:47 -0700 | |
changeset 6930 | b6fea484cbb2 |
parent 5847 | 1908176fd6e3 |
child 7681 | 1f0819a3341f |
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.io.OutputStream; |
|
30 |
import java.io.PrintWriter; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
31 |
import java.util.ArrayList; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
32 |
import java.util.HashSet; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
33 |
import java.util.List; |
10 | 34 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
35 |
import java.util.Set; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
36 |
import javax.lang.model.element.Element; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
37 |
import javax.lang.model.element.ExecutableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
38 |
import javax.lang.model.element.Modifier; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
39 |
import javax.lang.model.element.Name; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
40 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
41 |
import javax.lang.model.element.VariableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
42 |
import javax.lang.model.type.ArrayType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
43 |
import javax.lang.model.type.PrimitiveType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
44 |
import javax.lang.model.type.TypeKind; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
45 |
import javax.lang.model.type.TypeMirror; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
46 |
import javax.lang.model.type.TypeVisitor; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
47 |
import javax.lang.model.util.ElementFilter; |
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
48 |
import javax.lang.model.util.SimpleTypeVisitor7; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
49 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
50 |
/* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5841
diff
changeset
|
51 |
* <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
|
52 |
* 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
|
53 |
* 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
|
54 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
55 |
* |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
56 |
* @author Sucheta Dambalkar(Revised) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
57 |
*/ |
10 | 58 |
public class LLNI extends Gen { |
59 |
||
60 |
protected final char innerDelim = '$'; /* For inner classes */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
61 |
protected Set<String> doneHandleTypes; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
62 |
List<VariableElement> fields; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
63 |
List<ExecutableElement> methods; |
10 | 64 |
private boolean doubleAlign; |
65 |
private int padFieldNum = 0; |
|
66 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
67 |
LLNI(boolean doubleAlign, Util util) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
68 |
super(util); |
10 | 69 |
this.doubleAlign = doubleAlign; |
70 |
} |
|
71 |
||
72 |
protected String getIncludes() { |
|
73 |
return ""; |
|
74 |
} |
|
75 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
76 |
protected void write(OutputStream o, TypeElement clazz) throws Util.Exit { |
6930 | 77 |
try { |
78 |
String cname = mangleClassName(clazz.getQualifiedName().toString()); |
|
79 |
PrintWriter pw = wrapWriter(o); |
|
80 |
fields = ElementFilter.fieldsIn(clazz.getEnclosedElements()); |
|
81 |
methods = ElementFilter.methodsIn(clazz.getEnclosedElements()); |
|
82 |
generateDeclsForClass(pw, clazz, cname); |
|
83 |
// FIXME check if errors occurred on the PrintWriter and throw exception if so |
|
84 |
} catch (TypeSignature.SignatureException e) { |
|
85 |
util.error("llni.sigerror", e.getMessage()); |
|
86 |
} |
|
10 | 87 |
} |
88 |
||
89 |
protected void generateDeclsForClass(PrintWriter pw, |
|
6930 | 90 |
TypeElement clazz, String cname) |
91 |
throws TypeSignature.SignatureException, Util.Exit { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
92 |
doneHandleTypes = new HashSet<String>(); |
10 | 93 |
/* The following handle types are predefined in "typedefs.h". Suppress |
94 |
inclusion in the output by generating them "into the blue" here. */ |
|
95 |
genHandleType(null, "java.lang.Class"); |
|
96 |
genHandleType(null, "java.lang.ClassLoader"); |
|
97 |
genHandleType(null, "java.lang.Object"); |
|
98 |
genHandleType(null, "java.lang.String"); |
|
99 |
genHandleType(null, "java.lang.Thread"); |
|
100 |
genHandleType(null, "java.lang.ThreadGroup"); |
|
101 |
genHandleType(null, "java.lang.Throwable"); |
|
102 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
103 |
pw.println("/* LLNI Header for class " + clazz.getQualifiedName() + " */" + lineSep); |
10 | 104 |
pw.println("#ifndef _Included_" + cname); |
105 |
pw.println("#define _Included_" + cname); |
|
106 |
pw.println("#include \"typedefs.h\""); |
|
107 |
pw.println("#include \"llni.h\""); |
|
108 |
pw.println("#include \"jni.h\"" + lineSep); |
|
109 |
||
110 |
forwardDecls(pw, clazz); |
|
111 |
structSectionForClass(pw, clazz, cname); |
|
112 |
methodSectionForClass(pw, clazz, cname); |
|
113 |
pw.println("#endif"); |
|
114 |
} |
|
115 |
||
116 |
protected void genHandleType(PrintWriter pw, String clazzname) { |
|
117 |
String cname = mangleClassName(clazzname); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
118 |
if (!doneHandleTypes.contains(cname)) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
119 |
doneHandleTypes.add(cname); |
10 | 120 |
if (pw != null) { |
121 |
pw.println("#ifndef DEFINED_" + cname); |
|
122 |
pw.println(" #define DEFINED_" + cname); |
|
123 |
pw.println(" GEN_HANDLE_TYPES(" + cname + ");"); |
|
124 |
pw.println("#endif" + lineSep); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
protected String mangleClassName(String s) { |
|
130 |
return s.replace('.', '_') |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
131 |
.replace('/', '_') |
10 | 132 |
.replace(innerDelim, '_'); |
133 |
} |
|
134 |
||
6930 | 135 |
protected void forwardDecls(PrintWriter pw, TypeElement clazz) |
136 |
throws TypeSignature.SignatureException { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
137 |
TypeElement object = elems.getTypeElement("java.lang.Object"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
138 |
if (clazz.equals(object)) |
10 | 139 |
return; |
140 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
141 |
genHandleType(pw, clazz.getQualifiedName().toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
142 |
TypeElement superClass = (TypeElement) (types.asElement(clazz.getSuperclass())); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
143 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
144 |
if (superClass != null) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
145 |
String superClassName = superClass.getQualifiedName().toString(); |
10 | 146 |
forwardDecls(pw, superClass); |
147 |
} |
|
148 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
149 |
for (VariableElement field: fields) { |
10 | 150 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
151 |
if (!field.getModifiers().contains(Modifier.STATIC)) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
152 |
TypeMirror t = types.erasure(field.asType()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
153 |
TypeSignature newTypeSig = new TypeSignature(elems); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
154 |
String tname = newTypeSig.qualifiedTypeName(t); |
10 | 155 |
String sig = newTypeSig.getTypeSignature(tname); |
156 |
||
157 |
if (sig.charAt(0) != '[') |
|
158 |
forwardDeclsFromSig(pw, sig); |
|
159 |
} |
|
160 |
} |
|
161 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
162 |
for (ExecutableElement method: methods) { |
10 | 163 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
164 |
if (method.getModifiers().contains(Modifier.NATIVE)) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
165 |
TypeMirror retType = types.erasure(method.getReturnType()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
166 |
String typesig = signature(method); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
167 |
TypeSignature newTypeSig = new TypeSignature(elems); |
10 | 168 |
String sig = newTypeSig.getTypeSignature(typesig, retType); |
169 |
||
170 |
if (sig.charAt(0) != '[') |
|
171 |
forwardDeclsFromSig(pw, sig); |
|
172 |
||
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
protected void forwardDeclsFromSig(PrintWriter pw, String sig) { |
|
178 |
int len = sig.length(); |
|
179 |
int i = sig.charAt(0) == '(' ? 1 : 0; |
|
180 |
||
181 |
/* Skip the initial "(". */ |
|
182 |
while (i < len) { |
|
183 |
if (sig.charAt(i) == 'L') { |
|
184 |
int j = i + 1; |
|
185 |
while (sig.charAt(j) != ';') j++; |
|
186 |
genHandleType(pw, sig.substring(i + 1, j)); |
|
187 |
i = j + 1; |
|
188 |
} else { |
|
189 |
i++; |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
protected void structSectionForClass(PrintWriter pw, |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
195 |
TypeElement jclazz, String cname) { |
10 | 196 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
197 |
String jname = jclazz.getQualifiedName().toString(); |
10 | 198 |
|
199 |
if (cname.equals("java_lang_Object")) { |
|
200 |
pw.println("/* struct java_lang_Object is defined in typedefs.h. */"); |
|
201 |
pw.println(); |
|
202 |
return; |
|
203 |
} |
|
204 |
pw.println("#if !defined(__i386)"); |
|
205 |
pw.println("#pragma pack(4)"); |
|
206 |
pw.println("#endif"); |
|
207 |
pw.println(); |
|
208 |
pw.println("struct " + cname + " {"); |
|
209 |
pw.println(" ObjHeader h;"); |
|
210 |
pw.print(fieldDefs(jclazz, cname)); |
|
211 |
||
212 |
if (jname.equals("java.lang.Class")) |
|
213 |
pw.println(" Class *LLNI_mask(cClass);" + |
|
214 |
" /* Fake field; don't access (see oobj.h) */"); |
|
215 |
pw.println("};" + lineSep + lineSep + "#pragma pack()"); |
|
216 |
pw.println(); |
|
217 |
return; |
|
218 |
} |
|
219 |
||
220 |
private static class FieldDefsRes { |
|
221 |
public String className; /* Name of the current class. */ |
|
222 |
public FieldDefsRes parent; |
|
223 |
public String s; |
|
224 |
public int byteSize; |
|
225 |
public boolean bottomMost; |
|
226 |
public boolean printedOne = false; |
|
227 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
228 |
FieldDefsRes(TypeElement clazz, FieldDefsRes parent, boolean bottomMost) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
229 |
this.className = clazz.getQualifiedName().toString(); |
10 | 230 |
this.parent = parent; |
231 |
this.bottomMost = bottomMost; |
|
232 |
int byteSize = 0; |
|
233 |
if (parent == null) this.s = ""; |
|
234 |
else this.s = parent.s; |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
/* Returns "true" iff added a field. */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
239 |
private boolean doField(FieldDefsRes res, VariableElement field, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
240 |
String cname, boolean padWord) { |
10 | 241 |
|
242 |
String fieldDef = addStructMember(field, cname, padWord); |
|
243 |
if (fieldDef != null) { |
|
244 |
if (!res.printedOne) { /* add separator */ |
|
245 |
if (res.bottomMost) { |
|
246 |
if (res.s.length() != 0) |
|
247 |
res.s = res.s + " /* local members: */" + lineSep; |
|
248 |
} else { |
|
249 |
res.s = res.s + " /* inherited members from " + |
|
250 |
res.className + ": */" + lineSep; |
|
251 |
} |
|
252 |
res.printedOne = true; |
|
253 |
} |
|
254 |
res.s = res.s + fieldDef; |
|
255 |
return true; |
|
256 |
} |
|
257 |
||
258 |
// Otherwise. |
|
259 |
return false; |
|
260 |
} |
|
261 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
262 |
private int doTwoWordFields(FieldDefsRes res, TypeElement clazz, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
263 |
int offset, String cname, boolean padWord) { |
10 | 264 |
boolean first = true; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
265 |
List<VariableElement> fields = ElementFilter.fieldsIn(clazz.getEnclosedElements()); |
10 | 266 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
267 |
for (VariableElement field: fields) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
268 |
TypeKind tk = field.asType().getKind(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
269 |
boolean twoWords = (tk == TypeKind.LONG || tk == TypeKind.DOUBLE); |
10 | 270 |
if (twoWords && doField(res, field, cname, first && padWord)) { |
271 |
offset += 8; first = false; |
|
272 |
} |
|
273 |
} |
|
274 |
return offset; |
|
275 |
} |
|
276 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
277 |
String fieldDefs(TypeElement clazz, String cname) { |
10 | 278 |
FieldDefsRes res = fieldDefs(clazz, cname, true); |
279 |
return res.s; |
|
280 |
} |
|
281 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
282 |
FieldDefsRes fieldDefs(TypeElement clazz, String cname, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
283 |
boolean bottomMost){ |
10 | 284 |
FieldDefsRes res; |
285 |
int offset; |
|
286 |
boolean didTwoWordFields = false; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
287 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
288 |
TypeElement superclazz = (TypeElement) types.asElement(clazz.getSuperclass()); |
10 | 289 |
|
290 |
if (superclazz != null) { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
291 |
String supername = superclazz.getQualifiedName().toString(); |
10 | 292 |
res = new FieldDefsRes(clazz, |
293 |
fieldDefs(superclazz, cname, false), |
|
294 |
bottomMost); |
|
295 |
offset = res.parent.byteSize; |
|
296 |
} else { |
|
297 |
res = new FieldDefsRes(clazz, null, bottomMost); |
|
298 |
offset = 0; |
|
299 |
} |
|
300 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
301 |
List<VariableElement> fields = ElementFilter.fieldsIn(clazz.getEnclosedElements()); |
10 | 302 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
303 |
for (VariableElement field: fields) { |
10 | 304 |
|
305 |
if (doubleAlign && !didTwoWordFields && (offset % 8) == 0) { |
|
306 |
offset = doTwoWordFields(res, clazz, offset, cname, false); |
|
307 |
didTwoWordFields = true; |
|
308 |
} |
|
309 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
310 |
TypeKind tk = field.asType().getKind(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
311 |
boolean twoWords = (tk == TypeKind.LONG || tk == TypeKind.DOUBLE); |
10 | 312 |
|
313 |
if (!doubleAlign || !twoWords) { |
|
314 |
if (doField(res, field, cname, false)) offset += 4; |
|
315 |
} |
|
316 |
||
317 |
} |
|
318 |
||
319 |
if (doubleAlign && !didTwoWordFields) { |
|
320 |
if ((offset % 8) != 0) offset += 4; |
|
321 |
offset = doTwoWordFields(res, clazz, offset, cname, true); |
|
322 |
} |
|
323 |
||
324 |
res.byteSize = offset; |
|
325 |
return res; |
|
326 |
} |
|
327 |
||
328 |
/* OVERRIDE: This method handles instance fields */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
329 |
protected String addStructMember(VariableElement member, String cname, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
330 |
boolean padWord) { |
10 | 331 |
String res = null; |
332 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
333 |
if (member.getModifiers().contains(Modifier.STATIC)) { |
10 | 334 |
res = addStaticStructMember(member, cname); |
335 |
// if (res == null) /* JNI didn't handle it, print comment. */ |
|
336 |
// res = " /* Inaccessible static: " + member + " */" + lineSep; |
|
337 |
} else { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
338 |
TypeMirror mt = types.erasure(member.asType()); |
10 | 339 |
if (padWord) res = " java_int padWord" + padFieldNum++ + ";" + lineSep; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
340 |
res = " " + llniType(mt, false, false) + " " + llniFieldName(member); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
341 |
if (isLongOrDouble(mt)) res = res + "[2]"; |
10 | 342 |
res = res + ";" + lineSep; |
343 |
} |
|
344 |
return res; |
|
345 |
} |
|
346 |
||
347 |
static private final boolean isWindows = |
|
348 |
System.getProperty("os.name").startsWith("Windows"); |
|
349 |
||
350 |
/* |
|
351 |
* This method only handles static final fields. |
|
352 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
353 |
protected String addStaticStructMember(VariableElement field, String cname) { |
10 | 354 |
String res = null; |
355 |
Object exp = null; |
|
356 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
357 |
if (!field.getModifiers().contains(Modifier.STATIC)) |
10 | 358 |
return res; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
359 |
if (!field.getModifiers().contains(Modifier.FINAL)) |
10 | 360 |
return res; |
361 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
362 |
exp = field.getConstantValue(); |
10 | 363 |
|
364 |
if (exp != null) { |
|
365 |
/* Constant. */ |
|
366 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
367 |
String cn = cname + "_" + field.getSimpleName(); |
10 | 368 |
String suffix = null; |
369 |
long val = 0; |
|
370 |
/* Can only handle int, long, float, and double fields. */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
371 |
if (exp instanceof Byte |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
372 |
|| exp instanceof Short |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
373 |
|| exp instanceof Integer) { |
10 | 374 |
suffix = "L"; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
375 |
val = ((Number)exp).intValue(); |
10 | 376 |
} |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
377 |
else if (exp instanceof Long) { |
10 | 378 |
// Visual C++ supports the i64 suffix, not LL |
379 |
suffix = isWindows ? "i64" : "LL"; |
|
380 |
val = ((Long)exp).longValue(); |
|
381 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
382 |
else if (exp instanceof Float) suffix = "f"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
383 |
else if (exp instanceof Double) suffix = ""; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
384 |
else if (exp instanceof Character) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
385 |
suffix = "L"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
386 |
Character ch = (Character) exp; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
387 |
val = ((int) ch) & 0xffff; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
388 |
} |
10 | 389 |
if (suffix != null) { |
390 |
// Some compilers will generate a spurious warning |
|
391 |
// for the integer constants for Integer.MIN_VALUE |
|
392 |
// and Long.MIN_VALUE so we handle them specially. |
|
393 |
if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) || |
|
394 |
(suffix.equals("LL") && (val == Long.MIN_VALUE))) { |
|
395 |
res = " #undef " + cn + lineSep |
|
396 |
+ " #define " + cn |
|
397 |
+ " (" + (val + 1) + suffix + "-1)" + lineSep; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
398 |
} else if (suffix.equals("L") || suffix.endsWith("LL")) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
399 |
res = " #undef " + cn + lineSep |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
400 |
+ " #define " + cn + " " + val + suffix + lineSep; |
10 | 401 |
} else { |
402 |
res = " #undef " + cn + lineSep |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
403 |
+ " #define " + cn + " " + exp + suffix + lineSep; |
10 | 404 |
} |
405 |
} |
|
406 |
} |
|
407 |
return res; |
|
408 |
} |
|
409 |
||
410 |
protected void methodSectionForClass(PrintWriter pw, |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
411 |
TypeElement clazz, String cname) |
6930 | 412 |
throws TypeSignature.SignatureException, Util.Exit { |
10 | 413 |
String methods = methodDecls(clazz, cname); |
414 |
||
415 |
if (methods.length() != 0) { |
|
416 |
pw.println("/* Native method declarations: */" + lineSep); |
|
417 |
pw.println("#ifdef __cplusplus"); |
|
418 |
pw.println("extern \"C\" {"); |
|
419 |
pw.println("#endif" + lineSep); |
|
420 |
pw.println(methods); |
|
421 |
pw.println("#ifdef __cplusplus"); |
|
422 |
pw.println("}"); |
|
423 |
pw.println("#endif"); |
|
424 |
} |
|
425 |
} |
|
426 |
||
6930 | 427 |
protected String methodDecls(TypeElement clazz, String cname) |
428 |
throws TypeSignature.SignatureException, Util.Exit { |
|
10 | 429 |
|
430 |
String res = ""; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
431 |
for (ExecutableElement method: methods) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
432 |
if (method.getModifiers().contains(Modifier.NATIVE)) |
10 | 433 |
res = res + methodDecl(method, clazz, cname); |
434 |
} |
|
435 |
return res; |
|
436 |
} |
|
437 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
438 |
protected String methodDecl(ExecutableElement method, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
439 |
TypeElement clazz, String cname) |
6930 | 440 |
throws TypeSignature.SignatureException, Util.Exit { |
10 | 441 |
String res = null; |
442 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
443 |
TypeMirror retType = types.erasure(method.getReturnType()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
444 |
String typesig = signature(method); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
445 |
TypeSignature newTypeSig = new TypeSignature(elems); |
10 | 446 |
String sig = newTypeSig.getTypeSignature(typesig, retType); |
447 |
boolean longName = needLongName(method, clazz); |
|
448 |
||
449 |
if (sig.charAt(0) != '(') |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
450 |
util.error("invalid.method.signature", sig); |
10 | 451 |
|
452 |
||
453 |
res = "JNIEXPORT " + jniType(retType) + " JNICALL" + lineSep + jniMethodName(method, cname, longName) |
|
454 |
+ "(JNIEnv *, " + cRcvrDecl(method, cname); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
455 |
List<? extends VariableElement> params = method.getParameters(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
456 |
List<TypeMirror> argTypes = new ArrayList<TypeMirror>(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
457 |
for (VariableElement p: params){ |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
458 |
argTypes.add(types.erasure(p.asType())); |
10 | 459 |
} |
460 |
||
461 |
/* It would have been nice to include the argument names in the |
|
462 |
declaration, but there seems to be a bug in the "BinaryField" |
|
463 |
class, causing the getArguments() method to return "null" for |
|
464 |
most (non-constructor) methods. */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
465 |
for (TypeMirror argType: argTypes) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
466 |
res = res + ", " + jniType(argType); |
10 | 467 |
res = res + ");" + lineSep; |
468 |
return res; |
|
469 |
} |
|
470 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
471 |
protected final boolean needLongName(ExecutableElement method, |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
472 |
TypeElement clazz) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
473 |
Name methodName = method.getSimpleName(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
474 |
for (ExecutableElement memberMethod: methods) { |
10 | 475 |
if ((memberMethod != method) && |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
476 |
memberMethod.getModifiers().contains(Modifier.NATIVE) && |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
477 |
(methodName.equals(memberMethod.getSimpleName()))) |
10 | 478 |
return true; |
479 |
} |
|
480 |
return false; |
|
481 |
} |
|
482 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
483 |
protected final String jniMethodName(ExecutableElement method, String cname, |
6930 | 484 |
boolean longName) |
485 |
throws TypeSignature.SignatureException { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
486 |
String res = "Java_" + cname + "_" + method.getSimpleName(); |
10 | 487 |
|
488 |
if (longName) { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
489 |
TypeMirror mType = types.erasure(method.getReturnType()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
490 |
List<? extends VariableElement> params = method.getParameters(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
491 |
List<TypeMirror> argTypes = new ArrayList<TypeMirror>(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
492 |
for (VariableElement param: params) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
493 |
argTypes.add(types.erasure(param.asType())); |
10 | 494 |
} |
495 |
||
496 |
res = res + "__"; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
497 |
for (TypeMirror t: argTypes) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
498 |
String tname = t.toString(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
499 |
TypeSignature newTypeSig = new TypeSignature(elems); |
10 | 500 |
String sig = newTypeSig.getTypeSignature(tname); |
501 |
res = res + nameToIdentifier(sig); |
|
502 |
} |
|
503 |
} |
|
504 |
return res; |
|
505 |
} |
|
506 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
507 |
// copied from JNI.java |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
508 |
protected final String jniType(TypeMirror t) throws Util.Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
509 |
TypeElement throwable = elems.getTypeElement("java.lang.Throwable"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
510 |
TypeElement jClass = elems.getTypeElement("java.lang.Class"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
511 |
TypeElement jString = elems.getTypeElement("java.lang.String"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
512 |
Element tclassDoc = types.asElement(t); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
513 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
514 |
switch (t.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
515 |
case ARRAY: { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
516 |
TypeMirror ct = ((ArrayType) t).getComponentType(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
517 |
switch (ct.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
518 |
case BOOLEAN: return "jbooleanArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
519 |
case BYTE: return "jbyteArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
520 |
case CHAR: return "jcharArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
521 |
case SHORT: return "jshortArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
522 |
case INT: return "jintArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
523 |
case LONG: return "jlongArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
524 |
case FLOAT: return "jfloatArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
525 |
case DOUBLE: return "jdoubleArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
526 |
case ARRAY: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
527 |
case DECLARED: return "jobjectArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
528 |
default: throw new Error(ct.toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
529 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
530 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
531 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
532 |
case VOID: return "void"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
533 |
case BOOLEAN: return "jboolean"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
534 |
case BYTE: return "jbyte"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
535 |
case CHAR: return "jchar"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
536 |
case SHORT: return "jshort"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
537 |
case INT: return "jint"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
538 |
case LONG: return "jlong"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
539 |
case FLOAT: return "jfloat"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
540 |
case DOUBLE: return "jdouble"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
541 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
542 |
case DECLARED: { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
543 |
if (tclassDoc.equals(jString)) |
10 | 544 |
return "jstring"; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
545 |
else if (types.isAssignable(t, throwable.asType())) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
546 |
return "jthrowable"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
547 |
else if (types.isAssignable(t, jClass.asType())) |
10 | 548 |
return "jclass"; |
549 |
else |
|
550 |
return "jobject"; |
|
551 |
} |
|
552 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
553 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
554 |
util.bug("jni.unknown.type"); |
10 | 555 |
return null; /* dead code. */ |
556 |
} |
|
557 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
558 |
protected String llniType(TypeMirror t, boolean handleize, boolean longDoubleOK) { |
10 | 559 |
String res = null; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
560 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
561 |
switch (t.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
562 |
case ARRAY: { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
563 |
TypeMirror ct = ((ArrayType) t).getComponentType(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
564 |
switch (ct.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
565 |
case BOOLEAN: res = "IArrayOfBoolean"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
566 |
case BYTE: res = "IArrayOfByte"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
567 |
case CHAR: res = "IArrayOfChar"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
568 |
case SHORT: res = "IArrayOfShort"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
569 |
case INT: res = "IArrayOfInt"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
570 |
case LONG: res = "IArrayOfLong"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
571 |
case FLOAT: res = "IArrayOfFloat"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
572 |
case DOUBLE: res = "IArrayOfDouble"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
573 |
case ARRAY: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
574 |
case DECLARED: res = "IArrayOfRef"; break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
575 |
default: throw new Error(ct.getKind() + " " + ct); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
576 |
} |
10 | 577 |
if (!handleize) res = "DEREFERENCED_" + res; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
578 |
break; |
10 | 579 |
} |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
580 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
581 |
case VOID: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
582 |
res = "void"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
583 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
584 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
585 |
case BOOLEAN: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
586 |
case BYTE: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
587 |
case CHAR: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
588 |
case SHORT: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
589 |
case INT: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
590 |
res = "java_int" ; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
591 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
592 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
593 |
case LONG: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
594 |
res = longDoubleOK ? "java_long" : "val32 /* java_long */"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
595 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
596 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
597 |
case FLOAT: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
598 |
res = "java_float"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
599 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
600 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
601 |
case DOUBLE: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
602 |
res = longDoubleOK ? "java_double" : "val32 /* java_double */"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
603 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
604 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
605 |
case DECLARED: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
606 |
TypeElement e = (TypeElement) types.asElement(t); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
607 |
res = "I" + mangleClassName(e.getQualifiedName().toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
608 |
if (!handleize) res = "DEREFERENCED_" + res; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
609 |
break; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
610 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
611 |
default: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
612 |
throw new Error(t.getKind() + " " + t); // FIXME |
10 | 613 |
} |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
614 |
|
10 | 615 |
return res; |
616 |
} |
|
617 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
618 |
protected final String cRcvrDecl(Element field, String cname) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
619 |
return (field.getModifiers().contains(Modifier.STATIC) ? "jclass" : "jobject"); |
10 | 620 |
} |
621 |
||
622 |
protected String maskName(String s) { |
|
623 |
return "LLNI_mask(" + s + ")"; |
|
624 |
} |
|
625 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
626 |
protected String llniFieldName(VariableElement field) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
627 |
return maskName(field.getSimpleName().toString()); |
10 | 628 |
} |
629 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
630 |
protected final boolean isLongOrDouble(TypeMirror t) { |
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
631 |
TypeVisitor<Boolean,Void> v = new SimpleTypeVisitor7<Boolean,Void>() { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
632 |
public Boolean defaultAction(TypeMirror t, Void p){ |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
633 |
return false; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
634 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
635 |
public Boolean visitArray(ArrayType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
636 |
return visit(t.getComponentType(), p); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
637 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
638 |
public Boolean visitPrimitive(PrimitiveType t, Void p) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
639 |
TypeKind tk = t.getKind(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
640 |
return (tk == TypeKind.LONG || tk == TypeKind.DOUBLE); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
641 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
642 |
}; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
643 |
return v.visit(t, null); |
10 | 644 |
} |
645 |
||
646 |
/* Do unicode to ansi C identifier conversion. |
|
647 |
%%% This may not be right, but should be called more often. */ |
|
648 |
protected final String nameToIdentifier(String name) { |
|
649 |
int len = name.length(); |
|
650 |
StringBuffer buf = new StringBuffer(len); |
|
651 |
for (int i = 0; i < len; i++) { |
|
652 |
char c = name.charAt(i); |
|
653 |
if (isASCIILetterOrDigit(c)) |
|
654 |
buf.append(c); |
|
655 |
else if (c == '/') |
|
656 |
buf.append('_'); |
|
657 |
else if (c == '.') |
|
658 |
buf.append('_'); |
|
659 |
else if (c == '_') |
|
660 |
buf.append("_1"); |
|
661 |
else if (c == ';') |
|
662 |
buf.append("_2"); |
|
663 |
else if (c == '[') |
|
664 |
buf.append("_3"); |
|
665 |
else |
|
666 |
buf.append("_0" + ((int)c)); |
|
667 |
} |
|
668 |
return new String(buf); |
|
669 |
} |
|
670 |
||
671 |
protected final boolean isASCIILetterOrDigit(char c) { |
|
672 |
if (((c >= 'A') && (c <= 'Z')) || |
|
673 |
((c >= 'a') && (c <= 'z')) || |
|
674 |
((c >= '0') && (c <= '9'))) |
|
675 |
return true; |
|
676 |
else |
|
677 |
return false; |
|
678 |
} |
|
679 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
1789
diff
changeset
|
680 |