-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindowsApiRun.java
More file actions
380 lines (336 loc) · 11.5 KB
/
WindowsApiRun.java
File metadata and controls
380 lines (336 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// Windows API Generator for Java
// Copyright (c) 2025 Manuel Bleichenbacher
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//
package net.codecrete.windowsapi;
import net.codecrete.windowsapi.events.Event;
import net.codecrete.windowsapi.events.EventListener;
import net.codecrete.windowsapi.winmd.MetadataBuilder;
import net.codecrete.windowsapi.writer.CodeWriter;
import net.codecrete.windowsapi.writer.GenerationException;
import net.codecrete.windowsapi.writer.Scope;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* A single run to generate Java source code for accessing the Windows API.
* <p>
* To generate code, create an instance of this class, set the configuration arguments
* and call {@link #generateCode()}.
* </p>
* <p>
* The generated source code uses the Java Foreign Function and Memory API (FFM)
* and requires Java 23 or higher.
* </p>
*
* @see
* <a href="https://docs.oracle.com/en/java/javase/23/core/foreign-function-and-memory-api.html">Foreign Function and Memory API</a>
*/
public class WindowsApiRun {
private Path outputDirectory;
private String basePackage = "";
private EventListener eventListener = new NullEventListener();
private Set<String> structs = new LinkedHashSet<>();
private Set<String> functions = new LinkedHashSet<>();
private Set<String> enumerations = new LinkedHashSet<>();
private Set<String> callbackFunctions = new LinkedHashSet<>();
private Set<String> comInterfaces = new LinkedHashSet<>();
private Set<String> constants = new LinkedHashSet<>();
/**
* Creates a new instance.
*/
public WindowsApiRun() {
// default constructor
}
/**
* Gets the directory for generating the source code.
* <p>
* The specified directory is the root directory for packages and subpackages.
* </p>
*
* @return the directory
*/
public Path getOutputDirectory() {
return outputDirectory;
}
/**
* Sets the directory for generating the source code.
* <p>
* The specified directory is the root directory for packages and subpackages.
* </p>
*
* @param outputDirectory the directory
*/
public void setOutputDirectory(Path outputDirectory) {
this.outputDirectory = outputDirectory;
}
/**
* Gets the base package for the generated code.
* <p>
* The generated code will have package names starting with this base package.
* The Windows API data structures and functions are assigned to packages such
* as {@code windows.win32.ui.windowsandmessaging}. The base package is prepended.
* The default is an empty string, i.e., no additional package name.
* </p>
*
* @return the base package name
*/
public String getBasePackage() {
return basePackage;
}
/**
* Sets the base package for the generated code.
* <p>
* The generated code will have package names starting with this base package.
* The Windows API data structures and functions are assigned to packages such
* as {@code windows.win32.ui.windowsandmessaging}. The base package is prepended.
* A valid package name looks like {@code com.company.product}.
* The default is an empty string, i.e., no additional package name.
* </p>
*
* @param basePackage base package name
*/
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
/**
* Gets the names of the C struct and union types to generate.
*
* @return struct/union names
*/
public Set<String> getStructs() {
return structs;
}
/**
* Sets the names of the C struct and union types to generate.
*
* @param structs the struct/union names
*/
public void setStructs(Set<String> structs) {
this.structs = structs;
}
/**
* Gets the names of the Windows API functions to generate.
*
* @return the function names
*/
public Set<String> getFunctions() {
return functions;
}
/**
* Sets the names of the Windows API functions to generate.
* <p>
* Note that if an ANSI and Unicode version of a function exists, the exact version must be specified.
* ANSI versions usually end with the uppercase A while Unicode versions end in the uppercase W.
* </p>
*
* @param functions the function names
*/
public void setFunctions(Set<String> functions) {
this.functions = functions;
}
/**
* Gets the names of the enumerations to generate.
*
* @return the enumeration names
*/
public Set<String> getEnumerations() {
return enumerations;
}
/**
* Sets the names of the enumerations to generate.
*
* @param enumerations the enumeration names
*/
public void setEnumerations(Set<String> enumerations) {
this.enumerations = enumerations;
}
/**
* Gets the names of the callback functions (function pointers) to generate.
*
* @return the callback function names
*/
public Set<String> getCallbackFunctions() {
return callbackFunctions;
}
/**
* Sets the names of the callback functions (function pointers) to generate.
*
* @param callbackFunctions the callback function names
*/
public void setCallbackFunctions(Set<String> callbackFunctions) {
this.callbackFunctions = callbackFunctions;
}
/**
* Gets the names of the COM interfaces to generate.
*
* @return the COM interface names
*/
public Set<String> getComInterfaces() {
return comInterfaces;
}
/**
* Sets the names of the COM interfaces to generate.
*
* @param comInterfaces the COM interface names
*/
public void setComInterfaces(Set<String> comInterfaces) {
this.comInterfaces = comInterfaces;
}
/**
* Gets the names of the constants to generate.
*
* @return the constant names
*/
public Set<String> getConstants() {
return constants;
}
/**
* Sets the names of the constants to generate.
*
* @param constants the constant names
*/
public void setConstants(Set<String> constants) {
this.constants = constants;
}
/**
* Gets the event listener.
* <p>
* The event listener is notified about events such as code generation progress or validation errors.
* </p>
*
* @return the event listener
*/
public EventListener getEventListener() {
return eventListener;
}
/**
* Sets the event listener.
* <p>
* The event listener is notified about events such as code generation progress or validation errors.
* </p>
*
* @param eventListener the event listener
*/
public void setEventListener(EventListener eventListener) {
this.eventListener = eventListener;
}
/**
* Generates the code.
*/
public void generateCode() {
generate(false);
}
/**
* Executes a dry run.
* <p>
* A dry run does not write or delete any files or directories.
* </p>
*/
public void dryRun() {
generate(true);
}
private void generate(boolean isDryRun) {
if (!isAnyWork())
return;
var metadata = MetadataBuilder.load();
var scope = new Scope(metadata, eventListener);
scope.addStructs(structs);
scope.addEnums(enumerations);
scope.addFunctions(functions);
scope.addCallbackFunctions(callbackFunctions);
scope.addComInterfaces(comInterfaces);
scope.addConstants(constants);
if (scope.hasInvalidArguments())
throw new WindowsApiException("Invalid arguments specified for Windows API code generation");
scope.buildTransitiveScope();
var writer = new CodeWriter(metadata, outputDirectory, eventListener);
writer.setDryRun(isDryRun);
writer.setBasePackage(basePackage);
writer.write(scope);
var generatedFiles = writer.getGeneratedFiles();
if (generatedFiles != null)
deleteOldFiles(generatedFiles);
}
private boolean isAnyWork() {
return !functions.isEmpty() || !structs.isEmpty() || !constants.isEmpty()
|| !enumerations.isEmpty() || !callbackFunctions.isEmpty() || !comInterfaces.isEmpty();
}
/**
* Creates the specified directory.
*
* @param path path of directory
*/
public void createDirectory(Path path) {
var directory = path.toFile();
if (directory.exists())
return;
if (!directory.mkdirs())
throw new GenerationException("Unable to create directory " + path);
eventListener.onEvent(new Event.DirectoryCreated(path));
}
/**
* Deletes all files in the output directory except the specified ones.
* <p>
* Empty directories are also deleted.
* </p>
*
* @param generatedFiles set of paths (relative to output directory)
*/
void deleteOldFiles(Set<Path> generatedFiles) {
try {
Files.walkFileTree(outputDirectory, new FileVisitor<>() {
private final Deque<Boolean> isEmptyDirectoryStack = new ArrayDeque<>();
private boolean isEmptyDirectory = true;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
isEmptyDirectoryStack.push(isEmptyDirectory);
isEmptyDirectory = true;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
var relative = outputDirectory.relativize(file);
if (generatedFiles.contains(relative)) {
isEmptyDirectory = false;
} else {
Files.delete(file);
eventListener.onEvent(new Event.FileDeleted(file));
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
throw new UncheckedIOException("Unable to delete old files in output directory " + outputDirectory, exc);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (isEmptyDirectory && !dir.equals(outputDirectory)) {
Files.delete(dir);
eventListener.onEvent(new Event.DirectoryDeleted(dir));
}
isEmptyDirectory = isEmptyDirectoryStack.pop() && isEmptyDirectory;
return FileVisitResult.CONTINUE;
}
});
} catch (IOException exc) {
throw new UncheckedIOException("Unable to clean output directory " + outputDirectory, exc);
}
}
static class NullEventListener implements EventListener {
@Override
public void onEvent(Event event) {
// no output
}
}
}