1 /*
2  * Copyright (C) 2017 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 "android-base/macros.h"
18 #include "jni.h"
19 #include "jvmti.h"
20 
21 // Test infrastructure
22 #include "test_env.h"
23 
24 #include "jvmti_helper.h"
25 #include "scoped_local_ref.h"
26 
27 namespace art {
28 namespace Test984ObsoleteInvoke {
29 
30 static constexpr size_t kNumFrames = 30;
31 
Java_art_Test984_getFirstObsoleteMethod984(JNIEnv * env,jclass)32 extern "C" JNIEXPORT jobject JNICALL Java_art_Test984_getFirstObsoleteMethod984(JNIEnv* env,
33                                                                                 jclass) {
34   jthread cur;
35   jint frame_count;
36   jvmtiFrameInfo frames[kNumFrames];
37   // jint cur_start = 0;
38   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetCurrentThread(&cur))) {
39     // ERROR
40     return nullptr;
41   }
42   if (JvmtiErrorToException(env, jvmti_env,
43                             jvmti_env->GetStackTrace(cur,
44                                                      0,
45                                                      kNumFrames,
46                                                      frames,
47                                                      &frame_count))) {
48     // ERROR
49     return nullptr;
50   }
51   for (jint i = 0; i < frame_count; i++) {
52     jmethodID method = frames[i].method;
53     jboolean is_obsolete = false;
54     if (JvmtiErrorToException(env, jvmti_env, jvmti_env->IsMethodObsolete(method, &is_obsolete))) {
55       // ERROR
56       return nullptr;
57     }
58     if (is_obsolete) {
59       return env->ToReflectedMethod(env->FindClass("java/lang/reflect/Method"),
60                                     method,
61                                     JNI_TRUE);
62     }
63   }
64   ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
65   env->ThrowNew(rt_exception.get(), "Unable to find obsolete method!");
66   return nullptr;
67 }
68 
69 }  // namespace Test984ObsoleteInvoke
70 }  // namespace art
71