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) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
015: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
016: * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
017: * Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
018: *
019: * Alternatively, the contents of this file may be used under the terms of
020: * either of the GNU General Public License Version 2 or later (the "GPL"),
021: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022: * in which case the provisions of the GPL or the LGPL are applicable instead
023: * of those above. If you wish to allow use of your version of this file only
024: * under the terms of either the GPL or the LGPL, and not to allow others to
025: * use your version of this file under the terms of the CPL, indicate your
026: * decision by deleting the provisions above and replace them with the notice
027: * and other provisions required by the GPL or the LGPL. If you do not delete
028: * the provisions above, a recipient may use your version of this file under
029: * the terms of any one of the CPL, the GPL or the LGPL.
030: ***** END LICENSE BLOCK *****/package org.jruby.javasupport;
031:
032: import java.lang.reflect.Modifier;
033:
034: import org.jruby.Ruby;
035: import org.jruby.RubyArray;
036: import org.jruby.RubyBoolean;
037: import org.jruby.RubyClass;
038: import org.jruby.RubyFixnum;
039: import org.jruby.runtime.CallbackFactory;
040: import org.jruby.runtime.builtin.IRubyObject;
041:
042: public abstract class JavaCallable extends JavaAccessibleObject {
043:
044: public JavaCallable(Ruby runtime, RubyClass rubyClass) {
045: super (runtime, rubyClass);
046: }
047:
048: public final RubyFixnum arity() {
049: return getRuntime().newFixnum(getArity());
050: }
051:
052: public final RubyArray argument_types() {
053: Class[] parameterTypes = parameterTypes();
054: RubyArray result = getRuntime().newArray(parameterTypes.length);
055: for (int i = 0; i < parameterTypes.length; i++) {
056: result.append(JavaClass
057: .get(getRuntime(), parameterTypes[i]));
058: }
059: return result;
060: }
061:
062: public IRubyObject inspect() {
063: StringBuffer result = new StringBuffer();
064: result.append(nameOnInspection());
065: Class[] parameterTypes = parameterTypes();
066: for (int i = 0; i < parameterTypes.length; i++) {
067: result.append(parameterTypes[i].getName());
068: if (i < parameterTypes.length - 1) {
069: result.append(',');
070: }
071: }
072: result.append(")>");
073: return getRuntime().newString(result.toString());
074: }
075:
076: protected abstract int getArity();
077:
078: protected abstract Class[] parameterTypes();
079:
080: protected abstract int getModifiers();
081:
082: /**
083: * @return the name used in the head of the string returned from inspect()
084: */
085: protected abstract String nameOnInspection();
086:
087: public RubyBoolean public_p() {
088: return RubyBoolean.newBoolean(getRuntime(), Modifier
089: .isPublic(getModifiers()));
090: }
091:
092: public static void registerRubyMethods(Ruby runtime,
093: RubyClass result, Class klass) {
094: registerRubyMethods(runtime, result);
095:
096: CallbackFactory callbackFactory = runtime
097: .callbackFactory(klass);
098:
099: result.defineFastMethod("public?", callbackFactory
100: .getFastMethod("public_p"));
101: }
102: }
|