Changeset 12303


Ignore:
Timestamp:
12/23/09 20:53:57 (14 years ago)
Author:
astalla
Message:

Extended the new smart behavior of jcall to jnew: (jnew class-name args) finds the most specific constructor and calls it.

File:
1 edited

Legend:

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

    r12302 r12303  
    449449            LispObject classRef = args[0];
    450450            try {
    451                 Constructor constructor = (Constructor) JavaObject.getObject(classRef);
     451                Constructor constructor;
     452    if(classRef instanceof AbstractString) {
     453        constructor = findConstructor(javaClass(classRef), args);
     454    } else {
     455        constructor = (Constructor) JavaObject.getObject(classRef);
     456    }
    452457                Class[] argTypes = constructor.getParameterTypes();
    453458                Object[] initargs = new Object[args.length-1];
     
    677682    }
    678683
    679     private static Method findMethod(Class<?> c, String methodName, LispObject[] args) throws NoSuchMethodException {
    680         int argCount = args.length - 2;
     684    private static Object[] translateMethodArguments(LispObject[] args) {
     685  return translateMethodArguments(args, 0);
     686    }
     687
     688    private static Object[] translateMethodArguments(LispObject[] args, int offs) {
     689  int argCount = args.length - offs;
    681690        Object[] javaArgs = new Object[argCount];
    682691        for (int i = 0; i < argCount; ++i) {
    683             Object x = args[i + 2];
     692            Object x = args[i + offs];
    684693            if (x == NIL) {
    685694                javaArgs[i] = null;
     
    688697            }
    689698        }
     699  return javaArgs;
     700    }
     701
     702    private static Method findMethod(Class<?> c, String methodName, LispObject[] args) throws NoSuchMethodException {
     703  int argCount = args.length - 2;
     704        Object[] javaArgs = translateMethodArguments(args, 2);
    690705        Method[] methods = c.getMethods();
    691706        Method result = null;
     
    702717                continue;
    703718            }
    704             if (result == null || isMoreSpecialized(method, result)) {
     719            if (result == null || isMoreSpecialized(methodTypes, result.getParameterTypes())) {
    705720                result = method;
    706721            }
     
    708723        if (result == null) {
    709724            throw new NoSuchMethodException(methodName);
     725        }
     726        return result;
     727    }
     728
     729    private static Constructor findConstructor(Class<?> c, LispObject[] args) throws NoSuchMethodException {
     730  int argCount = args.length - 1;
     731        Object[] javaArgs = translateMethodArguments(args, 1);
     732        Constructor[] ctors = c.getConstructors();
     733        Constructor result = null;
     734        for (int i = ctors.length; i-- > 0;) {
     735            Constructor ctor = ctors[i];
     736            if (ctor.getParameterTypes().length != argCount) {
     737                continue;
     738            }
     739            Class<?>[] methodTypes = (Class<?>[]) ctor.getParameterTypes();
     740            if (!isApplicableMethod(methodTypes, javaArgs)) {
     741                continue;
     742            }
     743            if (result == null || isMoreSpecialized(methodTypes, result.getParameterTypes())) {
     744                result = ctor;
     745            }
     746        }
     747        if (result == null) {
     748            throw new NoSuchMethodException(c.getSimpleName());
    710749        }
    711750        return result;
     
    729768    }
    730769
    731     private static boolean isMoreSpecialized(Method x, Method y) {
    732         Class<?>[] xtypes = x.getParameterTypes();
    733         Class<?>[] ytypes = y.getParameterTypes();
     770    private static boolean isMoreSpecialized(Class<?>[] xtypes, Class<?>[] ytypes) {
    734771        for (int i = 0; i < xtypes.length; ++i) {
    735772            Class<?> xtype = xtypes[i];
Note: See TracChangeset for help on using the changeset viewer.