001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
015: * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
016: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
017: * Copyright (C) 2004 Charles O Nutter <headius@headius.com>
018: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
019: *
020: * Alternatively, the contents of this file may be used under the terms of
021: * either of the GNU General Public License Version 2 or later (the "GPL"),
022: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
023: * in which case the provisions of the GPL or the LGPL are applicable instead
024: * of those above. If you wish to allow use of your version of this file only
025: * under the terms of either the GPL or the LGPL, and not to allow others to
026: * use your version of this file under the terms of the CPL, indicate your
027: * decision by deleting the provisions above and replace them with the notice
028: * and other provisions required by the GPL or the LGPL. If you do not delete
029: * the provisions above, a recipient may use your version of this file under
030: * the terms of any one of the CPL, the GPL or the LGPL.
031: ***** END LICENSE BLOCK *****/package org.jruby.runtime;
032:
033: import org.jruby.Ruby;
034: import org.jruby.runtime.callback.Callback;
035: import org.jruby.runtime.callback.ReflectionCallbackFactory;
036: import org.jruby.runtime.callback.InvocationCallbackFactory;
037: import org.jruby.runtime.callback.DumpingInvocationCallbackFactory;
038: import org.jruby.util.JRubyClassLoader;
039:
040: /**
041: * Helper class to build Callback method.
042: * This impements method corresponding to the signature of method most often found in
043: * the Ruby library, for methods with other signature the appropriate Callback object
044: * will need to be explicitly created.
045: **/
046: public abstract class CallbackFactory {
047: public static final Class[] NULL_CLASS_ARRAY = new Class[0];
048:
049: /**
050: * gets an instance method with no arguments.
051: * @param method name of the method
052: * @return a CallBack object corresponding to the appropriate method
053: **/
054: public abstract Callback getMethod(String method);
055:
056: public abstract Callback getFastMethod(String method);
057:
058: /**
059: * gets an instance method with 1 argument.
060: * @param method name of the method
061: * @param arg1 the class of the only argument for this method
062: * @return a CallBack object corresponding to the appropriate method
063: **/
064: public abstract Callback getMethod(String method, Class arg1);
065:
066: public abstract Callback getFastMethod(String method, Class arg1);
067:
068: /**
069: * gets an instance method with two arguments.
070: * @param method name of the method
071: * @param arg1 the java class of the first argument for this method
072: * @param arg2 the java class of the second argument for this method
073: * @return a CallBack object corresponding to the appropriate method
074: **/
075: public abstract Callback getMethod(String method, Class arg1,
076: Class arg2);
077:
078: public abstract Callback getFastMethod(String method, Class arg1,
079: Class arg2);
080:
081: /**
082: * gets an instance method with two arguments.
083: * @param method name of the method
084: * @param arg1 the java class of the first argument for this method
085: * @param arg2 the java class of the second argument for this method
086: * @param arg3 the java class of the second argument for this method
087: * @return a CallBack object corresponding to the appropriate method
088: **/
089: public abstract Callback getMethod(String method, Class arg1,
090: Class arg2, Class arg3);
091:
092: public abstract Callback getFastMethod(String method, Class arg1,
093: Class arg2, Class arg3);
094:
095: /**
096: * gets a singleton (class) method without arguments.
097: * @param method name of the method
098: * @return a CallBack object corresponding to the appropriate method
099: **/
100: public abstract Callback getSingletonMethod(String method);
101:
102: public abstract Callback getFastSingletonMethod(String method);
103:
104: /**
105: * gets a singleton (class) method with 1 argument.
106: * @param method name of the method
107: * @param arg1 the class of the only argument for this method
108: * @return a CallBack object corresponding to the appropriate method
109: **/
110: public abstract Callback getSingletonMethod(String method,
111: Class arg1);
112:
113: public abstract Callback getFastSingletonMethod(String method,
114: Class arg1);
115:
116: /**
117: * gets a singleton (class) method with 2 arguments.
118: * @param method name of the method
119: * @return a CallBack object corresponding to the appropriate method
120: **/
121: public abstract Callback getSingletonMethod(String method,
122: Class arg1, Class arg2);
123:
124: public abstract Callback getFastSingletonMethod(String method,
125: Class arg1, Class arg2);
126:
127: /**
128: * gets a singleton (class) method with 3 arguments.
129: * @param method name of the method
130: * @return a CallBack object corresponding to the appropriate method
131: **/
132: public abstract Callback getSingletonMethod(String method,
133: Class arg1, Class arg2, Class arg3);
134:
135: public abstract Callback getFastSingletonMethod(String method,
136: Class arg1, Class arg2, Class arg3);
137:
138: public abstract Callback getBlockMethod(String method);
139:
140: public abstract CompiledBlockCallback getBlockCallback(String method);
141:
142: /**
143: * gets a singleton (class) method with no mandatory argument and some optional arguments.
144: * @param method name of the method
145: * @return a CallBack object corresponding to the appropriate method
146: **/
147: public abstract Callback getOptSingletonMethod(String method);
148:
149: public abstract Callback getFastOptSingletonMethod(String method);
150:
151: /**
152: * gets an instance method with no mandatory argument and some optional arguments.
153: * @param method name of the method
154: * @return a CallBack object corresponding to the appropriate method
155: **/
156: public abstract Callback getOptMethod(String method);
157:
158: public abstract Callback getFastOptMethod(String method);
159:
160: private static boolean reflection = false;
161: private static boolean dumping = false;
162: private static String dumpingPath = null;
163:
164: static {
165: if (Ruby.isSecurityRestricted())
166: reflection = true;
167: else {
168: if (System.getProperty("jruby.reflection") != null
169: && Boolean.getBoolean("jruby.reflection")) {
170: reflection = true;
171: }
172: if (System.getProperty("jruby.dump_invocations") != null) {
173: dumping = true;
174: dumpingPath = System.getProperty(
175: "jruby.dump_invocations").toString();
176: }
177: }
178: }
179:
180: public static CallbackFactory createFactory(Ruby runtime, Class type) {
181: if (reflection) {
182: return new ReflectionCallbackFactory(type);
183: } else if (dumping) {
184: return new DumpingInvocationCallbackFactory(runtime, type,
185: dumpingPath);
186: } else {
187: return new InvocationCallbackFactory(runtime, type, runtime
188: .getJRubyClassLoader());
189: }
190: }
191:
192: public static CallbackFactory createFactory(Ruby runtime,
193: Class type, ClassLoader classLoader) {
194: if (reflection) {
195: return new ReflectionCallbackFactory(type);
196: } else if (dumping) {
197: return new DumpingInvocationCallbackFactory(runtime, type,
198: dumpingPath);
199: } else {
200: // FIXME: No, I don't like it.
201: return new InvocationCallbackFactory(runtime, type,
202: (JRubyClassLoader) classLoader);
203: }
204: }
205: }
|