Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 19 | #include "android-base/macros.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 20 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 21 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 22 | #include "scoped_local_ref.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 23 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 24 | // Test infrastructure |
| 25 | #include "jni_helper.h" |
| 26 | #include "test_env.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace Test918Fields { |
| 30 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 31 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test918_getFieldName( |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 32 | JNIEnv* env, jclass klass, jobject field) { |
| 33 | jfieldID id = env->FromReflectedField(field); |
| 34 | |
| 35 | char* name; |
| 36 | char* sig; |
| 37 | char* gen; |
| 38 | // Note: technically putting the caller class here is wrong, but we don't need it, anyways. |
| 39 | jvmtiError result = jvmti_env->GetFieldName(klass, id, &name, &sig, &gen); |
| 40 | if (result != JVMTI_ERROR_NONE) { |
| 41 | char* err; |
| 42 | jvmti_env->GetErrorName(result, &err); |
| 43 | printf("Failure running GetFieldName: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 44 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | auto callback = [&](jint i) { |
| 49 | if (i == 0) { |
| 50 | return name == nullptr ? nullptr : env->NewStringUTF(name); |
| 51 | } else if (i == 1) { |
| 52 | return sig == nullptr ? nullptr : env->NewStringUTF(sig); |
| 53 | } else { |
| 54 | return gen == nullptr ? nullptr : env->NewStringUTF(gen); |
| 55 | } |
| 56 | }; |
| 57 | jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback); |
| 58 | |
| 59 | // Need to deallocate the strings. |
| 60 | if (name != nullptr) { |
| 61 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 62 | } |
| 63 | if (sig != nullptr) { |
| 64 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 65 | } |
| 66 | if (gen != nullptr) { |
| 67 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 68 | } |
| 69 | |
| 70 | // Also run GetMethodName with all parameter pointers null to check for segfaults. |
| 71 | jvmtiError result2 = jvmti_env->GetFieldName(klass, id, nullptr, nullptr, nullptr); |
| 72 | if (result2 != JVMTI_ERROR_NONE) { |
| 73 | char* err; |
| 74 | jvmti_env->GetErrorName(result2, &err); |
| 75 | printf("Failure running GetFieldName(null, null, null): %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 76 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 83 | extern "C" JNIEXPORT jclass JNICALL Java_art_Test918_getFieldDeclaringClass( |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 84 | JNIEnv* env, jclass klass, jobject field) { |
| 85 | jfieldID id = env->FromReflectedField(field); |
| 86 | |
| 87 | jclass declaring_class; |
| 88 | jvmtiError result = jvmti_env->GetFieldDeclaringClass(klass, id, &declaring_class); |
| 89 | if (result != JVMTI_ERROR_NONE) { |
| 90 | char* err; |
| 91 | jvmti_env->GetErrorName(result, &err); |
| 92 | printf("Failure running GetFieldDeclaringClass: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 93 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | return declaring_class; |
| 98 | } |
| 99 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 100 | extern "C" JNIEXPORT jint JNICALL Java_art_Test918_getFieldModifiers( |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 101 | JNIEnv* env, jclass klass, jobject field) { |
| 102 | jfieldID id = env->FromReflectedField(field); |
| 103 | |
| 104 | jint modifiers; |
| 105 | jvmtiError result = jvmti_env->GetFieldModifiers(klass, id, &modifiers); |
| 106 | if (result != JVMTI_ERROR_NONE) { |
| 107 | char* err; |
| 108 | jvmti_env->GetErrorName(result, &err); |
| 109 | printf("Failure running GetFieldModifiers: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 110 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | return modifiers; |
| 115 | } |
| 116 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 117 | extern "C" JNIEXPORT jboolean JNICALL Java_art_Test918_isFieldSynthetic( |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 118 | JNIEnv* env, jclass klass, jobject field) { |
| 119 | jfieldID id = env->FromReflectedField(field); |
| 120 | |
| 121 | jboolean synth; |
| 122 | jvmtiError result = jvmti_env->IsFieldSynthetic(klass, id, &synth); |
| 123 | if (result != JVMTI_ERROR_NONE) { |
| 124 | char* err; |
| 125 | jvmti_env->GetErrorName(result, &err); |
| 126 | printf("Failure running IsFieldSynthetic: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 127 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | return synth; |
| 132 | } |
| 133 | |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 134 | } // namespace Test918Fields |
| 135 | } // namespace art |