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

View File

@ -9,14 +9,6 @@ import java.io.FileOutputStream;
import java.io.FileDescriptor;
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;
// static {
@ -42,28 +34,14 @@ public abstract class System {
}
}
int code = Unknown;
if (name.equals("java.class.path")) {
code = JavaClassPath;
} 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;
}
boolean[] found = new boolean[1];
String value = getProperty(name, found);
if (found[0]) return value;
if (code == Unknown) {
return null;
} else if (code == JavaClassPath) {
return getVMProperty(code);
} else {
return getProperty(code);
}
value = getVMProperty(name, found);
if (found[0]) return value;
return null;
}
public static String setProperty(String name, String value) {
@ -79,9 +57,9 @@ public abstract class System {
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();

View File

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