// // JavaMethod.java : Method の wrapper クラス定義 (一般 Java Class 向け) // // This is a part of miolisp sourcefile // Copyright (C) 1998 Nishiyama, Naoki / Mio software lab. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // import java.lang.reflect.* ; class JavaMethod { static final int INITSIZE = 2 ; // Initial size of m static final int ADDSIZE = 10 ; // Amount of increase int nmethods = 0 ; // Number of registered methods public Method[] m ; // Array of same name methods JavaMethod() {m = new Method[INITSIZE];} JavaMethod(Method x) { this() ; m[0] = x ; nmethods++ ; } void addMethod(Method x) { // reject same method for (int i = 0 ; i < m.length ; i++) { if (m[i] == x) return ; } if (nmethods >= m.length) { Method[] nm = new Method[m.length + ADDSIZE] ; for (int i = 0 ; i < m.length ; i++) nm[i] = m[i] ; // copy m m = nm ; // dispose m } m[nmethods++] = x ; } void pull1st(int index) { if (index >= nmethods) return ; Method tmp = m[0] ; m[0] = m[index] ; m[index] = tmp ; } // Evaluate with suitable method Object eval(Object cd,Environment e) throws Exception { Cons c = Evalutil.mustbecons("JavaMethod :",cd) ; Object args = Evalutil.evalall(-1,-1,c.cdr,e) ; Object[] argarray = Evalutil.listtoarg(args) ; for (int i=0;i < nmethods;i++) { try { // System.out.println("Applying->"+m[i]) ; Object r = m[i].invoke(c.car,argarray) ; if (i != 0) pull1st(i) ; return r ; } catch(IllegalArgumentException ex) { if (i+1 < nmethods) continue ; // fail , but try next ... throw (Exception)ex ; // all methods failed. } catch(InvocationTargetException ex) { throw (Exception)ex.getTargetException() ; } catch(Exception ex) { throw ex ; } } return null ; // Never come here .... } }