<commit_before># ----------------------------------------------------------------------------
#  CMake file for java support
# ----------------------------------------------------------------------------
if(IOS OR NOT PYTHON_EXECUTABLE OR NOT ANT_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)))
  ocv_module_disable(java)
endif()

set(the_description "The java bindings")
ocv_add_module(java BINDINGS opencv_core opencv_imgproc OPTIONAL opencv_objdetect opencv_features2d opencv_video opencv_highgui opencv_ml opencv_calib3d opencv_photo opencv_nonfree opencv_contrib)
ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp")

if(NOT ANDROID)
  include_directories(${JNI_INCLUDE_DIRS})
endif()

# output locations
set(JAVA_INSTALL_ROOT "sdk/java")
set(JNI_INSTALL_ROOT  "sdk/native")

# get list of modules to wrap
string(REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}")
foreach(module ${OPENCV_JAVA_MODULES})
  if(NOT HAVE_opencv_${module})
    list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
  endif()
endforeach()

######################################################################################################################################

# scripts
set(scripts_gen_java "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_java.py")
set(scripts_hdr_parser "${CMAKE_CURRENT_SOURCE_DIR}/../python/src2/hdr_parser.py")
set(scripts_gen_javadoc "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_javadoc.py")
set(scripts_rst_parser "${CMAKE_CURRENT_SOURCE_DIR}/generator/rst_parser.py")

