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