Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2005 Thomas E Enebo <enebo@acm.org> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 
 package org.jruby.internal.runtime.methods;
 
 import org.jruby.Ruby;
DynamicMethod represents a method handle in JRuby, to provide both entry points into AST and bytecode interpreters, but also to provide handles to JIT-compiled and hand-implemented Java methods. All methods invokable from Ruby code are referenced by method handles, either directly or through delegation or callback mechanisms.
 
 public abstract class DynamicMethod {
    
The Ruby module or class in which this method is immediately defined.
 
     protected RubyModule implementationClass;
    
The "protected class" used for calculating protected access.
 
     protected RubyModule protectedClass;
    
The visibility of this method.
 
     protected Visibility visibility;
    
The "call configuration" to use for pre/post call logic.
 
     protected CallConfiguration callConfig;
    
The serial number for this method object, to globally identify it
 
     protected long serialNumber;
    
Is this a builtin core method or not
 
     protected boolean builtin = false;
    
Single-arity native call
 
     protected NativeCall nativeCall;
    
Alternate-arity NativeCalls
 
     protected NativeCall[] nativeCalls = new NativeCall[10];
    
The simple, base name this method was defined under. May be null
 
     protected String name;
    
Whether this method is "not implemented".
 
     protected boolean notImplemented = false;
    
An arbitrarily-typed "method handle" for use by compilers and call sites
 
     protected Object handle;

    
Base constructor for dynamic method handles.

Parameters:
implementationClass The class to which this method will be immediately bound
visibility The visibility assigned to this method
callConfig The CallConfiguration to use for this method's pre/post invocation logic.
 
     protected DynamicMethod(RubyModule implementationClassVisibility visibilityCallConfiguration callConfig) {
         assert implementationClass != null;
         init(implementationClassvisibilitycallConfig);
     }

    
Base constructor for dynamic method handles with names.

Parameters:
implementationClass The class to which this method will be immediately bound
visibility The visibility assigned to this method
callConfig The CallConfiguration to use for this method's pre/post invocation logic.
    protected DynamicMethod(RubyModule implementationClassVisibility visibilityCallConfiguration callConfigString name) {
        this(implementationClassvisibilitycallConfig);
        this. = name;
    }

    
A no-arg constructor used only by the UndefinedMethod subclass and CompiledMethod handles. instanceof assertions make sure this is so.
    protected DynamicMethod() {
//        assert (this instanceof UndefinedMethod ||
//                this instanceof CompiledMethod ||
//                this instanceof );
    }
    protected void init(RubyModule implementationClassVisibility visibilityCallConfiguration callConfig) {
        this. = visibility;
        this. = implementationClass;
        // TODO: Determine whether we should perhaps store non-singleton class
        // in the implementationClass
        this. = calculateProtectedClass(implementationClass);
        this. = callConfig;
        this. = implementationClass.getRuntime().getNextDynamicMethodSerial();
    }

    
Get the global serial number for this method object

Returns:
This method object's serial number
    public long getSerialNumber() {
        return ;
    }
    public boolean isBuiltin() {
        return ;
    }
    public void setIsBuiltin(boolean isBuiltin) {
        this. = isBuiltin;
    }

    
The minimum 'call' method required for a dynamic method handle. Subclasses must impleemnt this method, but may implement the other signatures to provide faster, non-boxing call paths. Typically subclasses will implement this method to check variable arity calls, then performing a specific-arity invocation to the appropriate method or performing variable-arity logic in-line.

Parameters:
context The thread context for the currently executing thread
self The 'self' or 'receiver' object to use for this call
klazz The Ruby class against which this method is binding
name The incoming name used to invoke this method
args The argument list to this invocation
block The block passed to this invocation
Returns:
The result of the call
    public abstract IRubyObject call(ThreadContext contextIRubyObject selfRubyModule clazz,
            String nameIRubyObject[] argsBlock block);

    
A default implementation of n-arity, non-block 'call' method, which simply calls the n-arity, block-receiving version with the arg list and Block.NULL_BLOCK.

Parameters:
context The thread context for the currently executing thread
self The 'self' or 'receiver' object to use for this call
klazz The Ruby class against which this method is binding
name The incoming name used to invoke this method
arg1 The first argument to this invocation
arg2 The second argument to this invocation
Returns:
The result of the call
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule clazz,
            String nameIRubyObject[] args) {
        return call(contextselfclazznameargs.);
    }
    ////////////////////////////////////////////////////////////////////////////
    // Now we provide default impls of a number of signatures. For each arity,
    // we first generate a non-block version of the method, which just adds
    // NULL_BLOCK and re-calls, allowing e.g. compiled code, which always can
    // potentially take a block, to only generate the block-receiving signature
    // and still avoid arg boxing.
    //
    // We then provide default implementations of each block-accepting method
    // that in turn call the IRubyObject[]+Block version of call. This then
    // finally falls back on the minimum implementation requirement for
    // dynamic method handles.
    ////////////////////////////////////////////////////////////////////////////

    
