|
| 1 | +/***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 |
| 3 | + * |
| 4 | + * The contents of this file are subject to the Eclipse 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/epl-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 Alan Moore <alan_moore@gmx.net> |
| 15 | + * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de> |
| 16 | + * Copyright (C) 2002 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 EPL, 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 EPL, the GPL or the LGPL. |
| 31 | + ***** END LICENSE BLOCK *****/ |
| 32 | +package org.jruby; |
| 33 | + |
| 34 | +import org.jruby.anno.JRubyClass; |
| 35 | +import org.jruby.anno.JRubyMethod; |
| 36 | +import org.jruby.ext.jruby.JRubyLibrary; |
| 37 | +import org.jruby.internal.runtime.methods.DynamicMethod; |
| 38 | +import org.jruby.internal.runtime.methods.ProcMethod; |
| 39 | +import org.jruby.runtime.Block; |
| 40 | +import org.jruby.runtime.BlockBody; |
| 41 | +import org.jruby.runtime.ClassIndex; |
| 42 | +import org.jruby.runtime.CompiledBlockCallback19; |
| 43 | +import org.jruby.runtime.CompiledBlockLight19; |
| 44 | +import org.jruby.runtime.ObjectAllocator; |
| 45 | +import org.jruby.runtime.PositionAware; |
| 46 | +import org.jruby.runtime.ThreadContext; |
| 47 | +import org.jruby.runtime.Visibility; |
| 48 | +import org.jruby.runtime.builtin.IRubyObject; |
| 49 | +import org.jruby.runtime.marshal.DataType; |
| 50 | + |
| 51 | +/** |
| 52 | + * The RubyMethod class represents a RubyMethod object. |
| 53 | + * |
| 54 | + * You can get such a method by calling the "method" method of an object. |
| 55 | + * |
| 56 | + * Note: This was renamed from Method.java |
| 57 | + * |
| 58 | + * @author jpetersen |
| 59 | + * @since 0.2.3 |
| 60 | + */ |
| 61 | +public abstract class AbstractRubyMethod extends RubyObject implements DataType { |
| 62 | + protected RubyModule implementationModule; |
| 63 | + protected String methodName; |
| 64 | + protected RubyModule originModule; |
| 65 | + protected String originName; |
| 66 | + protected DynamicMethod method; |
| 67 | + |
| 68 | + protected AbstractRubyMethod(Ruby runtime, RubyClass rubyClass) { |
| 69 | + super(runtime, rubyClass); |
| 70 | + } |
| 71 | + |
| 72 | + public DynamicMethod getMethod() { |
| 73 | + return method; |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns the number of arguments a method accepted. |
| 77 | + * |
| 78 | + * @return the number of arguments of a method. |
| 79 | + */ |
| 80 | + @JRubyMethod(name = "arity") |
| 81 | + public RubyFixnum arity() { |
| 82 | + return getRuntime().newFixnum(method.getArity().getValue()); |
| 83 | + } |
| 84 | + |
| 85 | + @JRubyMethod(name = "eql?", required = 1, compat = CompatVersion.RUBY1_9) |
| 86 | + public IRubyObject op_eql19(ThreadContext context, IRubyObject other) { |
| 87 | + return op_equal(context, other); |
| 88 | + } |
| 89 | + |
| 90 | + public abstract AbstractRubyMethod rbClone(); |
| 91 | + |
| 92 | + @JRubyMethod(name = "name", compat = CompatVersion.RUBY1_8) |
| 93 | + public IRubyObject name(ThreadContext context) { |
| 94 | + return context.runtime.newString(methodName); |
| 95 | + } |
| 96 | + |
| 97 | + @JRubyMethod(name = "name", compat = CompatVersion.RUBY1_9) |
| 98 | + public IRubyObject name19(ThreadContext context) { |
| 99 | + return context.runtime.newSymbol(methodName); |
| 100 | + } |
| 101 | + |
| 102 | + public String getMethodName() { |
| 103 | + return methodName; |
| 104 | + } |
| 105 | + |
| 106 | + @JRubyMethod(name = "owner") |
| 107 | + public IRubyObject owner(ThreadContext context) { |
| 108 | + return implementationModule; |
| 109 | + } |
| 110 | + |
| 111 | + @JRubyMethod(name = "source_location", compat = CompatVersion.RUBY1_9) |
| 112 | + public IRubyObject source_location(ThreadContext context) { |
| 113 | + Ruby runtime = context.runtime; |
| 114 | + |
| 115 | + String filename = getFilename(); |
| 116 | + if (filename != null) { |
| 117 | + return runtime.newArray(runtime.newString(filename), runtime.newFixnum(getLine())); |
| 118 | + } |
| 119 | + |
| 120 | + return context.runtime.getNil(); |
| 121 | + } |
| 122 | + |
| 123 | + public String getFilename() { |
| 124 | + DynamicMethod realMethod = method.getRealMethod(); // Follow Aliases |
| 125 | + if (realMethod instanceof PositionAware) { |
| 126 | + PositionAware poser = (PositionAware) realMethod; |
| 127 | + return poser.getFile(); |
| 128 | + } |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + public int getLine() { |
| 133 | + DynamicMethod realMethod = method.getRealMethod(); // Follow Aliases |
| 134 | + if (realMethod instanceof PositionAware) { |
| 135 | + PositionAware poser = (PositionAware) realMethod; |
| 136 | + return poser.getLine() + 1; |
| 137 | + } |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + @JRubyMethod(name = "parameters", compat = CompatVersion.RUBY1_9) |
| 142 | + public IRubyObject parameters(ThreadContext context) { |
| 143 | + return JRubyLibrary.MethodExtensions.methodArgs(this); |
| 144 | + } |
| 145 | +} |
| 146 | + |
0 commit comments