# handwritten C/C++ and Java sources
file(GLOB handwrittren_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.hpp")
file(GLOB handwrittren_cpp_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.cpp")
file(GLOB handwrittren_java_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.java")
file(GLOB handwrittren_aidl_sources  "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.aidl")
if(NOT ANDROID)
  ocv_list_filterout(handwrittren_java_sources "/(engine|android)\\\\+")
  ocv_list_filterout(handwrittren_aidl_sources "/(engine|android)\\\\+")
else()
  file(GLOB_RECURSE handwrittren_lib_project_files_rel RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/" "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/*")
  list(REMOVE_ITEM handwrittren_lib_project_files_rel "${ANDROID_MANIFEST_FILE}")
endif()

# headers of OpenCV modules
set(opencv_public_headers "")
foreach(module ${OPENCV_JAVA_MODULES})
  # get list of module headers
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist")
    file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist" module_headers)
    ocv_list_add_prefix(module_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/")
  else()
    set(module_headers "${OPENCV_MODULE_opencv_${module}_HEADERS}")
  endif()
  if(module_headers)
    # C headers must go first
    set(module_headers_cpp ${module_headers})
    ocv_list_filterout(module_headers_cpp "\\\\.h$")
    if(module_headers_cpp)
      list(REMOVE_ITEM module_headers ${module_headers_cpp})
      list(APPEND module_headers ${module_headers_cpp})
    endif()
    unset(module_headers_cpp)

    set(opencv_public_headers_${module} ${module_headers})
    list(APPEND opencv_public_headers ${module_headers})
  else()
    list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
  endif()
endforeach()

# rst documentation used for javadoc generation
set(javadoc_rst_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  file(GLOB_RECURSE refman_rst_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/*.rst")
  list(APPEND javadoc_rst_sources ${refman_rst_headers})
endforeach()

# generated cpp files
set(generated_cpp_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  list(APPEND generated_cpp_sources "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp")
endforeach()

# IMPORTANT: add dependencies to cmake (we should rerun cmake if any of these files is modified)
configure_file("${scripts_gen_java}"   "${OpenCV_BINARY_DIR}/junk/gen_java.junk" COPYONLY)
configure_file("${scripts_hdr_parser}" "${OpenCV_BINARY_DIR}/junk/hdr_parser.junk" COPYONLY)
foreach(header ${opencv_public_headers})
  get_filename_component(header_name "${header}" NAME)
  configure_file("${header}" "${OpenCV_BINARY_DIR}/junk/${header_name}.junk" COPYONLY)
endforeach()

# generated java files
set(generated_java_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  # first run of gen_java.py (to get list of generated files)
  file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/")
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out")
  execute_process(COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
                  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out"
                  OUTPUT_QUIET ERROR_QUIET)
  unset(generated_java_sources_${module})
  file(GLOB_RECURSE generated_java_sources_${module} RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/" "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/*.java")
  ocv_list_add_prefix(generated_java_sources_${module} "${CMAKE_CURRENT_BINARY_DIR}/")

  list(APPEND generated_java_sources ${generated_java_sources_${module}})
endforeach()

# generated java files with javadoc
set(documented_java_files "")
foreach(java_file ${generated_java_sources} ${handwrittren_java_sources})
  get_filename_component(java_file_name "${java_file}" NAME_WE)
  list(APPEND documented_java_files "${CMAKE_CURRENT_BINARY_DIR}/${java_file_name}-jdoc.java")
endforeach()

######################################################################################################################################

# step 1: generate .cpp/.java from OpenCV headers
set(step1_depends "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers})
foreach(module ${OPENCV_JAVA_MODULES})
  # second run of gen_java.py (at build time)
  add_custom_command(OUTPUT ${generated_java_sources_${module}} "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp"
                     COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                     DEPENDS "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers_${module}}
                    )
endforeach()

# step 2: generate javadoc comments
set(step2_depends ${step1_depends} ${scripts_gen_javadoc} ${scripts_rst_parser} ${javadoc_rst_sources} ${generated_java_sources} ${handwrittren_java_sources})
string(REPLACE ";" "," OPENCV_JAVA_MODULES_STR "${OPENCV_JAVA_MODULES}")
add_custom_command(OUTPUT ${documented_java_files}
                   COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_javadoc}" --modules ${OPENCV_JAVA_MODULES_STR} "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java" "${CMAKE_CURRENT_BINARY_DIR}" 2>${CMAKE_CURRENT_BINARY_DIR}/get_javadoc_errors.log
                   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                   DEPENDS ${step2_depends}
                  )

# step 3: copy files to destination
set(step3_input_files ${documented_java_files} ${handwrittren_aidl_sources})
set(copied_files "")
foreach(java_file ${step3_input_files})
  get_filename_component(java_file_name "${java_file}" NAME)
  string(REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}")
  string(REPLACE "+" "/" java_file_name "${java_file_name}")
  set(output_name "${OpenCV_BINARY_DIR}/src/org/opencv/${java_file_name}")

  add_custom_command(OUTPUT "${output_name}"
                     COMMAND ${CMAKE_COMMAND} -E copy "${java_file}" "${output_name}"
                     MAIN_DEPENDENCY "${java_file}"
                     DEPENDS ${step2_depends}
                     COMMENT "Generating src/org/opencv/${java_file_name}"
                    )<commit_msg>fix the crash as suggested by @SpecLad<commit_after># ----------------------------------------------------------------------------
#  CMake file for java support
# ----------------------------------------------------------------------------
if(IOS OR NOT PYTHON_EXECUTABLE OR NOT ANT_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)))
  ocv_module_disable(java)
endif()

set(the_description "The java bindings")
ocv_add_module(java BINDINGS opencv_core opencv_imgproc OPTIONAL opencv_objdetect opencv_features2d opencv_video opencv_highgui opencv_ml opencv_calib3d opencv_photo opencv_nonfree opencv_contrib)
ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp")

if(NOT ANDROID)
  include_directories(${JNI_INCLUDE_DIRS})
endif()

# output locations
set(JAVA_INSTALL_ROOT "sdk/java")
set(JNI_INSTALL_ROOT  "sdk/native")

# get list of modules to wrap
string(REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}")
foreach(module ${OPENCV_JAVA_MODULES})
  if(NOT HAVE_opencv_${module})
    list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
  endif()
endforeach()

######################################################################################################################################

# scripts
set(scripts_gen_java "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_java.py")
set(scripts_hdr_parser "${CMAKE_CURRENT_SOURCE_DIR}/../python/src2/hdr_parser.py")
set(scripts_gen_javadoc "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_javadoc.py")
set(scripts_rst_parser "${CMAKE_CURRENT_SOURCE_DIR}/generator/rst_parser.py")

# handwritten C/C++ and Java sources
file(GLOB handwrittren_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.hpp")
file(GLOB handwrittren_cpp_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.cpp")
file(GLOB handwrittren_java_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.java")
file(GLOB handwrittren_aidl_sources  "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.aidl")
if(NOT ANDROID)
  ocv_list_filterout(handwrittren_java_sources "/(engine|android)\\\\+")
  ocv_list_filterout(handwrittren_aidl_sources "/(engine|android)\\\\+")
else()
  file(GLOB_RECURSE handwrittren_lib_project_files_rel RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/" "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/*")
  list(REMOVE_ITEM handwrittren_lib_project_files_rel "${ANDROID_MANIFEST_FILE}")
endif()

# headers of OpenCV modules
set(opencv_public_headers "")
foreach(module ${OPENCV_JAVA_MODULES})
  # get list of module headers
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist")
    file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist" module_headers)
    ocv_list_add_prefix(module_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/")
  else()
    set(module_headers "${OPENCV_MODULE_opencv_${module}_HEADERS}")
  endif()
  if(module_headers)
    # C headers must go first
    set(module_headers_cpp ${module_headers})
    ocv_list_filterout(module_headers_cpp "\\\\.h$")
    if(module_headers_cpp)
      list(REMOVE_ITEM module_headers ${module_headers_cpp})
      list(APPEND module_headers ${module_headers_cpp})
    endif()
    unset(module_headers_cpp)

    set(opencv_public_headers_${module} ${module_headers})
    list(APPEND opencv_public_headers ${module_headers})
  else()
    list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
  endif()
endforeach()

# rst documentation used for javadoc generation
set(javadoc_rst_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  file(GLOB_RECURSE refman_rst_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/*.rst")
  list(APPEND javadoc_rst_sources ${refman_rst_headers})
endforeach()

# generated cpp files
set(generated_cpp_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  list(APPEND generated_cpp_sources "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp")
endforeach()

# IMPORTANT: add dependencies to cmake (we should rerun cmake if any of these files is modified)
configure_file("${scripts_gen_java}"   "${OpenCV_BINARY_DIR}/junk/gen_java.junk" COPYONLY)
configure_file("${scripts_hdr_parser}" "${OpenCV_BINARY_DIR}/junk/hdr_parser.junk" COPYONLY)
foreach(header ${opencv_public_headers})
  get_filename_component(header_name "${header}" NAME)
  configure_file("${header}" "${OpenCV_BINARY_DIR}/junk/${header_name}.junk" COPYONLY)
endforeach()

# generated java files
set(generated_java_sources "")
foreach(module ${OPENCV_JAVA_MODULES})
  # first run of gen_java.py (to get list of generated files)
  file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/")
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out")
  execute_process(COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
                  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out"
                  OUTPUT_QUIET ERROR_QUIET)
  unset(generated_java_sources_${module})
  file(GLOB_RECURSE generated_java_sources_${module} RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/" "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/*.java")
  ocv_list_add_prefix(generated_java_sources_${module} "${CMAKE_CURRENT_BINARY_DIR}/")

  list(APPEND generated_java_sources ${generated_java_sources_${module}})
endforeach()

# generated java files with javadoc
set(documented_java_files "")
foreach(java_file ${generated_java_sources} ${handwrittren_java_sources})
  get_filename_component(java_file_name "${java_file}" NAME_WE)
  list(APPEND documented_java_files "${CMAKE_CURRENT_BINARY_DIR}/${java_file_name}-jdoc.java")
endforeach()

######################################################################################################################################

# step 1: generate .cpp/.java from OpenCV headers
set(step1_depends "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers})
foreach(module ${OPENCV_JAVA_MODULES})
  # second run of gen_java.py (at build time)
  add_custom_command(OUTPUT ${generated_java_sources_${module}} "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp"
                     COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                     DEPENDS "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers_${module}}
                    )
endforeach()

# step 2: generate javadoc comments
set(step2_depends ${step1_depends} ${scripts_gen_javadoc} ${scripts_rst_parser} ${javadoc_rst_sources} ${generated_java_sources} ${handwrittren_java_sources})
string(REPLACE ";" "," OPENCV_JAVA_MODULES_STR "${OPENCV_JAVA_MODULES}")
add_custom_command(OUTPUT ${documented_java_files}
                   COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_javadoc}" --modules ${OPENCV_JAVA_MODULES_STR} "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java" "${CMAKE_CURRENT_BINARY_DIR}" 2> "${CMAKE_CURRENT_BINARY_DIR}/get_javadoc_errors.log"
                   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                   DEPENDS ${step2_depends}
                  )

# step 3: copy files to destination
set(step3_input_files ${documented_java_files} ${handwrittren_aidl_sources})
set(copied_files "")
foreach(java_file ${step3_input_files})
  get_filename_component(java_file_name "${java_file}" NAME)
  string(REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}")
  string(REPLACE "+" "/" java_file_name "${java_file_name}")
  set(output_name "${OpenCV_BINARY_DIR}/src/org/opencv/${java_file_name}")

  add_custom_command(OUTPUT "${output_name}"
                     COMMAND ${CMAKE_COMMAND} -E copy "${java_file}" "${output_name}"
                     MAIN_DEPENDENCY "${java_file}"
                     DEPENDS ${step2_depends}
                     COMMENT "Generating src/org/opencv/${java_file_name}"
                    )
{'fraction_non_alphanumeric': 0.1677520350657483, 'fraction_numerical': 0.001878522229179712, 'mean_word_length': 5.836900684931507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2983623', 'n_tokens_mistral': 5427, 'n_tokens_neox': 5181, 'n_words': 783}