1 
2 /*
3  * Copyright (C) 2019 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <cstdio>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "android-base/logging.h"
25 #include "android-base/macros.h"
26 #include "android-base/stringprintf.h"
27 #include "jni.h"
28 #include "jvmti.h"
29 #include "scoped_local_ref.h"
30 #include "scoped_utf_chars.h"
31 
32 // Test infrastructure
33 #include "jni_helper.h"
34 #include "jvmti_helper.h"
35 #include "test_env.h"
36 #include "ti_macros.h"
37 
38 namespace art {
39 namespace Test1975StructuralTransform {
40 
Java_art_Test1975_readNativeFields(JNIEnv * env,jclass k,jclass f_class,jlongArray f)41 extern "C" JNIEXPORT void JNICALL Java_art_Test1975_readNativeFields(JNIEnv* env,
42                                                                     jclass k,
43                                                                     jclass f_class,
44                                                                     jlongArray f) {
45   jint len = env->GetArrayLength(f);
46   for (jint i = 0; i < len; i++) {
47     jlong fid_val;
48     env->GetLongArrayRegion(f, i, 1, &fid_val);
49     jfieldID fid = reinterpret_cast<jfieldID>(static_cast<intptr_t>(fid_val));
50     // For this test everything is objects and static.
51     jobject val = env->GetStaticObjectField(f_class, fid);
52     env->CallStaticVoidMethod(
53         k,
54         env->GetStaticMethodID(
55             k, "printNativeField", "(JLjava/lang/reflect/Field;Ljava/lang/Object;)V"),
56         fid_val,
57         env->ToReflectedField(f_class, fid, true),
58         val);
59     env->DeleteLocalRef(val);
60   }
61 }
62 
Java_art_Test1975_getNativeFields(JNIEnv * env,jclass,jobjectArray f)63 extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test1975_getNativeFields(JNIEnv* env,
64                                                                           jclass,
65                                                                           jobjectArray f) {
66   jint len = env->GetArrayLength(f);
67   jlongArray arr = env->NewLongArray(len);
68   for (jint i = 0; i < len; i++) {
69     jfieldID fid = env->FromReflectedField(env->GetObjectArrayElement(f, i));
70     jlong lfid = static_cast<jlong>(reinterpret_cast<intptr_t>(fid));
71     env->SetLongArrayRegion(arr, i, 1, &lfid);
72   }
73   return arr;
74 }
75 
76 }  // namespace Test1975StructuralTransform
77 }  // namespace art
78