Arity 0, no block
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString name) {
        return call(contextselfklazzname.);
    }
    
Arity 0, with block; calls through IRubyObject[] path
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameBlock block) {
        return call(contextselfklazzname.block);
    }
    
Arity 1, no block
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0) {
        return call(contextselfklazznamearg0.);
    }
    
Arity 1, with block; calls through IRubyObject[] path
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0Block block) {
        return call(contextselfklazznamenew IRubyObject[] {arg0}, block);
    }
    
Arity 2, no block
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0IRubyObject arg1) {
        return call(contextselfklazznamearg0arg1.);
    }
    
Arity 2, with block; calls through IRubyObject[] path
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0IRubyObject arg1Block block) {
        return call(contextselfklazznamenew IRubyObject[] {arg0arg1}, block);
    }
    
Arity 3, no block
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0IRubyObject arg1IRubyObject arg2) {
        return call(contextselfklazznamearg0arg1arg2.);
    }
    
Arity 3, with block; calls through IRubyObject[] path
    public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString nameIRubyObject arg0IRubyObject arg1IRubyObject arg2Block block) {
        return call(contextselfklazznamenew IRubyObject[] {arg0arg1arg2}, block);
    }



    
Duplicate this method, returning DynamicMethod referencing the same code and with the same attributes. It is not required that this method produce a new object if the semantics of the DynamicMethod subtype do not require such.

Returns:
An identical DynamicMethod object to the target.
    public abstract DynamicMethod dup();

    
Determine whether this method is callable from the given object using the given call type.

Parameters:
caller The calling object
callType The type of call
Returns:
true if the call would not violate visibility; false otherwise
    public boolean isCallableFrom(IRubyObject callerCallType callType) {
        switch () {
        case :
            return true;
        case :
            return callType != .;
        case :
            return protectedAccessOk(caller);
        }
        return true;
    }

    
Determine whether the given object can safely invoke protected methods on the class this method is bound to.

Parameters:
caller The calling object
Returns:
true if the calling object can call protected methods; false otherwise
    private boolean protectedAccessOk(IRubyObject caller) {
        return getProtectedClass().isInstance(caller);
    }

    
Calculate, based on given RubyModule, which class in its hierarchy should be used to determine protected access.

