3333 return; \
3434 }
3535
36+ static jclass java_util_ArrayList ;
3637static jclass java_lang_OutOfMemoryError ;
3738static jclass com_uber_h3core_exceptions_H3Exception ;
39+ static jclass com_uber_h3core_util_LatLng ;
3840
3941static jmethodID com_uber_h3core_exceptions_H3Exception_init ;
42+ static jmethodID com_uber_h3core_util_LatLng_init ;
4043static jmethodID java_lang_OutOfMemoryError_init ;
44+ static jmethodID java_util_ArrayList_init ;
45+ static jmethodID java_util_ArrayList_add ;
4146
4247jint JNI_OnLoad (JavaVM * vm , void * reserved ) {
4348 JNIEnv * env ;
4449 if ((* * vm ).GetEnv (vm , (void * * )& env , JNI_VERSION_1_6 ) != JNI_OK ) {
4550 return JNI_ERR ;
4651 } else {
52+ jclass local_arrayListClass =
53+ (* * env ).FindClass (env , "java/util/ArrayList" );
54+ java_util_ArrayList_init =
55+ (* * env ).GetMethodID (env , local_arrayListClass , "<init>" , "()V" );
56+ java_util_ArrayList_add = (* * env ).GetMethodID (
57+ env , local_arrayListClass , "add" , "(Ljava/lang/Object;)Z" );
58+ java_util_ArrayList =
59+ (jclass )(* * env ).NewGlobalRef (env , local_arrayListClass );
60+
61+ jclass local_latLngClass =
62+ (* * env ).FindClass (env , "com/uber/h3core/util/LatLng" );
63+ com_uber_h3core_util_LatLng_init =
64+ (* * env ).GetMethodID (env , local_latLngClass , "<init>" , "(DD)V" );
65+ com_uber_h3core_util_LatLng =
66+ (jclass )(* * env ).NewGlobalRef (env , local_latLngClass );
67+
4768 jclass local_h3eClass =
4869 (* * env ).FindClass (env , "com/uber/h3core/exceptions/H3Exception" );
4970 com_uber_h3core_exceptions_H3Exception_init =
@@ -76,6 +97,12 @@ void JNI_OnUnload(JavaVM *vm, void *reserved) {
7697 if (java_lang_OutOfMemoryError != NULL ) {
7798 (* * env ).DeleteGlobalRef (env , java_lang_OutOfMemoryError );
7899 }
100+ if (java_util_ArrayList != NULL ) {
101+ (* * env ).DeleteGlobalRef (env , java_util_ArrayList );
102+ }
103+ if (com_uber_h3core_util_LatLng != NULL ) {
104+ (* * env ).DeleteGlobalRef (env , com_uber_h3core_util_LatLng );
105+ }
79106 }
80107}
81108
@@ -639,38 +666,9 @@ JNIEXPORT void JNICALL Java_com_uber_h3core_NativeMethods_polygonToCells(
639666void ConvertLinkedGeoPolygonToManaged (JNIEnv * env ,
640667 LinkedGeoPolygon * currentPolygon ,
641668 jobject results ) {
642- jclass arrayListClass = (* * env ).FindClass (env , "java/util/ArrayList" );
643- if (arrayListClass == NULL ) {
644- ThrowOutOfMemoryError (env );
645- return ;
646- }
647- jclass latLngClass = (* * env ).FindClass (env , "com/uber/h3core/util/LatLng" );
648- if (latLngClass == NULL ) {
649- ThrowOutOfMemoryError (env );
650- return ;
651- }
652- jmethodID arrayListConstructor =
653- (* * env ).GetMethodID (env , arrayListClass , "<init>" , "()V" );
654- if (arrayListConstructor == NULL ) {
655- ThrowOutOfMemoryError (env );
656- return ;
657- }
658- jmethodID arrayListAdd = (* * env ).GetMethodID (env , arrayListClass , "add" ,
659- "(Ljava/lang/Object;)Z" );
660- if (arrayListAdd == NULL ) {
661- ThrowOutOfMemoryError (env );
662- return ;
663- }
664- jmethodID latLngConstructor =
665- (* * env ).GetMethodID (env , latLngClass , "<init>" , "(DD)V" );
666- if (latLngConstructor == NULL ) {
667- ThrowOutOfMemoryError (env );
668- return ;
669- }
670-
671669 while (currentPolygon != NULL ) {
672- jobject resultLoops =
673- ( * * env ). NewObject ( env , arrayListClass , arrayListConstructor );
670+ jobject resultLoops = ( * * env ). NewObject ( env , java_util_ArrayList ,
671+ java_util_ArrayList_init );
674672 if (resultLoops == NULL ) {
675673 return ;
676674 }
@@ -681,35 +679,38 @@ void ConvertLinkedGeoPolygonToManaged(JNIEnv *env,
681679 if (resultLoops != NULL && currentPolygon -> first != NULL ) {
682680 LinkedGeoLoop * currentLoop = currentPolygon -> first ;
683681 while (currentLoop != NULL ) {
684- jobject resultLoop = (* * env ).NewObject (env , arrayListClass ,
685- arrayListConstructor );
682+ jobject resultLoop = (* * env ).NewObject (
683+ env , java_util_ArrayList , java_util_ArrayList_init );
686684 if (resultLoop == NULL ) {
687685 return ;
688686 }
689687
690688 LinkedLatLng * coord = currentLoop -> first ;
691689 while (coord != NULL ) {
692690 jobject v =
693- (* * env ).NewObject (env , latLngClass , latLngConstructor ,
691+ (* * env ).NewObject (env , com_uber_h3core_util_LatLng ,
692+ com_uber_h3core_util_LatLng_init ,
694693 coord -> vertex .lat , coord -> vertex .lng );
695694 if (v == NULL ) {
696695 return ;
697696 }
698697
699- (* * env ).CallBooleanMethod (env , resultLoop , arrayListAdd , v );
698+ (* * env ).CallBooleanMethod (env , resultLoop ,
699+ java_util_ArrayList_add , v );
700700 RETURN_ON_EXCEPTION (env );
701701
702702 coord = coord -> next ;
703703 }
704704
705- (* * env ).CallBooleanMethod (env , resultLoops , arrayListAdd ,
706- resultLoop );
705+ (* * env ).CallBooleanMethod (env , resultLoops ,
706+ java_util_ArrayList_add , resultLoop );
707707 RETURN_ON_EXCEPTION (env );
708708
709709 currentLoop = currentLoop -> next ;
710710 }
711711
712- (* * env ).CallBooleanMethod (env , results , arrayListAdd , resultLoops );
712+ (* * env ).CallBooleanMethod (env , results , java_util_ArrayList_add ,
713+ resultLoops );
713714 RETURN_ON_EXCEPTION (env );
714715 }
715716
0 commit comments