blob: 9aeb98c6e1b795728f6e009647f66cd04812384f [file] [log] [blame]
Andreas Gampe336c3c32016-11-08 17:02:19 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_TEST_TI_AGENT_COMMON_HELPER_H_
18#define ART_TEST_TI_AGENT_COMMON_HELPER_H_
19
20#include "jni.h"
21#include "ScopedLocalRef.h"
22
23namespace art {
24
25template <typename T>
26static jobjectArray CreateObjectArray(JNIEnv* env,
27 jint length,
28 const char* component_type_descriptor,
29 T src) {
30 if (length < 0) {
31 return nullptr;
32 }
33
34 ScopedLocalRef<jclass> obj_class(env, env->FindClass(component_type_descriptor));
35 if (obj_class.get() == nullptr) {
36 return nullptr;
37 }
38
39 ScopedLocalRef<jobjectArray> ret(env, env->NewObjectArray(length, obj_class.get(), nullptr));
40 if (ret.get() == nullptr) {
41 return nullptr;
42 }
43
44 for (jint i = 0; i < length; ++i) {
45 jobject element = src(i);
46 env->SetObjectArrayElement(ret.get(), static_cast<jint>(i), element);
47 env->DeleteLocalRef(element);
48 if (env->ExceptionCheck()) {
49 return nullptr;
50 }
51 }
52
53 return ret.release();
54}
55
56} // namespace art
57
58#endif // ART_TEST_TI_AGENT_COMMON_HELPER_H_