[m-rev.] diff: use Object[] for non-primitive arrays in java

Peter Wang novalazy at gmail.com
Fri Jul 17 16:10:02 AEST 2009


Branches: main

Use java.lang.Object[] as the type of all non-primitive arrays instead of more
specific types.  To create arrays with the more specific types we used
reflection on a representative element, but we can't do that if the element is
`null', as it might be for foreign types.

compiler/mlds_to_java.m:
        Write java.lang.Object[] instead of Type[] when Type is not a primitive
        type.

library/array.m:
        For non-primitive element types, just create an Object[] array.

diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index e1404d5..b69a97e 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -2189,7 +2189,19 @@ output_type(Style,
mlds_mercury_array_type(ElementType), !IO) :-
         % class, so we just use the universal base `java.lang.Object'.
         io.write_string("/* Array */ java.lang.Object", !IO)
     ;
-        output_type(Style, ElementType, !IO),
+        % For primitive element types we use arrays of the primitive type.
+        % For non-primitive element types, we just use `java.lang.Object []'.
+        % We used to use more specific types, but then to create an array of
+        % the right type we need to use reflection to determine the class of a
+        % representative element.  That doesn't work if the representative
+        % element is of a foreign type, and has the value null.
+        ( java_builtin_type(ElementType, _, _, _) ->
+            output_type(Style, ElementType, !IO)
+        ;
+            io.write_string("/* ", !IO),
+            output_type(Style, ElementType, !IO),
+            io.write_string("[] */ java.lang.Object", !IO)
+        ),
         output_array_brackets(Style, !IO)
     ).
 output_type(_, mlds_native_int_type, !IO) :-
diff --git a/library/array.m b/library/array.m
index 70a25f0..ff93f52 100644
--- a/library/array.m
+++ b/library/array.m
@@ -641,27 +641,7 @@ ML_new_array(int Size, Object Item)
     if (Item instanceof Boolean) {
         return new boolean[Size];
     }
-
-    // We must find the class corresponding to the Mercury type.
-    // For enumerations this is the class of the Item.
-    // For general d.u. types the class of the Item corresponds
-    // to a functor, so we want the enclosing class.
-
-    java.lang.Class itemClass = Item.getClass();
-    if (!itemClass.isArray()) {
-        boolean found = false;
-        for (java.lang.Class iface : itemClass.getInterfaces()) {
-            if (iface == jmercury.runtime.MercuryType.class) {
-                found = true;
-                break;
-            }
-        }
-        if (!found) {
-            itemClass = itemClass.getEnclosingClass();
-        }
-        assert itemClass != null;
-    }
-    return java.lang.reflect.Array.newInstance(itemClass, Size);
+    return new Object[Size];
 }
 ").
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list