1 /*
2  * Copyright (C) 2019 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 #include <stdio.h>
18 
19 #include <vector>
20 
21 #include "android-base/logging.h"
22 #include "android-base/macros.h"
23 #include "jni.h"
24 #include "jvmti.h"
25 
26 // Test infrastructure
27 #include "jvmti_helper.h"
28 #include "scoped_local_ref.h"
29 #include "test_env.h"
30 
31 namespace art {
32 namespace Test2009StructuralLocalRef {
33 
Java_art_Test2009_NativeLocalCallStatic(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject obj,jobject thnk)34 extern "C" JNIEXPORT jstring JNICALL Java_art_Test2009_NativeLocalCallStatic(
35     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jobject thnk) {
36   jclass obj_klass = env->GetObjectClass(obj);
37   jmethodID run_meth = env->GetMethodID(env->FindClass("java/lang/Runnable"), "run", "()V");
38   env->CallVoidMethod(thnk, run_meth);
39   jmethodID new_method =
40       env->GetStaticMethodID(obj_klass, "getGreetingStatic", "()Ljava/lang/String;");
41   if (env->ExceptionCheck()) {
42     return nullptr;
43   } else {
44     return reinterpret_cast<jstring>(env->CallStaticObjectMethod(obj_klass, new_method));
45   }
46 }
47 
Java_art_Test2009_NativeLocalCallVirtual(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject obj,jobject thnk)48 extern "C" JNIEXPORT jstring JNICALL Java_art_Test2009_NativeLocalCallVirtual(
49     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jobject thnk) {
50   jclass obj_klass = env->GetObjectClass(obj);
51   jmethodID run_meth = env->GetMethodID(env->FindClass("java/lang/Runnable"), "run", "()V");
52   env->CallVoidMethod(thnk, run_meth);
53   jmethodID new_method = env->GetMethodID(obj_klass, "getGreeting", "()Ljava/lang/String;");
54   if (env->ExceptionCheck()) {
55     return nullptr;
56   } else {
57     return reinterpret_cast<jstring>(env->CallObjectMethod(obj, new_method));
58   }
59 }
Java_art_Test2009_NativeLocalGetIField(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject obj,jobject thnk)60 extern "C" JNIEXPORT jstring JNICALL Java_art_Test2009_NativeLocalGetIField(
61     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jobject thnk) {
62   jclass obj_klass = env->GetObjectClass(obj);
63   jmethodID run_meth = env->GetMethodID(env->FindClass("java/lang/Runnable"), "run", "()V");
64   env->CallVoidMethod(thnk, run_meth);
65   jfieldID new_field = env->GetFieldID(obj_klass, "greeting", "Ljava/lang/String;");
66   if (env->ExceptionCheck()) {
67     return nullptr;
68   } else {
69     env->SetObjectField(obj, new_field, env->NewStringUTF("VirtualString"));
70     return reinterpret_cast<jstring>(env->GetObjectField(obj, new_field));
71   }
72 }
Java_art_Test2009_NativeLocalGetSField(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject obj,jobject thnk)73 extern "C" JNIEXPORT jstring JNICALL Java_art_Test2009_NativeLocalGetSField(
74     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jobject thnk) {
75   jclass obj_klass = env->GetObjectClass(obj);
76   jmethodID run_meth = env->GetMethodID(env->FindClass("java/lang/Runnable"), "run", "()V");
77   env->CallVoidMethod(thnk, run_meth);
78   jfieldID new_field = env->GetStaticFieldID(obj_klass, "static_greeting", "Ljava/lang/String;");
79   if (env->ExceptionCheck()) {
80     return nullptr;
81   } else {
82     env->SetStaticObjectField(obj_klass, new_field, env->NewStringUTF("StaticString"));
83     return reinterpret_cast<jstring>(env->GetStaticObjectField(obj_klass, new_field));
84   }
85 }
86 
87 }  // namespace Test2009StructuralLocalRef
88 }  // namespace art
89