001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.Call;
032: import com.caucho.es.wrapper.ESBeanInfo;
033: import com.caucho.es.wrapper.ESIntrospector;
034: import com.caucho.es.wrapper.ESMethodDescriptor;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * Utility class for selecting the best Java method matching the
040: * JavaScript call arguments.
041: */
042: class JavaMethod {
043: /**
044: * Returns the best matching method for the class.
045: */
046: static ESMethodDescriptor bestMethod(Class cl, String name,
047: boolean isStatic, ArrayList args) {
048: ESBeanInfo beanInfo;
049:
050: try {
051: beanInfo = ESIntrospector.getBeanInfo(cl);
052: } catch (Exception e) {
053: return null;
054: }
055:
056: ArrayList overload;
057:
058: if (isStatic)
059: overload = beanInfo.getStaticMethods(name);
060: else
061: overload = beanInfo.getMethods(name);
062:
063: if (overload == null)
064: return null;
065:
066: ESMethodDescriptor[] methods;
067:
068: // special case of (Call, int)
069: if (overload.size() > 2) {
070: methods = (ESMethodDescriptor[]) overload.get(2);
071: for (int i = 0; methods != null && i < methods.length; i++) {
072: Class[] param = methods[i].getParameterTypes();
073:
074: if (param[0].equals(Call.class)
075: && param[1].equals(int.class))
076: return null;
077: }
078: }
079:
080: methods = (ESMethodDescriptor[]) overload.get(args.size());
081:
082: if (methods == null)
083: return null;
084:
085: ESMethodDescriptor bestMethod = null;
086: int bestCost = Integer.MAX_VALUE;
087:
088: for (int i = 0; i < methods.length; i++) {
089: ESMethodDescriptor method = methods[i];
090:
091: Class[] param = method.getParameterTypes();
092:
093: int cost = methodCost(param, args);
094:
095: if (cost < bestCost && cost < 1000000) {
096: bestCost = cost;
097: bestMethod = method;
098: }
099: }
100:
101: return bestMethod;
102: }
103:
104: /**
105: * Return the class of the method.
106: */
107: static int methodCost(Class[] param, ArrayList args) {
108: int cost = 0;
109:
110: if (param.length < args.size())
111: cost += (args.size() - param.length) * 10000000;
112:
113: for (int j = 0; j < param.length; j++) {
114: if (j >= args.size()) {
115: cost += 1000000;
116: continue;
117: }
118: Expr arg = (Expr) args.get(j);
119: Class argType = arg.getJavaClass();
120:
121: if (argType == null)
122: cost += 1000;
123: else if (argType.equals(param[j])) {
124: } else if (param[j].isAssignableFrom(argType))
125: cost += 10;
126: else if (argType.isAssignableFrom(param[j]))
127: cost += 100;
128: else
129: cost += 1000000;
130: }
131:
132: return cost;
133: }
134: }
|