Skip to content

Commit 3e76fb2

Browse files
committed
docs: add doc for NativeRegistrationHelper
1 parent 6bdee88 commit 3e76fb2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

core-syscall/src/main/java/dev/tmpfs/libcoresyscall/elfloader/NativeRegistrationHelper.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515

1616
import dev.tmpfs.libcoresyscall.core.NativeAccess;
1717

18+
/**
19+
* A helper class for registering native methods in a library.
20+
*/
1821
public class NativeRegistrationHelper {
1922

2023
private NativeRegistrationHelper() {
2124
throw new AssertionError("no instances");
2225
}
2326

27+
/**
28+
* A summary of the registration results.
29+
*/
2430
public static class RegistrationSummary {
2531
// The methods that were successfully registered by this invocation.
2632
public final ArrayList<Method> registeredMethods = new ArrayList<>();
@@ -71,13 +77,31 @@ private static int findAndRegisterNativeMethodInternal(@NonNull NativeLibrarySym
7177
return JNI_NATIVE_REGISTRATION_SUCCESS;
7278
}
7379

80+
/**
81+
* Finds all native methods declared in the given classes and registers them with the specified library.
82+
* <p>
83+
* Note that native methods which are already registered will be skipped and will not be overridden.
84+
*
85+
* @param handle the handle of the library, as returned by {@link DlExtLibraryLoader#dlopen}
86+
* @param klasses the classes to search for native methods
87+
* @return a summary of the registration process
88+
*/
7489
public static RegistrationSummary findAndRegisterNativeMethods(long handle, @NonNull Class<?>[] klasses) {
7590
if (handle == 0) {
7691
throw new IllegalArgumentException("library handle is null");
7792
}
7893
return findAndRegisterNativeMethods(new DefaultNativeLibraryPublicSymbolResolver(handle), klasses);
7994
}
8095

96+
/**
97+
* Finds all native methods declared in the given classes and registers them with the specified library.
98+
* <p>
99+
* Note that native methods which are already registered will be skipped and will not be overridden.
100+
*
101+
* @param resolver the symbol resolver to use to find the native method implementations
102+
* @param klasses the classes to search for native methods
103+
* @return a summary of the registration process
104+
*/
81105
public static RegistrationSummary findAndRegisterNativeMethods(@NonNull NativeLibrarySymbolResolver resolver, @NonNull Class<?>[] klasses) {
82106
RegistrationSummary summary = new RegistrationSummary();
83107
for (Class<?> klass : klasses) {
@@ -120,21 +144,48 @@ public long resolveSymbol(String symbol) {
120144

121145
}
122146

147+
/**
148+
* Gets the short JNI symbol name for the given method.
149+
*
150+
* @param method the method to get the JNI symbol name for
151+
* @return the short JNI symbol name, e.g. "Java_com_example_Foo_bar"
152+
*/
123153
@NonNull
124154
public static String getJniShortName(@NonNull Method method) {
125155
return getJniShortName(method.getDeclaringClass().getName(), method.getName());
126156
}
127157

158+
/**
159+
* Gets the long JNI symbol name for the given method.
160+
*
161+
* @param method the method to get the JNI symbol name for
162+
* @return the long JNI symbol name, e.g. "Java_com_example_Foo_bar__I"
163+
*/
128164
@NonNull
129165
public static String getJniLongName(@NonNull Method method) {
130166
return getJniLongName(method.getDeclaringClass().getName(), method.getName(), method.getParameterTypes());
131167
}
132168

169+
/**
170+
* Gets the short JNI symbol name for the given class and method names.
171+
*
172+
* @param className the class name, e.g. "com.example.Foo"
173+
* @param methodName the method name, e.g. "bar"
174+
* @return the short JNI symbol name, e.g. "Java_com_example_Foo_bar"
175+
*/
133176
@NonNull
134177
public static String getJniShortName(@NonNull String className, @NonNull String methodName) {
135178
return "Java_" + mangleForJni(className) + "_" + mangleForJni(methodName);
136179
}
137180

181+
/**
182+
* Gets the long JNI symbol name for the given class, method, and argument types.
183+
*
184+
* @param className the class name, e.g. "com.example.Foo"
185+
* @param methodName the method name, e.g. "bar"
186+
* @param argTypes the argument types
187+
* @return the long JNI symbol name, e.g. "Java_com_example_Foo_bar__I"
188+
*/
138189
@NonNull
139190
public static String getJniLongName(@NonNull String className, @NonNull String methodName, @NonNull Class<?>[] argTypes) {
140191
StringBuilder sb = new StringBuilder();
@@ -149,6 +200,12 @@ public static String getJniLongName(@NonNull String className, @NonNull String m
149200
return sb.toString();
150201
}
151202

203+
/**
204+
* Get the JNI type signature for the given type.
205+
*
206+
* @param type the type, e.g. int.class or String.class
207+
* @return e.g. "I" for int.class, "Ljava/lang/String;" for String.class
208+
*/
152209
@NonNull
153210
public static String getTypeSignature(@NonNull Class<?> type) {
154211
if (type == Void.TYPE) {

0 commit comments

Comments
 (0)