KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > JavaMethod


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
37
38 /**
39  * Utility class for selecting the best Java method matching the
40  * JavaScript call arguments.
41  */

42 class JavaMethod {
43   /**
44    * Returns the best matching method for the class.
45    */

46   static ESMethodDescriptor bestMethod(Class JavaDoc cl, String JavaDoc name,
47                                        boolean isStatic, ArrayList JavaDoc args)
48   {
49     ESBeanInfo beanInfo;
50
51     try {
52       beanInfo = ESIntrospector.getBeanInfo(cl);
53     } catch (Exception JavaDoc e) {
54       return null;
55     }
56
57     ArrayList JavaDoc 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     // special case of (Call, int)
70
if (overload.size() > 2) {
71       methods = (ESMethodDescriptor []) overload.get(2);
72       for (int i = 0; methods != null && i < methods.length; i++) {
73         Class JavaDoc []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 JavaDoc []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   /**
106    * Return the class of the method.
107    */

108   static int methodCost(Class JavaDoc []param, ArrayList JavaDoc 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 JavaDoc 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