< prev index next >

src/java.base/share/native/libjava/Module.c

Print this page
rev 16412 : 8171855: Move package name transformations during module bootstrap into native code
Reviewed-by: alanb, acorn, lfoltan, mchung

@@ -20,22 +20,77 @@
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+#include <stdlib.h>
+#include <string.h>
 
 #include "jni.h"
+#include "jni_util.h"
 #include "jvm.h"
 
 #include "java_lang_reflect_Module.h"
 
+/*
+ * Translates '.' to '/'.  Does no validation, assumption being that both
+ * calling code in java.lang.reflect.Module and VM will do deeper validation.
+ */
+JNIEXPORT void
+ToInternalForm(char *name)
+{
+    char *p = name;
+    while (*p != '\0') {
+        if (*p == '.') {
+            *p = '/';
+        }
+        p++;
+    }
+}
+
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_defineModule0(JNIEnv *env, jclass cls, jobject module,
                                             jboolean is_open, jstring version,
                                             jstring location, jobjectArray packages)
 {
-    JVM_DefineModule(env, module, is_open, version, location, packages);
+    char** pkgs = NULL;
+    jsize idx;
+    jsize pkgs_len = (*env)->GetArrayLength(env, packages);
+
+    if (pkgs_len != 0 && (pkgs = calloc(pkgs_len, sizeof(char*))) == NULL) {
+        JNU_ThrowOutOfMemoryError(env, NULL);
+        return;
+    } else {
+        int valid = 1;
+        for (idx = 0; idx < pkgs_len; idx++) {
+            jstring pkg = (*env)->GetObjectArrayElement(env, packages, idx);
+            jsize len = (*env)->GetStringUTFLength(env, pkg);
+            jsize unicode_len = (*env)->GetStringLength(env, pkg);
+
+            pkgs[idx] = malloc(len + 1);
+            if (pkgs[idx] == NULL) {
+                JNU_ThrowOutOfMemoryError(env, NULL);
+                valid = 0;
+                break;
+            }
+
+            (*env)->GetStringUTFRegion(env, pkg, 0, unicode_len, pkgs[idx]);
+            ToInternalForm(pkgs[idx]);
+        }
+
+        if (valid != 0) {
+            JVM_DefineModule(env, module, is_open, version, location,
+                    (const char* const*)pkgs, pkgs_len);
+        }
+    }
+
+    for (idx = 0; idx < pkgs_len; idx++) {
+        if (pkgs[idx] != NULL) {
+            free(pkgs[idx]);
+        }
+    }
+    free(pkgs);
 }
 
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_addReads0(JNIEnv *env, jclass cls, jobject from, jobject to)
 {

@@ -44,27 +99,103 @@
 
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_addExports0(JNIEnv *env, jclass cls, jobject from,
                                           jstring pkg, jobject to)
 {
-    JVM_AddModuleExports(env, from, pkg, to);
+    jsize len;
+    jsize unicode_len;
+    char* pkg_name;
+
+    if (pkg == NULL) {
+        JNU_ThrowNullPointerException(env, "package is null");
+        return;
+    }
+
+    len = (*env)->GetStringUTFLength(env, pkg);
+    unicode_len = (*env)->GetStringLength(env, pkg);
+    pkg_name = malloc(len + 1);
+    if (pkg_name == NULL) {
+        JNU_ThrowOutOfMemoryError(env, NULL);
+    } else {
+        (*env)->GetStringUTFRegion(env, pkg, 0, unicode_len, pkg_name);
+        ToInternalForm(pkg_name);
+        JVM_AddModuleExports(env, from, pkg_name, to);
+        free(pkg_name);
+    }
 }
 
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_addExportsToAll0(JNIEnv *env, jclass cls, jobject from,
                                                jstring pkg)
 {
-    JVM_AddModuleExportsToAll(env, from, pkg);
+    jsize len;
+    jsize unicode_len;
+    char* pkg_name;
+
+    if (pkg == NULL) {
+        JNU_ThrowNullPointerException(env, "package is null");
+        return;
+    }
+
+    len = (*env)->GetStringUTFLength(env, pkg);
+    unicode_len = (*env)->GetStringLength(env, pkg);
+    pkg_name = malloc(len + 1);
+    if (pkg_name == NULL) {
+        JNU_ThrowOutOfMemoryError(env, NULL);
+    } else {
+        (*env)->GetStringUTFRegion(env, pkg, 0, unicode_len, pkg_name);
+        ToInternalForm(pkg_name);
+        JVM_AddModuleExportsToAll(env, from, pkg_name);
+        free(pkg_name);
+    }
 }
 
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_addExportsToAllUnnamed0(JNIEnv *env, jclass cls,
                                                       jobject from, jstring pkg)
 {
-    JVM_AddModuleExportsToAllUnnamed(env, from, pkg);
+    jsize len;
+    jsize unicode_len;
+    char* pkg_name;
+
+    if (pkg == NULL) {
+        JNU_ThrowNullPointerException(env, "package is null");
+        return;
+    }
+
+    len = (*env)->GetStringUTFLength(env, pkg);
+    unicode_len = (*env)->GetStringLength(env, pkg);
+    pkg_name = malloc(len + 1);
+    if (pkg_name == NULL) {
+        JNU_ThrowOutOfMemoryError(env, NULL);
+    } else {
+        (*env)->GetStringUTFRegion(env, pkg, 0, unicode_len, pkg_name);
+        ToInternalForm(pkg_name);
+        JVM_AddModuleExportsToAllUnnamed(env, from, pkg_name);
+        free(pkg_name);
+    }
 }
 
 JNIEXPORT void JNICALL
 Java_java_lang_reflect_Module_addPackage0(JNIEnv *env, jclass cls, jobject m, jstring pkg)
 {
-    JVM_AddModulePackage(env, m, pkg);
+    jsize len;
+    jsize unicode_len;
+    char* pkg_name;
+
+    if (pkg == NULL) {
+        JNU_ThrowNullPointerException(env, "package is null");
+        return;
+    }
+
+    len = (*env)->GetStringUTFLength(env, pkg);
+    unicode_len = (*env)->GetStringLength(env, pkg);
+    pkg_name = malloc(len + 1);
+    if (pkg_name == NULL) {
+        JNU_ThrowOutOfMemoryError(env, NULL);
+    } else {
+        (*env)->GetStringUTFRegion(env, pkg, 0, unicode_len, pkg_name);
+        ToInternalForm(pkg_name);
+        JVM_AddModulePackage(env, m, pkg_name);
+        free(pkg_name);
+    }
 }
< prev index next >