Ticket #352: is-assignable.patch

File is-assignable.patch, 3.2 KB (added by Mark Evenson, 10 years ago)

Olof's further patch

  • test/lisp/abcl/java-tests.lisp

     
    195195    (jcall method "test" (make-immediate-object nil :boolean) 0 "this is a test" 10 4))
    196196  t)
    197197
     198(deftest jcall.5
     199  (jcall "join" (jstatic "currentThread" "java.lang.Thread") 1 1)
     200  nil)
     201
     202(deftest jcall.6
     203  (jcall "offsetByCodePoints" "foobar" 0 #\Nul)
     204  0)
     205
    198206(deftest jfield.1
    199207  (type-of (jfield "java.lang.Integer" "TYPE"))
    200208  #+abcl    java-object
  • src/org/armedbear/lisp/Java.java

     
    10401040        return result;
    10411041    }
    10421042
     1043    private static boolean isAssignable(Class<?> from, Class<?> to) {
     1044        from = maybeBoxClass(from);
     1045        to = maybeBoxClass(to);
     1046        if (to.isAssignableFrom(from)) {
     1047            return true;
     1048        }
     1049        if (Byte.class.equals(from)) {
     1050            return Short.class.equals(to) || Integer.class.equals(to) || Long.class.equals(to) || Float.class.equals(to) || Double.class.equals(to);
     1051        } else if (Short.class.equals(from) || Character.class.equals(from)) {
     1052            return Integer.class.equals(to) || Long.class.equals(to) || Float.class.equals(to) || Double.class.equals(to);
     1053        } else if (Integer.class.equals(from)) {
     1054            return Long.class.equals(to) || Float.class.equals(to) || Double.class.equals(to);
     1055        } else if (Long.class.equals(from)) {
     1056            return Float.class.equals(to) || Double.class.equals(to);
     1057        } else if (Float.class.equals(from)) {
     1058            return Double.class.equals(to);
     1059        }
     1060        return false;
     1061    }
     1062
    10431063    private static boolean isApplicableMethod(Class<?>[] methodTypes,
    10441064            Object[] args) {
    10451065        for (int i = 0; i < methodTypes.length; ++i) {
    10461066            Class<?> methodType = methodTypes[i];
    10471067            Object arg = args[i];
    1048             if (methodType.isPrimitive()) {
    1049                 Class<?> x = getBoxedClass(methodType);
    1050                 if (!x.isInstance(arg)) {
    1051                     return false;
    1052                 }
    1053             } else if (arg != null && !methodType.isInstance(arg)) {
     1068            if (!isAssignable(arg.getClass(), methodType)) {
    10541069                return false;
    10551070            }
    10561071        }
     
    10591074
    10601075    private static boolean isMoreSpecialized(Class<?>[] xtypes, Class<?>[] ytypes) {
    10611076        for (int i = 0; i < xtypes.length; ++i) {
    1062             Class<?> xtype = xtypes[i];
    1063             if (xtype.isPrimitive()) {
    1064                 xtype = getBoxedClass(xtype);
    1065             }
    1066             Class<?> ytype = ytypes[i];
    1067             if (ytype.isPrimitive()) {
    1068                 ytype = getBoxedClass(ytype);
    1069             }
     1077            Class<?> xtype = maybeBoxClass(xtypes[i]);
     1078            Class<?> ytype = maybeBoxClass(ytypes[i]);
    10701079            if (xtype.equals(ytype)) {
    10711080                continue;
    10721081            }
    1073             if (ytype.isAssignableFrom(xtype)) {
     1082            if (isAssignable(xtype, ytype)) {
    10741083                return true;
    10751084            }
    10761085        }