1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.expr.Expr; 33 import com.caucho.quercus.module.ModuleContext; 34 35 import java.lang.reflect.Method ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.Comparator ; 39 40 43 public class JavaOverloadMethod extends AbstractJavaMethod { 44 private final JavaMethod []_methods; 45 46 51 public JavaOverloadMethod(ModuleContext moduleContext, 52 ArrayList <Method > methods) 53 { 54 Collections.sort(methods, OVERLOAD_COMPARATOR); 55 56 int length = methods.get(methods.size() - 1).getParameterTypes().length; 57 58 _methods = new JavaMethod[length + 1]; 59 60 for (int i = 0; i < methods.size(); i++) { 61 Method method = methods.get(i); 62 63 JavaMethod javaMethod = new JavaMethod(moduleContext, method); 64 65 Class []param = method.getParameterTypes(); 66 67 for (int j = param.length; j >= 0; j--) { 68 if (_methods[j] == null) 69 _methods[j] = javaMethod; 70 } 71 } 72 } 73 74 77 public Value call(Env env, Object obj, Expr []args) 78 { 79 if (args.length < _methods.length) 80 return _methods[args.length].call(env, obj, args); 81 else 82 return _methods[_methods.length - 1].call(env, obj, args); 83 } 84 85 88 public Value call(Env env, Object obj, Value []args) 89 { 90 if (args.length < _methods.length) 91 return _methods[args.length].call(env, obj, args); 92 else 93 return _methods[_methods.length - 1].call(env, obj, args); 94 } 95 96 public String toString() 97 { 98 return "JavaOverloadMethod[" + _methods[0] + "]"; 99 } 100 101 private static final Comparator <Method > OVERLOAD_COMPARATOR 102 = new Comparator <Method >() { 103 public int compare(Method a, Method b) 104 { 105 return a.getParameterTypes().length - b.getParameterTypes().length; 106 } 107 }; 108 } 109 | Popular Tags |