Parameters:
cls The class from which to calculate
Returns:
The class to be used for protected access checking.
    protected static RubyModule calculateProtectedClass(RubyModule cls) {
        // singleton classes don't get their own visibility domain
        if (cls.isSingleton()) cls = cls.getSuperClass();
        while (cls.isIncluded()) cls = cls.getMetaClass();
        // For visibility we need real meta class and not anonymous one from class << self
        if (cls instanceof MetaClasscls = ((MetaClasscls).getRealClass();
        return cls;
    }

    
Retrieve the pre-calculated "protected class" used for access checks.

Returns:
The "protected class" for access checks.
    protected RubyModule getProtectedClass() {
        return ;
    }

    
Retrieve the class or module on which this method is implemented, used for 'super' logic among others.

Returns:
The class on which this method is implemented
        return ;
    }

    
Set the class on which this method is implemented, used for 'super' logic, among others.

Parameters:
implClass The class on which this method is implemented
    public void setImplementationClass(RubyModule implClass) {
         = implClass;
         = calculateProtectedClass(implClass);
    }

    
Get the visibility of this method.

Returns:
The visibility of this method
    public Visibility getVisibility() {
        return ;
    }

    
Set the visibility of this method.

Parameters:
visibility The visibility of this method
    public void setVisibility(Visibility visibility) {
        this. = visibility;
    }

    
Whether this method is the "undefined" method, used to represent a missing or undef'ed method. Only returns true for UndefinedMethod instances, of which there should be only one (a singleton).

Returns:
true if this method is the undefined method; false otherwise
    public final boolean isUndefined() {
        return this == .;
    }

    
Retrieve the arity of this method, used for reporting arity to Ruby code. This arity may or may not reflect the actual specific or variable arities of the referenced method.

Returns:
The arity of the method, as reported to Ruby consumers.
    public Arity getArity() {
        return Arity.optional();
    }

    
Get the "real" method contained within this method. This simply returns self except in cases where a method is wrapped to give it a new name or new implementation class (AliasMethod, WrapperMethod, ...).

Returns:
The "real" method associated with this one
    public DynamicMethod getRealMethod() {
        return this;
    }

    
Get the CallConfiguration used for pre/post logic for this method handle.

Returns:
The CallConfiguration for this method handle
    public CallConfiguration getCallConfig() {
        return ;
    }

    
Set the CallConfiguration used for pre/post logic for this method handle.

Parameters:
callConfig The CallConfiguration for this method handle
    public void setCallConfig(CallConfiguration callConfig) {
        this. = callConfig;
    }
    
    public static class NativeCall {
        private final Class nativeTarget;
        private final String nativeName;
        private final Class nativeReturn;
        private final Class[] nativeSignature;
        private final boolean statik;
        private final boolean java;
        public NativeCall(Class nativeTargetString nativeNameClass nativeReturnClass[] nativeSignatureboolean statik) {
            this(nativeTargetnativeNamenativeReturnnativeSignaturestatikfalse);
        }
        public NativeCall(Class nativeTargetString nativeNameClass nativeReturnClass[] nativeSignatureboolean statikboolean java) {
            this. = nativeTarget;
            this. = nativeName;
            this. = nativeReturn;
            this. = nativeSignature;
            this. = statik;
            this. = java;
        }
        public Class getNativeTarget() {
            return ;
        }
        public String getNativeName() {
            return ;
        }
        public Class getNativeReturn() {
            return ;
        }
        public Class[] getNativeSignature() {
            return ;
        }
        public boolean isStatic() {
            return ;
        }
        
        public boolean isJava() {
            return ;
        }
        public boolean hasContext() {
            return . > 0 && [0] == ThreadContext.class;
        }
        public boolean hasBlock() {
            return . > 0 && [. - 1] == Block.class;
        }
        @Override
        public String toString() {
            return "" + (?"static ":"") + .getSimpleName() + " " + .getSimpleName() + "." +  + CodegenUtils.prettyShortParams();
        }
    }

    
Set the single-arity NativeCall for this method. All signatures for the non-single-arity getNativeCall will also be set to this value.

Parameters:
nativeTarget native method target
nativeName native method name
nativeReturn native method return
nativeSignature native method arguments
statik static?
java plain Java method?
    public void setNativeCall(Class nativeTargetString nativeNameClass nativeReturnClass[] nativeSignatureboolean statikboolean java) {
        this. = new NativeCall(nativeTargetnativeNamenativeReturnnativeSignaturestatikjava);
        Arrays.fill();
    }


    
Set the single-arity NativeCall for this method. All signatures for the non-single-arity getNativeCall will also be set to this value.

Parameters:
nativeTarget native method target
nativeName native method name
nativeReturn native method return
nativeSignature native method arguments
statik static?
    public void setNativeCall(Class nativeTargetString nativeNameClass nativeReturnClass[] nativeSignatureboolean statik) {
        setNativeCall(nativeTargetnativeNamenativeReturnnativeSignaturestatikfalse);
    }
    
    public NativeCall getNativeCall() {
        return this.;
    }
    
    public NativeCall getNativeCall(int argsboolean block) {
        if (args == -1 || args > 3) args = 4;
        if (blockargs += 5;
        return this.[args];
    }
    
    public void setNativeCall(int argsboolean blockNativeCall nativeCall) {
        if (args == -1 || args > 3) args = 4;
        if (blockargs += 5;
        this.[args] = nativeCall;
    }

    
Returns true if this method is backed by native (i.e. Java) code.

Returns:
true If backed by Java code or JVM bytecode; false otherwise
    public boolean isNative() {
        return false;
    }

    
Get the base name this method was defined as.

Returns:
the base name for the method
    public String getName() {
        return ;
    }

    
Set the base name for this method.

Parameters:
name the name to set
    public void setName(String name) {
        this. = name;
    }

    
Get the "handle" associated with this DynamicMethod.

Returns:
the handle
    public Object getHandle() {
        return ;
    }

    
Set the "handle" associated with this DynamicMethod.

Parameters:
handle the handle
    public void setHandle(Object handle) {
        this. = handle;
    }
    
    
Whether this method is "not implemented". This is primarily to support Ruby 1.9's behavior of respond_to? yielding false if the feature in question is unsupported (but still having the method defined).
    public boolean isNotImplemented() {
        return ;
    }
    
    
Set whether this method is "not implemented".
    public void setNotImplemented(boolean setNotImplemented) {
        this. = setNotImplemented;
    }
    protected IRubyObject handleRedo(Ruby runtimethrows RaiseException {
        throw runtime.newLocalJumpError(..runtime.getNil(), "unexpected redo");
    }
    protected IRubyObject handleReturn(ThreadContext contextJumpException.ReturnJump rjint callNumber) {
        if (rj.getTarget() == callNumber) {
            return (IRubyObjectrj.getValue();
        }
        throw rj;
    }
    protected IRubyObject handleBreak(ThreadContext contextRuby runtimeJumpException.BreakJump bjint callNumber) {
        if (bj.getTarget() == callNumber) {
            throw runtime.newLocalJumpError(..runtime.getNil(), "unexpected break");
        }
        throw bj;
    }
New to GrepCode? Check out our FAQ X