author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 7681 | 1f0819a3341f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
7681 | 2 |
* Copyright (c) 2002, 2010, 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 |
package com.sun.tools.javah; |
|
27 |
||
28 |
import java.io.OutputStream; |
|
29 |
import java.io.PrintWriter; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
30 |
import java.util.ArrayList; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
31 |
import java.util.List; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
32 |
import javax.lang.model.element.Element; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
33 |
import javax.lang.model.element.ExecutableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
34 |
import javax.lang.model.element.Modifier; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
35 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
36 |
import javax.lang.model.element.VariableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
37 |
import javax.lang.model.type.ArrayType; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
38 |
import javax.lang.model.type.TypeMirror; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
39 |
import javax.lang.model.util.ElementFilter; |
10 | 40 |
|
41 |
||
42 |
/** |
|
43 |
* Header file generator for JNI. |
|
44 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
45 |
* <p><b>This is NOT part of any supported API. |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
46 |
* If you write code that depends on this, you do so at your own |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
47 |
* risk. This code and its internal interfaces are subject to change |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
48 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
49 |
* |
10 | 50 |
* @author Sucheta Dambalkar(Revised) |
51 |
*/ |
|
52 |
public class JNI extends Gen { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
53 |
JNI(Util util) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
54 |
super(util); |
10 | 55 |
} |
56 |
||
57 |
public String getIncludes() { |
|
58 |
return "#include <jni.h>"; |
|
59 |
} |
|
60 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
61 |
public void write(OutputStream o, TypeElement clazz) throws Util.Exit { |
6930 | 62 |
try { |
63 |
String cname = mangler.mangle(clazz.getQualifiedName(), Mangle.Type.CLASS); |
|
64 |
PrintWriter pw = wrapWriter(o); |
|
65 |
pw.println(guardBegin(cname)); |
|
66 |
pw.println(cppGuardBegin()); |
|
10 | 67 |
|
6930 | 68 |
/* Write statics. */ |
69 |
List<VariableElement> classfields = getAllFields(clazz); |
|
10 | 70 |
|
6930 | 71 |
for (VariableElement v: classfields) { |
72 |
if (!v.getModifiers().contains(Modifier.STATIC)) |
|
73 |
continue; |
|
74 |
String s = null; |
|
75 |
s = defineForStatic(clazz, v); |
|
76 |
if (s != null) { |
|
77 |
pw.println(s); |
|
78 |
} |
|
10 | 79 |
} |
80 |
||
6930 | 81 |
/* Write methods. */ |
82 |
List<ExecutableElement> classmethods = ElementFilter.methodsIn(clazz.getEnclosedElements()); |
|
83 |
for (ExecutableElement md: classmethods) { |
|
84 |
if(md.getModifiers().contains(Modifier.NATIVE)){ |
|
85 |
TypeMirror mtr = types.erasure(md.getReturnType()); |
|
86 |
String sig = signature(md); |
|
87 |
TypeSignature newtypesig = new TypeSignature(elems); |
|
88 |
CharSequence methodName = md.getSimpleName(); |
|
89 |
boolean longName = false; |
|
90 |
for (ExecutableElement md2: classmethods) { |
|
91 |
if ((md2 != md) |
|
92 |
&& (methodName.equals(md2.getSimpleName())) |
|
93 |
&& (md2.getModifiers().contains(Modifier.NATIVE))) |
|
94 |
longName = true; |
|
10 | 95 |
|
6930 | 96 |
} |
97 |
pw.println("/*"); |
|
98 |
pw.println(" * Class: " + cname); |
|
99 |
pw.println(" * Method: " + |
|
100 |
mangler.mangle(methodName, Mangle.Type.FIELDSTUB)); |
|
101 |
pw.println(" * Signature: " + newtypesig.getTypeSignature(sig, mtr)); |
|
102 |
pw.println(" */"); |
|
103 |
pw.println("JNIEXPORT " + jniType(mtr) + |
|
104 |
" JNICALL " + |
|
105 |
mangler.mangleMethod(md, clazz, |
|
106 |
(longName) ? |
|
107 |
Mangle.Type.METHOD_JNI_LONG : |
|
108 |
Mangle.Type.METHOD_JNI_SHORT)); |
|
109 |
pw.print(" (JNIEnv *, "); |
|
110 |
List<? extends VariableElement> paramargs = md.getParameters(); |
|
22163 | 111 |
List<TypeMirror> args = new ArrayList<>(); |
6930 | 112 |
for (VariableElement p: paramargs) { |
113 |
args.add(types.erasure(p.asType())); |
|
114 |
} |
|
115 |
if (md.getModifiers().contains(Modifier.STATIC)) |
|
116 |
pw.print("jclass"); |
|
117 |
else |
|
118 |
pw.print("jobject"); |
|
119 |
||
120 |
for (TypeMirror arg: args) { |
|
121 |
pw.print(", "); |
|
122 |
pw.print(jniType(arg)); |
|
123 |
} |
|
124 |
pw.println(");" + lineSep); |
|
10 | 125 |
} |
126 |
} |
|
6930 | 127 |
pw.println(cppGuardEnd()); |
128 |
pw.println(guardEnd(cname)); |
|
129 |
} catch (TypeSignature.SignatureException e) { |
|
130 |
util.error("jni.sigerror", e.getMessage()); |
|
10 | 131 |
} |
132 |
} |
|
133 |
||
134 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
135 |
protected final String jniType(TypeMirror t) throws Util.Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
136 |
TypeElement throwable = elems.getTypeElement("java.lang.Throwable"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
137 |
TypeElement jClass = elems.getTypeElement("java.lang.Class"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
138 |
TypeElement jString = elems.getTypeElement("java.lang.String"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
139 |
Element tclassDoc = types.asElement(t); |
10 | 140 |
|
141 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
142 |
switch (t.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
143 |
case ARRAY: { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
144 |
TypeMirror ct = ((ArrayType) t).getComponentType(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
145 |
switch (ct.getKind()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
146 |
case BOOLEAN: return "jbooleanArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
147 |
case BYTE: return "jbyteArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
148 |
case CHAR: return "jcharArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
149 |
case SHORT: return "jshortArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
150 |
case INT: return "jintArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
151 |
case LONG: return "jlongArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
152 |
case FLOAT: return "jfloatArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
153 |
case DOUBLE: return "jdoubleArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
154 |
case ARRAY: |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
155 |
case DECLARED: return "jobjectArray"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
156 |
default: throw new Error(ct.toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
157 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
158 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
159 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
160 |
case VOID: return "void"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
161 |
case BOOLEAN: return "jboolean"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
162 |
case BYTE: return "jbyte"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
163 |
case CHAR: return "jchar"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
164 |
case SHORT: return "jshort"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
165 |
case INT: return "jint"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
166 |
case LONG: return "jlong"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
167 |
case FLOAT: return "jfloat"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
168 |
case DOUBLE: return "jdouble"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
169 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
170 |
case DECLARED: { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
171 |
if (tclassDoc.equals(jString)) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
172 |
return "jstring"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
173 |
else if (types.isAssignable(t, throwable.asType())) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
174 |
return "jthrowable"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
175 |
else if (types.isAssignable(t, jClass.asType())) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
176 |
return "jclass"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
177 |
else |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
178 |
return "jobject"; |
10 | 179 |
} |
180 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
181 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
182 |
util.bug("jni.unknown.type"); |
10 | 183 |
return null; /* dead code. */ |
184 |
} |
|
185 |
} |