1 28 29 package com.caucho.es.parser; 30 31 import com.caucho.es.Call; 32 import com.caucho.es.wrapper.ESBeanInfo; 33 import com.caucho.es.wrapper.ESIntrospector; 34 import com.caucho.es.wrapper.ESMethodDescriptor; 35 36 import java.util.ArrayList ; 37 38 42 class JavaMethod { 43 46 static ESMethodDescriptor bestMethod(Class cl, String name, 47 boolean isStatic, ArrayList args) 48 { 49 ESBeanInfo beanInfo; 50 51 try { 52 beanInfo = ESIntrospector.getBeanInfo(cl); 53 } catch (Exception e) { 54 return null; 55 } 56 57 ArrayList overload; 58 59 if (isStatic) 60 overload = beanInfo.getStaticMethods(name); 61 else 62 overload = beanInfo.getMethods(name); 63 64 if (overload == null) 65 return null; 66 67 ESMethodDescriptor []methods; 68 69 if (overload.size() > 2) { 71 methods = (ESMethodDescriptor []) overload.get(2); 72 for (int i = 0; methods != null && i < methods.length; i++) { 73 Class []param = methods[i].getParameterTypes(); 74 75 if (param[0].equals(Call.class) && param[1].equals(int.class)) 76 return null; 77 } 78 } 79 80 methods = (ESMethodDescriptor []) overload.get(args.size()); 81 82 if (methods == null) 83 return null; 84 85 ESMethodDescriptor bestMethod = null; 86 int bestCost = Integer.MAX_VALUE; 87 88 for (int i = 0; i < methods.length; i++) { 89 ESMethodDescriptor method = methods[i]; 90 91 92 Class []param = method.getParameterTypes(); 93 94 int cost = methodCost(param, args); 95 96 if (cost < bestCost && cost < 1000000) { 97 bestCost = cost; 98 bestMethod = method; 99 } 100 } 101 102 return bestMethod; 103 } 104 105 108 static int methodCost(Class []param, ArrayList args) 109 { 110 int cost = 0; 111 112 if (param.length < args.size()) 113 cost += (args.size() - param.length) * 10000000; 114 115 for (int j = 0; j < param.length; j++) { 116 if (j >= args.size()) { 117 cost += 1000000; 118 continue; 119 } 120 Expr arg = (Expr) args.get(j); 121 Class argType = arg.getJavaClass(); 122 123 if (argType == null) 124 cost += 1000; 125 else if (argType.equals(param[j])) { 126 } 127 else if (param[j].isAssignableFrom(argType)) 128 cost += 10; 129 else if (argType.isAssignableFrom(param[j])) 130 cost += 100; 131 else 132 cost += 1000000; 133 } 134 135 return cost; 136 } 137 } 138 | Popular Tags |