Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -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 | 1bdaf73 | 2017-01-09 19:21:06 -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_utf_chars.h" |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -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 "jvmti_helper.h" |
| 27 | #include "test_env.h" |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace Test922Properties { |
| 31 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 32 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test922_getSystemProperties( |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 33 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) { |
| 34 | jint count; |
| 35 | char** properties; |
| 36 | jvmtiError result = jvmti_env->GetSystemProperties(&count, &properties); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 37 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | auto callback = [&](jint i) -> jstring { |
| 42 | char* data = properties[i]; |
| 43 | if (data == nullptr) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | jstring ret = env->NewStringUTF(data); |
| 47 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(data)); |
| 48 | return ret; |
| 49 | }; |
| 50 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback); |
| 51 | |
| 52 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(properties)); |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 57 | extern "C" JNIEXPORT jstring JNICALL Java_art_Test922_getSystemProperty( |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 58 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key) { |
| 59 | ScopedUtfChars string(env, key); |
| 60 | if (string.c_str() == nullptr) { |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | char* value = nullptr; |
| 65 | jvmtiError result = jvmti_env->GetSystemProperty(string.c_str(), &value); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 66 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | jstring ret = (value == nullptr) ? nullptr : env->NewStringUTF(value); |
| 71 | |
| 72 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(value)); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 77 | extern "C" JNIEXPORT void JNICALL Java_art_Test922_setSystemProperty( |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 78 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key, jstring value) { |
| 79 | ScopedUtfChars key_string(env, key); |
| 80 | if (key_string.c_str() == nullptr) { |
| 81 | return; |
| 82 | } |
| 83 | ScopedUtfChars value_string(env, value); |
| 84 | if (value_string.c_str() == nullptr) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | jvmtiError result = jvmti_env->SetSystemProperty(key_string.c_str(), value_string.c_str()); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 89 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
Andreas Gampe | 1bdaf73 | 2017-01-09 19:21:06 -0800 | [diff] [blame] | 94 | } // namespace Test922Properties |
| 95 | } // namespace art |