refactor System.getProperty() to reduce coupling with native code

This commit is contained in:
Joel Dice 2007-10-26 12:13:21 -06:00
parent ccdc7fdda3
commit e892f1bff5
3 changed files with 63 additions and 88 deletions

View File

@ -31,58 +31,50 @@
#endif #endif
extern "C" JNIEXPORT jstring JNICALL extern "C" JNIEXPORT jstring JNICALL
Java_java_lang_System_getProperty(JNIEnv* e, jclass, jint code) Java_java_lang_System_getProperty(JNIEnv* e, jclass, jstring name,
jbooleanArray found)
{ {
enum { jstring r = 0;
LineSeparator = 100, const char* chars = e->GetStringUTFChars(name, 0);
FileSeparator = 101, if (chars) {
OsName = 102,
JavaIoTmpdir = 103,
UserHome = 104
};
switch (code) {
#ifdef WIN32 #ifdef WIN32
case LineSeparator: if (strcmp(chars, "line.separator") == 0) {
return e->NewStringUTF("\r\n"); r = e->NewStringUTF("\r\n");
} else if (strcmp(chars, "file.separator") == 0) {
case FileSeparator: r = e->NewStringUTF("\\");
return e->NewStringUTF("\\"); } else if (strcmp(chars, "os.name") == 0) {
r = e->NewStringUTF("windows");
case OsName: } else if (strcmp(chars, "java.io.tmpdir") == 0) {
return e->NewStringUTF("Windows"); TCHAR buffer[MAX_PATH];
GetTempPath(MAX_PATH, buffer);
case JavaIoTmpdir: { r = e->NewStringUTF(buffer);
TCHAR buffer[MAX_PATH]; } else if (strcmp(chars, "user.home") == 0) {
GetTempPath(MAX_PATH, buffer); LPWSTR home = _wgetenv(L"USERPROFILE");
return e->NewStringUTF(buffer); r = e->NewString(reinterpret_cast<jchar*>(home), lstrlenW(home));
} }
case UserHome: {
LPWSTR home = _wgetenv(L"USERPROFILE");
return e->NewString(reinterpret_cast<jchar*>(home), lstrlenW(home));
}
#else #else
case LineSeparator: if (strcmp(chars, "line.separator") == 0) {
return e->NewStringUTF("\n"); r = e->NewStringUTF("\n");
} else if (strcmp(chars, "file.separator") == 0) {
case FileSeparator: r = e->NewStringUTF("/");
return e->NewStringUTF("/"); } else if (strcmp(chars, "os.name") == 0) {
r = e->NewStringUTF("posix");
case OsName: } else if (strcmp(chars, "java.io.tmpdir") == 0) {
return e->NewStringUTF("posix"); r = e->NewStringUTF("/tmp");
} else if (strcmp(chars, "user.home") == 0) {
case JavaIoTmpdir: r = e->NewStringUTF(getenv("HOME"));
return e->NewStringUTF("/tmp"); }
case UserHome:
return e->NewStringUTF(getenv("HOME"));
#endif #endif
default: e->ReleaseStringUTFChars(name, chars);
throwNew(e, "java/lang/RuntimeException", 0);
return 0;
} }
if (r) {
jboolean v = true;
e->SetBooleanArrayRegion(found, 0, 1, &v);
}
return r;
} }
extern "C" JNIEXPORT jlong JNICALL extern "C" JNIEXPORT jlong JNICALL

View File

@ -9,14 +9,6 @@ import java.io.FileOutputStream;
import java.io.FileDescriptor; import java.io.FileDescriptor;
public abstract class System { public abstract class System {
private static final int Unknown = 0;
private static final int JavaClassPath = 1;
private static final int LineSeparator = 100;
private static final int FileSeparator = 101;
private static final int OsName = 102;
private static final int JavaIoTmpdir = 103;
private static final int UserHome = 104;
private static Property properties; private static Property properties;
// static { // static {
@ -42,28 +34,14 @@ public abstract class System {
} }
} }
int code = Unknown; boolean[] found = new boolean[1];
if (name.equals("java.class.path")) { String value = getProperty(name, found);
code = JavaClassPath; if (found[0]) return value;
} else if (name.equals("java.io.tmpdir")) {
code = JavaIoTmpdir;
} else if (name.equals("line.separator")) {
code = LineSeparator;
} else if (name.equals("file.separator")) {
code = FileSeparator;
} else if (name.equals("user.home")) {
code = UserHome;
} else if (name.equals("os.name")) {
code = OsName;
}
if (code == Unknown) { value = getVMProperty(name, found);
return null; if (found[0]) return value;
} else if (code == JavaClassPath) {
return getVMProperty(code); return null;
} else {
return getProperty(code);
}
} }
public static String setProperty(String name, String value) { public static String setProperty(String name, String value) {
@ -79,9 +57,9 @@ public abstract class System {
return null; return null;
} }
private static native String getProperty(int code); private static native String getProperty(String name, boolean[] found);
private static native String getVMProperty(int code); private static native String getVMProperty(String name, boolean[] found);
public static native long currentTimeMillis(); public static native long currentTimeMillis();

View File

@ -407,22 +407,27 @@ Java_java_lang_String_intern(Thread* t, jobject this_)
} }
extern "C" JNIEXPORT jstring JNICALL extern "C" JNIEXPORT jstring JNICALL
Java_java_lang_System_getVMProperty(Thread* t, jclass, jint code) Java_java_lang_System_getVMProperty(Thread* t, jclass, jstring name,
jbooleanArray found)
{ {
ENTER(t, Thread::ActiveState); ENTER(t, Thread::ActiveState);
enum { unsigned length = stringLength(t, *name);
JavaClassPath = 1 char n[length + 1];
}; stringChars(t, *name, n);
switch (code) { jstring r = 0;
case JavaClassPath: if (strcmp(n, "java.lang.classpath") == 0) {
return makeLocalReference(t, makeString(t, "%s", t->m->finder->path())); r = makeLocalReference(t, makeString(t, "%s", t->m->finder->path()));
} else if (strcmp(n, "avium.version") == 0) {
default: r = makeLocalReference(t, makeString(t, "0.0"));
t->exception = makeRuntimeException(t, 0);
return 0;
} }
if (r) {
booleanArrayBody(t, *found, 0) = true;
}
return r;
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL