-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves implementation of JniReference to cpp
Signed-off-by: Yauheni Khnykin <[email protected]>
- Loading branch information
Showing
3 changed files
with
142 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
gluecodium/src/main/resources/templates/jni/utils/JniReferenceImplementation.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
{{!! | ||
! | ||
! Copyright (C) 2016-2024 HERE Europe B.V. | ||
! | ||
! Licensed under the Apache License, Version 2.0 (the "License"); | ||
! you may not use this file except in compliance with the License. | ||
! You may obtain a copy of the License at | ||
! | ||
! http://www.apache.org/licenses/LICENSE-2.0 | ||
! | ||
! Unless required by applicable law or agreed to in writing, software | ||
! distributed under the License is distributed on an "AS IS" BASIS, | ||
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
! See the License for the specific language governing permissions and | ||
! limitations under the License. | ||
! | ||
! SPDX-License-Identifier: Apache-2.0 | ||
! License-Filename: LICENSE | ||
! | ||
!}} | ||
{{>java/CopyrightHeader}} | ||
|
||
#include "JniReference.h" | ||
|
||
#include "JniBase.h" | ||
|
||
{{#internalNamespace}} | ||
namespace {{.}} | ||
{ | ||
{{/internalNamespace}} | ||
namespace jni | ||
{ | ||
JniReferenceDeleter::JniReferenceDeleter() noexcept = default; | ||
JniReferenceDeleter JniReferenceDeleter::create_local(JNIEnv* jni_env) noexcept | ||
{ | ||
return JniReferenceDeleter(jni_env, DeleteReferenceMethod::LOCAL); | ||
} | ||
|
||
JniReferenceDeleter JniReferenceDeleter::create_global() noexcept | ||
{ | ||
return JniReferenceDeleter(nullptr, DeleteReferenceMethod::GLOBAL); | ||
} | ||
|
||
JniReferenceDeleter JniReferenceDeleter::create_non_releasing() noexcept | ||
{ | ||
return JniReferenceDeleter(nullptr, DeleteReferenceMethod::NON_RELEASING); | ||
} | ||
|
||
JniReferenceDeleter::JniReferenceDeleter(JNIEnv* jni_env, DeleteReferenceMethod delete_reference_method) noexcept | ||
: m_jni_env(jni_env) | ||
, m_delete_reference_method(delete_reference_method) | ||
{ | ||
} | ||
|
||
void JniReferenceDeleter::operator () (jobject obj) const noexcept | ||
{ | ||
if (obj) | ||
{ | ||
switch (m_delete_reference_method) | ||
{ | ||
case DeleteReferenceMethod::LOCAL: | ||
m_jni_env->DeleteLocalRef( obj ); | ||
break; | ||
case DeleteReferenceMethod::GLOBAL: | ||
if (JNIEnv* jni_env = {{>common/InternalNamespace}}jni::getJniEnvironmentForCurrentThread( )) | ||
{ | ||
jni_env->DeleteGlobalRef( obj ); | ||
} | ||
break; | ||
case DeleteReferenceMethod::NON_RELEASING: | ||
// noop | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
JniReference<jclass> find_class(JNIEnv* jni_env, const char* name) noexcept | ||
{ | ||
return make_local_ref(jni_env, jni_env->FindClass(name)); | ||
} | ||
|
||
JniReference<jobject> | ||
create_object( JNIEnv* env, const JniReference<jclass>& javaClass ) noexcept | ||
{ | ||
const char* name = "<init>"; | ||
const char* signature = "()V"; | ||
auto theConstructor = env->GetMethodID( javaClass.get(), name, signature ); | ||
return new_object(env, javaClass, theConstructor); | ||
} | ||
|
||
JniReference<jobject> | ||
create_instance_object( JNIEnv* env, const JniReference<jclass>& javaClass, jlong instancePointer ) noexcept | ||
{ | ||
const char* name = "<init>"; | ||
const char* signature = "(JLjava/lang/Object;)V"; | ||
auto theConstructor = env->GetMethodID( javaClass.get(), name, signature ); | ||
// On Mac platform `jlong` and `jobject` are `long long`, but `NULL` is `long`, so need to cast here. | ||
return new_object(env, javaClass, theConstructor, instancePointer, static_cast<jobject>(NULL)); | ||
} | ||
|
||
JniReference<jobject> | ||
alloc_object( JNIEnv* env, const JniReference<jclass>& javaClass ) noexcept | ||
{ | ||
return make_local_ref( env, env->AllocObject( javaClass.get( ) ) ); | ||
} | ||
|
||
} // namespace jni | ||
{{#internalNamespace}} | ||
} // namespace {{.}} | ||
{{/internalNamespace}} |