KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > CallbackFactory


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Charles O Nutter <headius@headius.com>
18  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby.runtime;
33
34 import org.jruby.Ruby;
35 import org.jruby.runtime.callback.Callback;
36 import org.jruby.runtime.callback.ReflectionCallbackFactory;
37 import org.jruby.runtime.callback.InvocationCallbackFactory;
38 import org.jruby.runtime.callback.DumpingInvocationCallbackFactory;
39
40 /**
41  * Helper class to build Callback method.
42  * This impements method corresponding to the signature of method most often found in
43  * the Ruby library, for methods with other signature the appropriate Callback object
44  * will need to be explicitly created.
45  **/

46 public abstract class CallbackFactory {
47     public static final Class JavaDoc[] NULL_CLASS_ARRAY = new Class JavaDoc[0];
48     
49     /**
50      * gets an instance method with no arguments.
51      * @param method name of the method
52      * @return a CallBack object corresponding to the appropriate method
53      **/

54     public abstract Callback getMethod(String JavaDoc method);
55     public abstract Callback getFastMethod(String JavaDoc method);
56
57     /**
58      * gets an instance method with 1 argument.
59      * @param method name of the method
60      * @param arg1 the class of the only argument for this method
61      * @return a CallBack object corresponding to the appropriate method
62      **/

63     public abstract Callback getMethod(String JavaDoc method, Class JavaDoc arg1);
64     public abstract Callback getFastMethod(String JavaDoc method, Class JavaDoc arg1);
65
66     /**
67      * gets an instance method with two arguments.
68      * @param method name of the method
69      * @param arg1 the java class of the first argument for this method
70      * @param arg2 the java class of the second argument for this method
71      * @return a CallBack object corresponding to the appropriate method
72      **/

73     public abstract Callback getMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2);
74     public abstract Callback getFastMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2);
75     
76     /**
77      * gets an instance method with two arguments.
78      * @param method name of the method
79      * @param arg1 the java class of the first argument for this method
80      * @param arg2 the java class of the second argument for this method
81      * @param arg3 the java class of the second argument for this method
82      * @return a CallBack object corresponding to the appropriate method
83      **/

84     public abstract Callback getMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2, Class JavaDoc arg3);
85     public abstract Callback getFastMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2, Class JavaDoc arg3);
86
87     /**
88      * gets a singleton (class) method without arguments.
89      * @param method name of the method
90      * @return a CallBack object corresponding to the appropriate method
91      **/

92     public abstract Callback getSingletonMethod(String JavaDoc method);
93     public abstract Callback getFastSingletonMethod(String JavaDoc method);
94
95     /**
96      * gets a singleton (class) method with 1 argument.
97      * @param method name of the method
98      * @param arg1 the class of the only argument for this method
99      * @return a CallBack object corresponding to the appropriate method
100      **/

101     public abstract Callback getSingletonMethod(String JavaDoc method, Class JavaDoc arg1);
102     public abstract Callback getFastSingletonMethod(String JavaDoc method, Class JavaDoc arg1);
103
104     /**
105      * gets a singleton (class) method with 2 arguments.
106      * @param method name of the method
107      * @return a CallBack object corresponding to the appropriate method
108      **/

109     public abstract Callback getSingletonMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2);
110     public abstract Callback getFastSingletonMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2);
111
112     /**
113      * gets a singleton (class) method with 3 arguments.
114      * @param method name of the method
115      * @return a CallBack object corresponding to the appropriate method
116      **/

117     public abstract Callback getSingletonMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2, Class JavaDoc arg3);
118     public abstract Callback getFastSingletonMethod(String JavaDoc method, Class JavaDoc arg1, Class JavaDoc arg2, Class JavaDoc arg3);
119
120     public abstract Callback getBlockMethod(String JavaDoc method);
121     public abstract CompiledBlockCallback getBlockCallback(String JavaDoc method);
122
123     /**
124     * gets a singleton (class) method with no mandatory argument and some optional arguments.
125      * @param method name of the method
126     * @return a CallBack object corresponding to the appropriate method
127     **/

128     public abstract Callback getOptSingletonMethod(String JavaDoc method);
129     public abstract Callback getFastOptSingletonMethod(String JavaDoc method);
130
131     /**
132     * gets an instance method with no mandatory argument and some optional arguments.
133      * @param method name of the method
134     * @return a CallBack object corresponding to the appropriate method
135     **/

136     public abstract Callback getOptMethod(String JavaDoc method);
137     public abstract Callback getFastOptMethod(String JavaDoc method);
138
139     private static boolean reflection = false;
140     private static boolean dumping = false;
141     private static String JavaDoc dumpingPath = null;
142
143     static {
144         if(System.getProperty("jruby.reflection") != null && Boolean.getBoolean("jruby.reflection")) {
145             reflection = true;
146         }
147         if(System.getProperty("jruby.dump_invocations") != null) {
148             dumping = true;
149             dumpingPath = System.getProperty("jruby.dump_invocations").toString();
150         }
151     }
152
153     public static CallbackFactory createFactory(Ruby runtime, Class JavaDoc type) {
154         if(reflection) {
155             return new ReflectionCallbackFactory(type);
156         } else if(dumping) {
157             return new DumpingInvocationCallbackFactory(runtime, type, dumpingPath);
158         } else {
159             return new InvocationCallbackFactory(runtime, type);
160         }
161     }
162 }
163
Popular Tags