KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > JavaOverloadMethod


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.Comparator JavaDoc;
39
40 /**
41  * Represents the introspected static function information.
42  */

43 public class JavaOverloadMethod extends AbstractJavaMethod {
44   private final JavaMethod []_methods;
45
46   /**
47    * Creates the statically introspected function.
48    *
49    * @param method the introspected method.
50    */

51   public JavaOverloadMethod(ModuleContext moduleContext,
52                 ArrayList JavaDoc<Method JavaDoc> 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 JavaDoc method = methods.get(i);
62
63       JavaMethod javaMethod = new JavaMethod(moduleContext, method);
64       
65       Class JavaDoc []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   /**
75    * Evaluates the function.
76    */

77   public Value call(Env env, Object JavaDoc 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   /**
86    * Evaluates the function.
87    */

88   public Value call(Env env, Object JavaDoc 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 JavaDoc toString()
97   {
98     return "JavaOverloadMethod[" + _methods[0] + "]";
99   }
100
101   private static final Comparator JavaDoc<Method JavaDoc> OVERLOAD_COMPARATOR
102     = new Comparator JavaDoc<Method JavaDoc>() {
103       public int compare(Method JavaDoc a, Method JavaDoc b)
104       {
105     return a.getParameterTypes().length - b.getParameterTypes().length;
106       }
107     };
108 }
109
Popular Tags