11/*
2- * Copyright 2022 the original author or authors.
2+ * Copyright 2022-present the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2020import java .util .stream .Stream ;
2121
2222import org .jspecify .annotations .Nullable ;
23- import org .springframework .aot .hint .ExecutableMode ;
2423import org .springframework .aot .hint .MemberCategory ;
2524import org .springframework .aot .hint .ProxyHints ;
2625import org .springframework .aot .hint .ReflectionHints ;
2726import org .springframework .aot .hint .ResourceHints ;
2827import org .springframework .aot .hint .RuntimeHints ;
2928import org .springframework .aot .hint .RuntimeHintsRegistrar ;
30- import org .springframework .aot .hint .TypeHint ;
3129import org .springframework .aot .hint .TypeReference ;
3230
3331/**
3432 * @author Piotr Olaszewski
33+ * @author Mahmoud Ben Hassine
3534 */
3635public class JnaRuntimeHints implements RuntimeHintsRegistrar {
3736
38- // TODO move JLine related hints to spring-shell-jline module
39-
4037 @ Override
4138 public void registerHints (RuntimeHints hints , @ Nullable ClassLoader classLoader ) {
4239 ResourceHints resource = hints .resources ();
4340 ProxyHints proxy = hints .proxies ();
4441 ReflectionHints reflection = hints .reflection ();
45- ReflectionHints jni = hints .jni ();
4642 // should go graalvm-reachability-metadata
4743 registerResources (resource );
4844 // should go graalvm-reachability-metadata
49- registerProxies (proxy , "com.sun.jna.Library" , "com.sun.jna.Callback" ,
50- "org.jline.terminal.impl.jna.win.Kernel32" , "org.jline.terminal.impl.jna.linux.CLibrary" );
45+ registerProxies (proxy , "com.sun.jna.Library" , "com.sun.jna.Callback" );
5146 // should go graalvm-reachability-metadata
5247 registerForMostReflection (reflection , "com.sun.jna.CallbackReference" , "com.sun.jna.Native" ,
5348 "com.sun.jna.NativeLong" , "com.sun.jna.Pointer" , "com.sun.jna.Structure" ,
5449 "com.sun.jna.ptr.IntByReference" , "com.sun.jna.ptr.PointerByReference" , "com.sun.jna.Klass" ,
5550 "com.sun.jna.Structure$FFIType" , "com.sun.jna.Structure$FFIType$size_t" );
56- // should go graalvm-reachability-metadata
57- registerForMostReflection (reflection , "org.jline.terminal.impl.jna.win.Kernel32$CHAR_INFO" ,
58- "org.jline.terminal.impl.jna.win.Kernel32$CONSOLE_CURSOR_INFO" ,
59- "org.jline.terminal.impl.jna.win.Kernel32$CONSOLE_SCREEN_BUFFER_INFO" ,
60- "org.jline.terminal.impl.jna.win.Kernel32$COORD" ,
61- "org.jline.terminal.impl.jna.win.Kernel32$INPUT_RECORD" ,
62- "org.jline.terminal.impl.jna.win.Kernel32$INPUT_RECORD$EventUnion" ,
63- "org.jline.terminal.impl.jna.win.Kernel32$KEY_EVENT_RECORD" ,
64- "org.jline.terminal.impl.jna.win.Kernel32$MOUSE_EVENT_RECORD" ,
65- "org.jline.terminal.impl.jna.win.Kernel32$WINDOW_BUFFER_SIZE_RECORD" ,
66- "org.jline.terminal.impl.jna.win.Kernel32$MENU_EVENT_RECORD" ,
67- "org.jline.terminal.impl.jna.win.Kernel32$FOCUS_EVENT_RECORD" ,
68- "org.jline.terminal.impl.jna.win.Kernel32$SMALL_RECT" ,
69- "org.jline.terminal.impl.jna.win.Kernel32$UnionChar" );
70-
71- registerForMostReflection (reflection , "org.jline.terminal.impl.jna.linux.CLibrary$termios" ,
72- "org.jline.terminal.impl.jna.linux.CLibrary$winsize" );
73-
74- registerJni (jni );
7551 }
7652
7753 private void registerResources (ResourceHints resource ) {
@@ -96,174 +72,4 @@ private List<TypeReference> typeReferences(String... classNames) {
9672 return Stream .of (classNames ).map (TypeReference ::of ).collect (Collectors .toList ());
9773 }
9874
99- private void registerJni (ReflectionHints jni ) {
100- TypeReference reachableType = TypeReference .of ("org.jline.terminal.impl.jna.win.Kernel32" );
101-
102- registerForJni (jni , reachableType , "com.sun.jna.Callback" , Methods .of (), Fields .of ());
103-
104- registerForJni (jni , reachableType , "com.sun.jna.CallbackReference" ,
105- Methods
106- .of (Method .of ("getCallback" , "java.lang.Class" , "com.sun.jna.Pointer" , "boolean" ),
107- Method .of ("getFunctionPointer" , "com.sun.jna.Callback" , "boolean" ),
108- Method .of ("getNativeString" , "java.lang.Object" , "boolean" ), Method .of ("initializeThread" ,
109- "com.sun.jna.Callback" , "com.sun.jna.CallbackReference$AttachOptions" )),
110- Fields .of ());
111-
112- registerForJni (jni , reachableType , "com.sun.jna.CallbackReference$AttachOptions" , Methods .of (), Fields .of ());
113-
114- registerForJni (jni , reachableType , "com.sun.jna.FromNativeConverter" , Methods .of (Method .of ("nativeType" )),
115- Fields .of ());
116-
117- registerForJni (jni , reachableType , "com.sun.jna.IntegerType" , Methods .of (), Fields .of ("value" ));
118-
119- registerForJni (jni , reachableType , "com.sun.jna.JNIEnv" , Methods .of (), Fields .of ());
120-
121- registerForJni (jni , reachableType , "com.sun.jna.Native" , Methods .of (Method .of ("dispose" ),
122- Method .of ("fromNative" , "com.sun.jna.FromNativeConverter" , "java.lang.Object" ,
123- "java.lang.reflect.Method" ),
124- Method .of ("fromNative" , "java.lang.Class" , "java.lang.Object" ),
125- Method .of ("fromNative" , "java.lang.reflect.Method" , "java.lang.Object" ),
126- Method .of ("nativeType" , "java.lang.Class" ),
127- Method .of ("toNative" , "com.sun.jna.ToNativeConverter" , "java.lang.Object" )), Fields .of ());
128-
129- registerForJni (jni , reachableType , "com.sun.jna.Native$ffi_callback" ,
130- Methods .of (Method .of ("invoke" , "long" , "long" , "long" )), Fields .of ());
131-
132- registerForJni (jni , reachableType , "com.sun.jna.NativeMapped" , Methods .of (Method .of ("toNative" )), Fields .of ());
133-
134- registerForJni (jni , reachableType , "com.sun.jna.Pointer" , Methods .of (Method .of ("<init>" , "long" )),
135- Fields .of ("peer" ));
136-
137- registerForJni (jni , reachableType , "com.sun.jna.PointerType" , Methods .of (), Fields .of ("pointer" ));
138-
139- registerForJni (
140- jni , reachableType , "com.sun.jna.Structure" , Methods .of (Method .of ("autoRead" ), Method .of ("autoWrite" ),
141- Method .of ("getTypeInfo" ), Method .of ("newInstance" , "java.lang.Class" , "long" )),
142- Fields .of ("memory" , "typeInfo" ));
143-
144- registerForJni (jni , reachableType , "com.sun.jna.Structure$ByValue" , Methods .of (), Fields .of ());
145-
146- registerForJni (jni , reachableType , "com.sun.jna.Structure$FFIType$FFITypes" , Methods .of (),
147- Fields .of ("ffi_type_double" , "ffi_type_float" , "ffi_type_longdouble" , "ffi_type_pointer" ,
148- "ffi_type_sint16" , "ffi_type_sint32" , "ffi_type_sint64" , "ffi_type_sint8" , "ffi_type_uint16" ,
149- "ffi_type_uint32" , "ffi_type_uint64" , "ffi_type_uint8" , "ffi_type_void" ));
150-
151- registerForJni (jni , reachableType , "com.sun.jna.WString" , Methods .of (Method .of ("<init>" , "java.lang.String" )),
152- Fields .of ());
153-
154- registerForJni (jni , reachableType , "java.lang.Boolean" , Methods .of (Method .of ("<init>" , "boolean" )),
155- Fields .of ("TYPE" , "value" ));
156-
157- registerForJni (jni , reachableType , "java.lang.Byte" , Methods .of (Method .of ("<init>" , "byte" )),
158- Fields .of ("TYPE" , "value" ));
159-
160- registerForJni (jni , reachableType , "java.lang.Character" , Methods .of (Method .of ("<init>" , "char" )),
161- Fields .of ("TYPE" , "value" ));
162-
163- registerForJni (jni , reachableType , "java.lang.Class" , Methods .of (Method .of ("getComponentType" )), Fields .of ());
164-
165- registerForJni (jni , reachableType , "java.lang.Double" , Methods .of (Method .of ("<init>" , "double" )),
166- Fields .of ("TYPE" , "value" ));
167-
168- registerForJni (jni , reachableType , "java.lang.Float" , Methods .of (Method .of ("<init>" , "float" )),
169- Fields .of ("TYPE" , "value" ));
170-
171- registerForJni (jni , reachableType , "java.lang.Integer" , Methods .of (Method .of ("<init>" , "int" )),
172- Fields .of ("TYPE" , "value" ));
173-
174- registerForJni (jni , reachableType , "java.lang.Long" , Methods .of (Method .of ("<init>" , "long" )),
175- Fields .of ("TYPE" , "value" ));
176-
177- registerForJni (jni , reachableType , "java.lang.Object" , Methods .of (Method .of ("toString" )), Fields .of ());
178-
179- registerForJni (jni , reachableType , "java.lang.Short" , Methods .of (Method .of ("<init>" , "short" )),
180- Fields .of ("TYPE" , "value" ));
181-
182- registerForJni (jni , reachableType , "java.lang.String" ,
183- Methods .of (Method .of ("<init>" , "byte[]" ), Method .of ("<init>" , "byte[]" , "java.lang.String" ),
184- Method .of ("getBytes" ), Method .of ("getBytes" , "java.lang.String" ), Method .of ("toCharArray" )),
185- Fields .of ());
186-
187- registerForJni (jni , reachableType , "java.lang.System" , Methods .of (Method .of ("getProperty" , "java.lang.String" )),
188- Fields .of ());
189-
190- registerForJni (jni , reachableType , "java.lang.UnsatisfiedLinkError" ,
191- Methods .of (Method .of ("<init>" , "java.lang.String" )), Fields .of ());
192-
193- registerForJni (jni , reachableType , "java.lang.Void" , Methods .of (), Fields .of ("TYPE" ));
194-
195- registerForJni (jni , reachableType , "java.lang.reflect.Method" ,
196- Methods .of (Method .of ("getParameterTypes" ), Method .of ("getReturnType" )), Fields .of ());
197-
198- registerForJni (jni , reachableType , "java.nio.Buffer" , Methods .of (Method .of ("position" )), Fields .of ());
199-
200- registerForJni (jni , reachableType , "java.nio.ByteBuffer" ,
201- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
202-
203- registerForJni (jni , reachableType , "java.nio.CharBuffer" ,
204- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
205-
206- registerForJni (jni , reachableType , "java.nio.DoubleBuffer" ,
207- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
208-
209- registerForJni (jni , reachableType , "java.nio.FloatBuffer" ,
210- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
211-
212- registerForJni (jni , reachableType , "java.nio.IntBuffer" ,
213- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
214-
215- registerForJni (jni , reachableType , "java.nio.LongBuffer" ,
216- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
217-
218- registerForJni (jni , reachableType , "java.nio.ShortBuffer" ,
219- Methods .of (Method .of ("array" ), Method .of ("arrayOffset" )), Fields .of ());
220-
221- }
222-
223- private void registerForJni (ReflectionHints jni , TypeReference reachableType , String type , Methods methods ,
224- Fields fields ) {
225- jni .registerType (TypeReference .of (type ), hint -> {
226- hint .onReachableType (reachableType );
227- methods .withHints (hint );
228- fields .withHints (hint );
229- });
230- }
231-
232- record Fields (String ... names ) {
233-
234- static Fields of (String ... names ) {
235- return new Fields (names );
236- }
237-
238- void withHints (TypeHint .Builder hint ) {
239- Stream .of (names ()).forEach (f -> hint .withField (f ));
240- }
241- }
242-
243- record Method (String name , String ... parameterTypes ) {
244-
245- static Method of (String name , String ... parameterTypes ) {
246- return new Method (name , parameterTypes );
247- }
248-
249- private List <TypeReference > asTypeReferences () {
250- return Stream .of (parameterTypes ).map (TypeReference ::of ).collect (Collectors .toList ());
251- }
252-
253- void withHints (TypeHint .Builder hint ) {
254- hint .withMethod (name , asTypeReferences (), ExecutableMode .INVOKE );
255- }
256- }
257-
258- record Methods (Method ... methods ) {
259-
260- static Methods of (Method ... methods ) {
261- return new Methods (methods );
262- }
263-
264- void withHints (TypeHint .Builder hint ) {
265- Stream .of (methods ()).forEach (m -> m .withHints (hint ));
266- }
267- }
268-
26975}
0 commit comments