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