@@ -320,16 +320,10 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeGetNodeNames(
320320 }
321321}
322322
323- JNIEXPORT void JNICALL
324- Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes (
325- JNIEnv * env, jclass, jlong handle , jobject jnames_and_types)
323+ void
324+ fill_jnames_and_types (
325+ JNIEnv * env, const rcl_names_and_types_t & names_and_types , jobject jnames_and_types)
326326{
327- rcl_node_t * node = reinterpret_cast <rcl_node_t *>(handle);
328- if (!node) {
329- rcljava_throw_exception (env, " java/lang/IllegalArgumentException" , " node handle is NULL" );
330- return ;
331- }
332-
333327 jclass collection_clazz = env->FindClass (" java/util/Collection" );
334328 jmethodID collection_add_mid = env->GetMethodID (
335329 collection_clazz, " add" , " (Ljava/lang/Object;)Z" );
@@ -343,34 +337,16 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes(
343337 jfieldID types_fid = env->GetFieldID (name_and_types_clazz, " types" , " Ljava/util/Collection;" );
344338 RCLJAVA_COMMON_CHECK_FOR_EXCEPTION (env);
345339
346- rcl_allocator_t allocator = rcl_get_default_allocator ();
347- rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types ();
348-
349- rcl_ret_t ret = rcl_get_topic_names_and_types (
350- node,
351- &allocator,
352- false ,
353- &topic_names_and_types);
354- RCLJAVA_COMMON_THROW_FROM_RCL (env, ret, " failed to get topic names and types" );
355- auto cleanup_names_and_types = rcpputils::make_scope_exit (
356- [pnames_and_types = &topic_names_and_types, env]() {
357- rcl_ret_t ret = rcl_names_and_types_fini (pnames_and_types);
358- if (!env->ExceptionCheck () && RCL_RET_OK != ret) {
359- rcljava_throw_rclexception (env, ret, " failed to fini topic names and types structure" );
360- }
361- }
362- );
363-
364- for (size_t i = 0 ; i < topic_names_and_types.names .size ; i++) {
340+ for (size_t i = 0 ; i < names_and_types.names .size ; i++) {
365341 jobject jitem = env->NewObject (name_and_types_clazz, name_and_types_init_mid);
366342 RCLJAVA_COMMON_CHECK_FOR_EXCEPTION (env);
367- jstring jname = env->NewStringUTF (topic_names_and_types .names .data [i]);
343+ jstring jname = env->NewStringUTF (names_and_types .names .data [i]);
368344 RCLJAVA_COMMON_CHECK_FOR_EXCEPTION (env);
369345 env->SetObjectField (jitem, name_fid, jname);
370346 // the default constructor already inits types to an empty ArrayList
371347 jobject jtypes = env->GetObjectField (jitem, types_fid);
372- for (size_t j = 0 ; j < topic_names_and_types .types [i].size ; j++) {
373- jstring jtype = env->NewStringUTF (topic_names_and_types .types [i].data [j]);
348+ for (size_t j = 0 ; j < names_and_types .types [i].size ; j++) {
349+ jstring jtype = env->NewStringUTF (names_and_types .types [i].data [j]);
374350 env->CallBooleanMethod (jtypes, collection_add_mid, jtype);
375351 RCLJAVA_COMMON_CHECK_FOR_EXCEPTION (env);
376352 }
@@ -379,6 +355,59 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes(
379355 }
380356}
381357
358+ JNIEXPORT void JNICALL
359+ Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes (
360+ JNIEnv * env, jclass, jlong handle, jobject jnames_and_types)
361+ {
362+ rcl_node_t * node = reinterpret_cast <rcl_node_t *>(handle);
363+ if (!node) {
364+ rcljava_throw_exception (env, " java/lang/IllegalArgumentException" , " node handle is NULL" );
365+ return ;
366+ }
367+
368+ rcl_allocator_t allocator = rcl_get_default_allocator ();
369+ rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types ();
370+
371+ rcl_ret_t ret = rcl_get_topic_names_and_types (
372+ node,
373+ &allocator,
374+ false ,
375+ &topic_names_and_types);
376+ RCLJAVA_COMMON_THROW_FROM_RCL (env, ret, " failed to get topic names and types" );
377+ fill_jnames_and_types (env, topic_names_and_types, jnames_and_types);
378+
379+ ret = rcl_names_and_types_fini (&topic_names_and_types);
380+ if (!env->ExceptionCheck () && RCL_RET_OK != ret) {
381+ rcljava_throw_rclexception (env, ret, " failed to fini topic names and types structure" );
382+ }
383+ }
384+
385+ JNIEXPORT void JNICALL
386+ Java_org_ros2_rcljava_node_NodeImpl_nativeGetServiceNamesAndTypes (
387+ JNIEnv * env, jclass, jlong handle, jobject jnames_and_types)
388+ {
389+ rcl_node_t * node = reinterpret_cast <rcl_node_t *>(handle);
390+ if (!node) {
391+ rcljava_throw_exception (env, " java/lang/IllegalArgumentException" , " node handle is NULL" );
392+ return ;
393+ }
394+
395+ rcl_allocator_t allocator = rcl_get_default_allocator ();
396+ rcl_names_and_types_t service_names_and_types = rcl_get_zero_initialized_names_and_types ();
397+
398+ rcl_ret_t ret = rcl_get_service_names_and_types (
399+ node,
400+ &allocator,
401+ &service_names_and_types);
402+ RCLJAVA_COMMON_THROW_FROM_RCL (env, ret, " failed to get service names and types" );
403+ fill_jnames_and_types (env, service_names_and_types, jnames_and_types);
404+
405+ ret = rcl_names_and_types_fini (&service_names_and_types);
406+ if (!env->ExceptionCheck () && RCL_RET_OK != ret) {
407+ rcljava_throw_rclexception (env, ret, " failed to fini service names and types structure" );
408+ }
409+ }
410+
382411template <typename FunctorT>
383412void
384413get_endpoint_info_common (
0 commit comments