[armedbear-devel] Losing type information after storing in java.util.ArrayList

dmiles@users.sourceforge.net logicmoo at gmail.com
Tue Mar 23 06:21:47 UTC 2010


I was able to fix this (bug?)

Basically ABCL is missing a

   if (cc.isInstance(obj)) return obj;

in the function

 public <T> Object javaInstance(Class<T> c)

of JavaObject.java



In ABCL's src i'd go from:


    @Override
	public <T> Object javaInstance(Class<T> c) {
		final Class cc;
		if (obj == null) {
			if (c.isPrimitive()) {
				throw new NullPointerException("Cannot assign null to " + c);
			}
			return obj;
		} else {
			cc = JavaFunctions.maybeBoxClass(c);
			if (cc.isAssignableFrom(intendedClass)) {
				return obj;
			} else {
				return error(new TypeError(intendedClass.getName() + " is not
assignable to " + c.getName()));
			}
		}
	}

to

    @Override
	public <T> Object javaInstance(Class<T> c) {
		final Class cc;
		if (obj == null) {
			if (c.isPrimitive()) {
				throw new NullPointerException("Cannot assign null to " + c);
			}
			return obj;
		} else {
			cc = JavaFunctions.maybeBoxClass(c);
			if (cc.isInstance(obj))
				return obj;
			if (cc.isAssignableFrom(intendedClass)) {
				return obj;
			} else {
				return error(new TypeError(intendedClass.getName() + " is not
assignable to " + c.getName()));
			}
		}
	}



A commiter should be able to help


On Mon, Mar 22, 2010 at 9:55 PM, David Kirkman <dkirkman at ucsd.edu> wrote:
> I've got a subtle problem:  if I create a byte array, store it in a
> java.util.ArrayList, and then retrieve the stored byte array, what I get
> back is not quite the same type as the original byte array.  I think that
> everything is the same, except that the JavaObjects have different values
> for the intendedClass field.
>
> Here is an example:
>
> (setq array1 (jnew-array "byte" 1))
>
> (setq array2
>      (let ((array-list (jnew (jconstructor "java.util.ArrayList"))))
>        ;; put the byte array into the ArrayList
>        (jcall (jmethod "java.util.AbstractList" "add" "java.lang.Object")
>               array-list array1)
>        ;; and pull it back out
>        (jcall (jmethod "java.util.AbstractList" "get" "int")
>               array-list 0)))
>
> (describe array1)
> ;#<jarray [B at c4ff22 {4A44DD}> is an object of type JAVA-OBJECT.
> ;The wrapped Java object is an array of bytes with 1 element.; No value
>
> (describe array2)
> ;#<jarray [B at c4ff22 {5E6B62}> is an object of type JAVA-OBJECT.
> ;The wrapped Java object is an array of bytes with 1 element.; No value
>
> At this point array1 and array2 look pretty compatible, but ...
>
> (sys::%make-byte-array-input-stream array1)
> ; works fine, gives a #S(SYSTEM::SYSTEM-STREAM)
>
> (sys::%make-byte-array-input-stream array2)
> ; gives a TYPE-ERROR, with message "java.lang.Object is not assignable to [B"
>
> The TYPE-ERROR comes from JavaObject.javaInstance(Class<?> c) (line 248
> of JavaObject.java).  It seems that array1 has intendedClass="[B", and
> array2 has intendedClass="java.lang.Object", so I'm pretty sure that's my
> problem.
>
> Is there a way to specify the intendedClass of array2?
>
> Thanks,
>
> -david k.
>
> _______________________________________________
> armedbear-devel mailing list
> armedbear-devel at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel
>




More information about the armedbear-devel mailing list