blob: 6af45f54d66875a295c307820b28e6f8f5482062 [file] [log] [blame]
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001/*
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 Gampe1bdaf732017-01-09 19:21:06 -080017#include <stdio.h>
18
Andreas Gampe027444b2017-03-31 12:49:07 -070019#include "android-base/macros.h"
Andreas Gampe1bdaf732017-01-09 19:21:06 -080020#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070021#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070022#include "scoped_utf_chars.h"
Andreas Gampe1bdaf732017-01-09 19:21:06 -080023
Andreas Gampe3f46c962017-03-30 10:26:59 -070024// Test infrastructure
25#include "jni_helper.h"
26#include "jvmti_helper.h"
27#include "test_env.h"
Andreas Gampe1bdaf732017-01-09 19:21:06 -080028
29namespace art {
30namespace Test922Properties {
31
Andreas Gampe46651672017-04-07 09:00:04 -070032extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test922_getSystemProperties(
Andreas Gampe1bdaf732017-01-09 19:21:06 -080033 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
34 jint count;
35 char** properties;
36 jvmtiError result = jvmti_env->GetSystemProperties(&count, &properties);
Andreas Gampe3f46c962017-03-30 10:26:59 -070037 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe1bdaf732017-01-09 19:21:06 -080038 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 Gampe46651672017-04-07 09:00:04 -070057extern "C" JNIEXPORT jstring JNICALL Java_art_Test922_getSystemProperty(
Andreas Gampe1bdaf732017-01-09 19:21:06 -080058 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 Gampe3f46c962017-03-30 10:26:59 -070066 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe1bdaf732017-01-09 19:21:06 -080067 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 Gampe46651672017-04-07 09:00:04 -070077extern "C" JNIEXPORT void JNICALL Java_art_Test922_setSystemProperty(
Andreas Gampe1bdaf732017-01-09 19:21:06 -080078 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 Gampe3f46c962017-03-30 10:26:59 -070089 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe1bdaf732017-01-09 19:21:06 -080090 return;
91 }
92}
93
Andreas Gampe1bdaf732017-01-09 19:21:06 -080094} // namespace Test922Properties
95} // namespace art