Changeset 15234


Ignore:
Timestamp:
02/09/20 13:03:52 (15 months ago)
Author:
Mark Evenson
Message:

Fix calling Java varargs methods

Calling varargs with java.reflect.Method.invoke() is tricky. Still
not sure how this works, but guessed at it after reading
<https://stackoverflow.com/questions/2600854/how-to-work-with-varargs-and-reflection>,
and it sems to work.

Test via

(java:jstatic-raw "asList" "java.util.Arrays" (java:jnew-array (java:jclass "int") 1))

Location:
trunk/abcl/src/org/armedbear/lisp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/Java.java

    r15233 r15234  
    471471            }
    472472            m.setAccessible(true);
    473             Object result = m.invoke(null, methodArgs);
     473            Object result = null;
     474            if (!m.isVarArgs()) {
     475              result = m.invoke(null, methodArgs);
     476            } else {
     477              result = m.invoke(null, (Object)methodArgs);
     478            }
    474479      return JavaObject.getInstance(result, translate, m.getReturnType());
    475480        }
     
    10651070        } else if (Float.class.equals(from)) {
    10661071            return Double.class.equals(to);
     1072        } else if (from.isArray() && to.isArray()) {
     1073            // for now just indicate that anything is assignable to an
     1074            // java.lang.Object[], as this is the most common case
     1075            if (to.getComponentType().equals(java.lang.Object.class)) {
     1076                return true;
     1077            }
    10671078        }
    10681079        return false;
  • trunk/abcl/src/org/armedbear/lisp/JavaObject.java

    r15228 r15234  
    289289              // representations. 
    290290                return obj;
     291            } else if (c.isArray() && obj.getClass().isArray()) {
     292                       // ??? only supports conversions to java.lang.Object[]
     293                       //                       && c.getComponentType().equals(obj.getClass().getComponentType())) {
     294                       
     295              return obj;
    291296            } else {
    292297                return error(new TypeError(intendedClass.getName() + " is not assignable to " + c.getName()));
Note: See TracChangeset for help on using the changeset viewer.