diff options
author | Elliott Hughes <enh@google.com> | 2011-03-16 16:42:06 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2011-03-16 17:27:22 -0700 |
commit | ec617e2cb4a374f0fd8fbda4a633214cf23a59a9 (patch) | |
tree | 0fa0d0cb7e38f8e35af48ca3842b7285b225b04a | |
parent | 951b4c64ef9a075c405fff859302e30c2fdb9897 (diff) | |
download | libcore-ec617e2cb4a374f0fd8fbda4a633214cf23a59a9.zip libcore-ec617e2cb4a374f0fd8fbda4a633214cf23a59a9.tar.gz libcore-ec617e2cb4a374f0fd8fbda4a633214cf23a59a9.tar.bz2 |
Move the getenv(3) native code out of System as a demo.
This is a fairly trivial patch, just to show the general structure.
Bug: 3107501
Change-Id: I547df621ccb8b8874e5f74c4d0bcf84ccaef97c3
-rw-r--r-- | luni/src/main/java/java/lang/System.java | 11 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/Libcore.java | 23 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/Os.java | 21 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/Posix.java | 23 | ||||
-rw-r--r-- | luni/src/main/native/Register.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/java_lang_System.cpp | 9 | ||||
-rw-r--r-- | luni/src/main/native/libcore_io_Posix.cpp | 38 | ||||
-rw-r--r-- | luni/src/main/native/sub.mk | 1 |
8 files changed, 114 insertions, 14 deletions
diff --git a/luni/src/main/java/java/lang/System.java b/luni/src/main/java/java/lang/System.java index 459e12d..df130e6 100644 --- a/luni/src/main/java/java/lang/System.java +++ b/luni/src/main/java/java/lang/System.java @@ -51,6 +51,7 @@ import java.util.Properties; import java.util.PropertyPermission; import java.util.Set; import libcore.icu.ICU; +import libcore.io.Libcore; import libcore.util.ZoneInfoDB; /** @@ -199,14 +200,14 @@ public final class System { * if no variable exists with the given name. */ public static String getenv(String name) { - if (name == null) { - throw new NullPointerException(); - } - return getEnvByName(name); + return getenv(name, null); } private static String getenv(String name, String defaultValue) { - String value = getEnvByName(name); + if (name == null) { + throw new NullPointerException("name == null"); + } + String value = Libcore.os.getenv(name); return (value != null) ? value : defaultValue; } diff --git a/luni/src/main/java/libcore/io/Libcore.java b/luni/src/main/java/libcore/io/Libcore.java new file mode 100644 index 0000000..d4c7b01 --- /dev/null +++ b/luni/src/main/java/libcore/io/Libcore.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.io; + +public final class Libcore { + private Libcore() { } + + public static Os os = new Posix(); +} diff --git a/luni/src/main/java/libcore/io/Os.java b/luni/src/main/java/libcore/io/Os.java new file mode 100644 index 0000000..bdcd25b --- /dev/null +++ b/luni/src/main/java/libcore/io/Os.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.io; + +public interface Os { + public String getenv(String name); +} diff --git a/luni/src/main/java/libcore/io/Posix.java b/luni/src/main/java/libcore/io/Posix.java new file mode 100644 index 0000000..7ee3c52 --- /dev/null +++ b/luni/src/main/java/libcore/io/Posix.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.io; + +public final class Posix implements Os { + Posix() { } + + public native String getenv(String name); +} diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp index 81c26fe..30fa135 100644 --- a/luni/src/main/native/Register.cpp +++ b/luni/src/main/native/Register.cpp @@ -55,6 +55,7 @@ extern int register_libcore_icu_TimeZones(JNIEnv* env); extern int register_libcore_io_IoUtils(JNIEnv* env); extern int register_libcore_io_Memory(JNIEnv* env); extern int register_libcore_io_OsConstants(JNIEnv* env); +extern int register_libcore_io_Posix(JNIEnv* env); extern int register_libcore_net_RawSocket(JNIEnv* env); extern int register_org_apache_harmony_dalvik_NativeTestTarget(JNIEnv* env); extern int register_org_apache_harmony_luni_platform_OSFileSystem(JNIEnv* env); @@ -104,6 +105,7 @@ extern "C" int registerCoreLibrariesJni(JNIEnv* env) { register_libcore_io_IoUtils(env) != -1 && register_libcore_io_Memory(env) != -1 && register_libcore_io_OsConstants(env) != -1 && + register_libcore_io_Posix(env) != -1 && register_libcore_net_RawSocket(env) != -1 && register_org_apache_harmony_dalvik_NativeTestTarget(env) != -1 && register_org_apache_harmony_luni_platform_OSFileSystem(env) != -1 && diff --git a/luni/src/main/native/java_lang_System.cpp b/luni/src/main/native/java_lang_System.cpp index 40d8b5b..2d7e70f 100644 --- a/luni/src/main/native/java_lang_System.cpp +++ b/luni/src/main/native/java_lang_System.cpp @@ -34,14 +34,6 @@ #include <sys/utsname.h> #include <unistd.h> -static jstring System_getEnvByName(JNIEnv* env, jclass, jstring javaName) { - ScopedUtfChars name(env, javaName); - if (name.c_str() == NULL) { - return NULL; - } - return env->NewStringUTF(getenv(name.c_str())); -} - static jstring System_getEnvByIndex(JNIEnv* env, jclass, jint index) { // Pointer to complete environment. extern char** environ; @@ -107,7 +99,6 @@ static jobjectArray System_specialProperties(JNIEnv* env, jclass) { static JNINativeMethod gMethods[] = { NATIVE_METHOD(System, getEnvByIndex, "(I)Ljava/lang/String;"), - NATIVE_METHOD(System, getEnvByName, "(Ljava/lang/String;)Ljava/lang/String;"), NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"), NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"), NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"), diff --git a/luni/src/main/native/libcore_io_Posix.cpp b/luni/src/main/native/libcore_io_Posix.cpp new file mode 100644 index 0000000..68145ba --- /dev/null +++ b/luni/src/main/native/libcore_io_Posix.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "Posix" + +#include "JNIHelp.h" +#include "JniConstants.h" +#include "ScopedUtfChars.h" + +#include <stdlib.h> + +static jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { + ScopedUtfChars name(env, javaName); + if (name.c_str() == NULL) { + return NULL; + } + return env->NewStringUTF(getenv(name.c_str())); +} + +static JNINativeMethod gMethods[] = { + NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), +}; +int register_libcore_io_Posix(JNIEnv* env) { + return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); +} diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk index 88c9309..c4dc428 100644 --- a/luni/src/main/native/sub.mk +++ b/luni/src/main/native/sub.mk @@ -46,6 +46,7 @@ LOCAL_SRC_FILES := \ libcore_io_IoUtils.cpp \ libcore_io_Memory.cpp \ libcore_io_OsConstants.cpp \ + libcore_io_Posix.cpp \ libcore_net_RawSocket.cpp \ org_apache_harmony_luni_platform_OSFileSystem.cpp \ org_apache_harmony_luni_platform_OSNetworkSystem.cpp \ |