author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 27852 | langtools/src/jdk.compiler/share/classes/com/sun/tools/javah/Gen.java@2e6ad0e4fe20 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
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 |
package com.sun.tools.javah; |
|
27 |
||
28 |
import java.io.ByteArrayOutputStream; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
29 |
import java.io.FileNotFoundException; |
10 | 30 |
import java.io.IOException; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
31 |
import java.io.InputStream; |
10 | 32 |
import java.io.OutputStream; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
33 |
import java.io.OutputStreamWriter; |
10 | 34 |
import java.io.PrintWriter; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
35 |
import java.io.UnsupportedEncodingException; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
36 |
import java.nio.file.NoSuchFileException; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
37 |
import java.util.ArrayList; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
38 |
import java.util.Arrays; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
39 |
import java.util.List; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
40 |
import java.util.Set; |
10 | 41 |
import java.util.Stack; |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
42 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
43 |
import javax.annotation.processing.ProcessingEnvironment; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
44 |
import javax.lang.model.element.ExecutableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
45 |
import javax.lang.model.element.Modifier; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
46 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
47 |
import javax.lang.model.element.VariableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
48 |
import javax.lang.model.util.ElementFilter; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
49 |
import javax.lang.model.util.Elements; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
50 |
import javax.lang.model.util.Types; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
51 |
import javax.tools.FileObject; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
52 |
import javax.tools.JavaFileManager; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
53 |
import javax.tools.JavaFileObject; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
54 |
import javax.tools.StandardLocation; |
10 | 55 |
|
56 |
/** |
|
57 |
* An abstraction for generating support files required by native methods. |
|
58 |
* Subclasses are for specific native interfaces. At the time of its |
|
59 |
* original writing, this interface is rich enough to support JNI and the |
|
60 |
* old 1.0-style native method interface. |
|
61 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
62 |
* <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:
2212
diff
changeset
|
63 |
* 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:
2212
diff
changeset
|
64 |
* 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:
2212
diff
changeset
|
65 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
66 |
* |
10 | 67 |
* @author Sucheta Dambalkar(Revised) |
68 |
*/ |
|
69 |
public abstract class Gen { |
|
70 |
protected String lineSep = System.getProperty("line.separator"); |
|
71 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
72 |
protected ProcessingEnvironment processingEnvironment; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
73 |
protected Types types; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
74 |
protected Elements elems; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
75 |
protected Mangle mangler; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
76 |
protected Util util; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
77 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
78 |
protected Gen(Util util) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
79 |
this.util = util; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
80 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
81 |
|
10 | 82 |
/* |
83 |
* List of classes for which we must generate output. |
|
84 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
85 |
protected Set<TypeElement> classes; |
10 | 86 |
static private final boolean isWindows = |
87 |
System.getProperty("os.name").startsWith("Windows"); |
|
88 |
||
89 |
||
90 |
/** |
|
91 |
* Override this abstract method, generating content for the named |
|
92 |
* class into the outputstream. |
|
93 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
94 |
protected abstract void write(OutputStream o, TypeElement clazz) throws Util.Exit; |
10 | 95 |
|
96 |
/** |
|
97 |
* Override this method to provide a list of #include statements |
|
98 |
* required by the native interface. |
|
99 |
*/ |
|
100 |
protected abstract String getIncludes(); |
|
101 |
||
102 |
/* |
|
103 |
* Output location. |
|
104 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
105 |
protected JavaFileManager fileManager; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
106 |
protected JavaFileObject outFile; |
10 | 107 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
108 |
public void setFileManager(JavaFileManager fm) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
109 |
fileManager = fm; |
10 | 110 |
} |
111 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
112 |
public void setOutFile(JavaFileObject outFile) { |
10 | 113 |
this.outFile = outFile; |
114 |
} |
|
115 |
||
116 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
117 |
public void setClasses(Set<TypeElement> classes) { |
10 | 118 |
this.classes = classes; |
119 |
} |
|
120 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
121 |
void setProcessingEnvironment(ProcessingEnvironment pEnv) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
122 |
processingEnvironment = pEnv; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
123 |
elems = pEnv.getElementUtils(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
124 |
types = pEnv.getTypeUtils(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
125 |
mangler = new Mangle(elems, types); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
126 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
127 |
|
10 | 128 |
/* |
129 |
* Smartness with generated files. |
|
130 |
*/ |
|
131 |
protected boolean force = false; |
|
132 |
||
133 |
public void setForce(boolean state) { |
|
134 |
force = state; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* We explicitly need to write ASCII files because that is what C |
|
139 |
* compilers understand. |
|
140 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
141 |
protected PrintWriter wrapWriter(OutputStream o) throws Util.Exit { |
10 | 142 |
try { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
143 |
return new PrintWriter(new OutputStreamWriter(o, "ISO8859_1"), true); |
10 | 144 |
} catch (UnsupportedEncodingException use) { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
145 |
util.bug("encoding.iso8859_1.not.found"); |
10 | 146 |
return null; /* dead code */ |
147 |
} |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* After initializing state of an instance, use this method to start |
|
152 |
* processing. |
|
153 |
* |
|
154 |
* Buffer size chosen as an approximation from a single sampling of: |
|
155 |
* expr `du -sk` / `ls *.h | wc -l` |
|
156 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
157 |
public void run() throws IOException, ClassNotFoundException, Util.Exit { |
10 | 158 |
int i = 0; |
159 |
if (outFile != null) { |
|
160 |
/* Everything goes to one big file... */ |
|
161 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(8192); |
|
162 |
writeFileTop(bout); /* only once */ |
|
163 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
164 |
for (TypeElement t: classes) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
165 |
write(bout, t); |
10 | 166 |
} |
167 |
||
168 |
writeIfChanged(bout.toByteArray(), outFile); |
|
169 |
} else { |
|
170 |
/* Each class goes to its own file... */ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
171 |
for (TypeElement t: classes) { |
10 | 172 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(8192); |
173 |
writeFileTop(bout); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
174 |
write(bout, t); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
175 |
writeIfChanged(bout.toByteArray(), getFileObject(t.getQualifiedName())); |
10 | 176 |
} |
177 |
} |
|
178 |
} |
|
179 |
||
180 |
/* |
|
181 |
* Write the contents of byte[] b to a file named file. Writing |
|
182 |
* is done if either the file doesn't exist or if the contents are |
|
183 |
* different. |
|
184 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
185 |
private void writeIfChanged(byte[] b, FileObject file) throws IOException { |
10 | 186 |
boolean mustWrite = false; |
187 |
String event = "[No need to update file "; |
|
188 |
||
189 |
if (force) { |
|
190 |
mustWrite = true; |
|
191 |
event = "[Forcefully writing file "; |
|
192 |
} else { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
193 |
InputStream in; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
194 |
byte[] a; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
195 |
try { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
196 |
// regrettably, there's no API to get the length in bytes |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
197 |
// for a FileObject, so we can't short-circuit reading the |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
198 |
// file here |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
199 |
in = file.openInputStream(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
200 |
a = readBytes(in); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
201 |
if (!Arrays.equals(a, b)) { |
10 | 202 |
mustWrite = true; |
203 |
event = "[Overwriting file "; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
204 |
|
10 | 205 |
} |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
206 |
} catch (FileNotFoundException | NoSuchFileException e) { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
207 |
mustWrite = true; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
208 |
event = "[Creating file "; |
10 | 209 |
} |
210 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
211 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
212 |
if (util.verbose) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
213 |
util.log(event + file + "]"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
214 |
|
10 | 215 |
if (mustWrite) { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
216 |
OutputStream out = file.openOutputStream(); |
10 | 217 |
out.write(b); /* No buffering, just one big write! */ |
218 |
out.close(); |
|
219 |
} |
|
220 |
} |
|
221 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
222 |
protected byte[] readBytes(InputStream in) throws IOException { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
223 |
try { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
224 |
byte[] array = new byte[in.available() + 1]; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
225 |
int offset = 0; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
226 |
int n; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
227 |
while ((n = in.read(array, offset, array.length - offset)) != -1) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
228 |
offset += n; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
229 |
if (offset == array.length) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
230 |
array = Arrays.copyOf(array, array.length * 2); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
231 |
} |
10 | 232 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
233 |
return Arrays.copyOf(array, offset); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
234 |
} finally { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
235 |
in.close(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
236 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
237 |
} |
10 | 238 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
239 |
protected String defineForStatic(TypeElement c, VariableElement f) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
240 |
throws Util.Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
241 |
CharSequence cnamedoc = c.getQualifiedName(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
242 |
CharSequence fnamedoc = f.getSimpleName(); |
10 | 243 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
244 |
String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
245 |
String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
246 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
247 |
if (!f.getModifiers().contains(Modifier.STATIC)) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
248 |
util.bug("tried.to.define.non.static"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
249 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
250 |
if (f.getModifiers().contains(Modifier.FINAL)) { |
10 | 251 |
Object value = null; |
252 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
253 |
value = f.getConstantValue(); |
10 | 254 |
|
255 |
if (value != null) { /* so it is a ConstantExpression */ |
|
256 |
String constString = null; |
|
257 |
if ((value instanceof Integer) |
|
258 |
|| (value instanceof Byte) |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
259 |
|| (value instanceof Short)) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
260 |
/* covers byte, short, int */ |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
261 |
constString = value.toString() + "L"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
262 |
} else if (value instanceof Boolean) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
263 |
constString = ((Boolean) value) ? "1L" : "0L"; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
264 |
} else if (value instanceof Character) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
265 |
Character ch = (Character) value; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
266 |
constString = String.valueOf(((int) ch) & 0xffff) + "L"; |
10 | 267 |
} else if (value instanceof Long) { |
268 |
// Visual C++ supports the i64 suffix, not LL. |
|
269 |
if (isWindows) |
|
270 |
constString = value.toString() + "i64"; |
|
271 |
else |
|
272 |
constString = value.toString() + "LL"; |
|
273 |
} else if (value instanceof Float) { |
|
274 |
/* bug for bug */ |
|
275 |
float fv = ((Float)value).floatValue(); |
|
276 |
if (Float.isInfinite(fv)) |
|
277 |
constString = ((fv < 0) ? "-" : "") + "Inff"; |
|
278 |
else |
|
279 |
constString = value.toString() + "f"; |
|
280 |
} else if (value instanceof Double) { |
|
281 |
/* bug for bug */ |
|
282 |
double d = ((Double)value).doubleValue(); |
|
283 |
if (Double.isInfinite(d)) |
|
284 |
constString = ((d < 0) ? "-" : "") + "InfD"; |
|
285 |
else |
|
286 |
constString = value.toString(); |
|
287 |
} |
|
288 |
if (constString != null) { |
|
14263 | 289 |
StringBuilder s = new StringBuilder("#undef "); |
10 | 290 |
s.append(cname); s.append("_"); s.append(fname); s.append(lineSep); |
291 |
s.append("#define "); s.append(cname); s.append("_"); |
|
292 |
s.append(fname); s.append(" "); s.append(constString); |
|
293 |
return s.toString(); |
|
294 |
} |
|
295 |
||
296 |
} |
|
297 |
} |
|
298 |
return null; |
|
299 |
} |
|
300 |
||
301 |
/* |
|
302 |
* Deal with the C pre-processor. |
|
303 |
*/ |
|
304 |
protected String cppGuardBegin() { |
|
305 |
return "#ifdef __cplusplus" + lineSep + "extern \"C\" {" + lineSep + "#endif"; |
|
306 |
} |
|
307 |
||
308 |
protected String cppGuardEnd() { |
|
309 |
return "#ifdef __cplusplus" + lineSep + "}" + lineSep + "#endif"; |
|
310 |
} |
|
311 |
||
312 |
protected String guardBegin(String cname) { |
|
313 |
return "/* Header for class " + cname + " */" + lineSep + lineSep + |
|
314 |
"#ifndef _Included_" + cname + lineSep + |
|
315 |
"#define _Included_" + cname; |
|
316 |
} |
|
317 |
||
318 |
protected String guardEnd(String cname) { |
|
319 |
return "#endif"; |
|
320 |
} |
|
321 |
||
322 |
/* |
|
323 |
* File name and file preamble related operations. |
|
324 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
325 |
protected void writeFileTop(OutputStream o) throws Util.Exit { |
10 | 326 |
PrintWriter pw = wrapWriter(o); |
327 |
pw.println("/* DO NOT EDIT THIS FILE - it is machine generated */" + lineSep + |
|
328 |
getIncludes()); |
|
329 |
} |
|
330 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
331 |
protected String baseFileName(CharSequence className) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
332 |
return mangler.mangle(className, Mangle.Type.CLASS); |
10 | 333 |
} |
334 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
335 |
protected FileObject getFileObject(CharSequence className) throws IOException { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
336 |
String name = baseFileName(className) + getFileSuffix(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
337 |
return fileManager.getFileForOutput(StandardLocation.SOURCE_OUTPUT, "", name, null); |
10 | 338 |
} |
339 |
||
340 |
protected String getFileSuffix() { |
|
341 |
return ".h"; |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Including super classes' fields. |
|
346 |
*/ |
|
347 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
348 |
List<VariableElement> getAllFields(TypeElement subclazz) { |
22163 | 349 |
List<VariableElement> fields = new ArrayList<>(); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
350 |
TypeElement cd = null; |
22163 | 351 |
Stack<TypeElement> s = new Stack<>(); |
10 | 352 |
|
353 |
cd = subclazz; |
|
354 |
while (true) { |
|
355 |
s.push(cd); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
356 |
TypeElement c = (TypeElement) (types.asElement(cd.getSuperclass())); |
10 | 357 |
if (c == null) |
358 |
break; |
|
359 |
cd = c; |
|
360 |
} |
|
361 |
||
362 |
while (!s.empty()) { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
363 |
cd = s.pop(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
364 |
fields.addAll(ElementFilter.fieldsIn(cd.getEnclosedElements())); |
10 | 365 |
} |
366 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
367 |
return fields; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
368 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
369 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
370 |
// c.f. MethodDoc.signature |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
371 |
String signature(ExecutableElement e) { |
14263 | 372 |
StringBuilder sb = new StringBuilder("("); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
373 |
String sep = ""; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
374 |
for (VariableElement p: e.getParameters()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
375 |
sb.append(sep); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
376 |
sb.append(types.erasure(p.asType()).toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
377 |
sep = ","; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
378 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
379 |
sb.append(")"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
380 |
return sb.toString(); |
10 | 381 |
} |
382 |
} |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
2212
diff
changeset
|
383 |