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