-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathAbstractJNAFeature.java
209 lines (193 loc) · 6.72 KB
/
AbstractJNAFeature.java
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
/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.hosted.RuntimeProxyCreation;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* Provides common logic for JNA-related GraalVM feature classes.
*
* <p>These classes should only be included at build time for a `native-image` target.</p>
*
* @since 5.15.0
* @author Sam Gammon ([email protected])
* @author Dario Valdespino ([email protected])
*/
abstract class AbstractJNAFeature implements Feature {
/**
* Obtain a reference to a method on a class, in order to register it for reflective access
*
* @param clazz Class to obtain method reference from
* @param methodName Name of the method to obtain a reference to
* @param args Method arguments
* @return Method reference
*/
protected static Method method(Class<?> clazz, String methodName, Class<?>... args) {
try {
return clazz.getDeclaredMethod(methodName, args);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
/**
* Obtain a reference to one or more fields on a class, in order to register them for reflective access
*
* @param clazz Class to obtain field references from
* @param fieldNames Names of the fields to obtain references to
* @return Field references
*/
protected static Field[] fields(Class<?> clazz, String... fieldNames) {
try {
Field[] fields = new Field[fieldNames.length];
for (int i = 0; i < fieldNames.length; i++) {
fields[i] = clazz.getDeclaredField(fieldNames[i]);
}
return fields;
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}
}
/**
* Register a class for reflective access at runtime
*
* @param clazz Class to register
*/
protected static void reflectiveClass(Class<?>... clazz) {
RuntimeReflection.register(clazz);
}
/**
* Register a resource for use in the final image
*
* @param module Module which owns the resource
* @param resource Path to the resource to register
*/
protected static void registerResource(Module module, String resource) {
RuntimeResourceAccess.addResource(module, resource);
}
/**
* Register a resource for use in the final image
*
* @param resource Path to the resource to register
*/
protected static void registerResource(String resource) {
registerResource(AbstractJNAFeature.class.getModule(), resource);
}
/**
* Register a class for JNI access at runtime
*
* @param clazz Class to register
*/
protected static void registerJniClass(Class<?> clazz) {
RuntimeJNIAccess.register(clazz);
Arrays.stream(clazz.getConstructors()).forEach(RuntimeJNIAccess::register);
Arrays.stream(clazz.getMethods()).forEach(RuntimeJNIAccess::register);
}
/**
* Register a class for JNI access at runtime, potentially with reflective access as well
*
* @param clazz Class to register
* @param reflective Whether to register the class and constructors for reflective access
*/
protected static void registerJniClass(Class<?> clazz, Boolean reflective) {
registerJniClass(clazz);
if (reflective) {
RuntimeReflection.register(clazz);
RuntimeReflection.registerAllConstructors(clazz);
}
}
/**
* Register a suite of JNA methods for use at runtime
*
* @param reflective Whether to register the methods for reflective access
* @param methods Methods to register
*/
protected static void registerJniMethods(Boolean reflective, Method... methods) {
RuntimeJNIAccess.register(methods);
if (reflective) {
RuntimeReflection.register(methods);
}
}
/**
* Register a suite of JNA methods for use at runtime
*
* @param methods Methods to register
*/
protected static void registerJniMethods(Method... methods) {
registerJniMethods(false, methods);
}
/**
* Register a suite of JNA fields for use at runtime
*
* @param reflective Whether to register the fields for reflective access
* @param fields Fields to register
*/
protected static void registerJniFields(Boolean reflective, Field[] fields) {
RuntimeJNIAccess.register(fields);
if (reflective) {
RuntimeReflection.register(fields);
}
}
/**
* Register a suite of JNA fields for use at runtime
*
* @param fields Fields to register
*/
protected static void registerJniFields(Field[] fields) {
registerJniFields(false, fields);
}
/**
* Register a combination of interfaces used at runtime as a dynamic proxy object
*
* @param classes Combination of interface classes; order matters
*/
protected static void registerProxyInterfaces(Class<?>... classes) {
RuntimeProxyCreation.register(classes);
}
/**
* Assign the specified class or classes to initialize at image build time
*
* @param clazz Classes to register for build-time initialization
*/
protected static void initializeAtBuildTime(Class<?>... clazz) {
for (Class<?> c : clazz) {
RuntimeClassInitialization.initializeAtBuildTime(c);
}
}
/**
* Assign the specified class or classes to initialize at image run-time
*
* @param clazz Classes to register for run-time initialization
*/
protected static void initializeAtRunTime(Class<?>... clazz) {
for (Class<?> c : clazz) {
RuntimeClassInitialization.initializeAtRunTime(c);
}
}
}