Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse 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/epl-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) 2009-2012 Yoko Harada <yokolet@gmail.com> 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 EPL, 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 EPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 
 package org.jruby.embed.variable;
 
 import java.util.List;
 import org.jruby.Ruby;
This class is responsible to local variable behavior dependent processing.

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 public class VariableInterceptor {
     //private LocalVariableBehavior behavior;
 
    
Constructs an instance with a given local variable behavior.

Parameters:
behavior local variable behavior
 
     //public VariableInterceptor(LocalVariableBehavior behavior) {
     //    this.behavior = behavior;
     //}
     
     //public LocalVariableBehavior getLocalVariableBehavior() {
     //    return behavior;
     //}
 
    
Returns an appropriate type of a variable instance to the specified local variable behavior.

Parameters:
runtime Ruby runtime
name variable name
value variable value
Returns:
an appropriate type of the variable instance.
 
     public static BiVariable getVariableInstance(LocalVariableBehavior behaviorRubyObject receiverString nameObject... value) {
         if (value == null || value.length < 1) {
             return null;
         }
         if ("ARGV".equals(name)) {
             return Argv.getInstance(receivernamevalue);
         }
         switch (behavior) {
             case :
                 return LocalGlobalVariable.getInstance(receivernamevalue);
             case :
                 BiVariable[] bEntries = {
                     PersistentLocalVariable.getInstance(receivernamevalue),
                     GlobalVariable.getInstance(receivernamevalue)
                 };
                 return resolve(bEntries);
             case :
                 BiVariable[] pEntries = {
                     GlobalVariable.getInstance(receivernamevalue),
                     InstanceVariable.getInstance(receivernamevalue),
                     ClassVariable.getInstance(receivernamevalue),
                     Constant.getInstance(receivernamevalue),
                     PersistentLocalVariable.getInstance(receivernamevalue)
                 };
                 return resolve(pEntries);
             default:
                 BiVariable[] tEntries = {
                     GlobalVariable.getInstance(receivernamevalue),
                     InstanceVariable.getInstance(receivernamevalue),
                    ClassVariable.getInstance(receivernamevalue),
                    Constant.getInstance(receivernamevalue),
                    TransientLocalVariable.getInstance(receivernamevalue)
                };
                return resolve(tEntries);
        }
    }
    private static BiVariable resolve(BiVariable[] entries) {
        for (BiVariable e : entries) {
            if (e != null) {
                return e;
            }
        }
        return null;
    }

    
Injects variable values from Java to Ruby just before an evaluation or method invocation.

Parameters:
map a variable map that has name-value pairs to be injected
runtime Ruby runtime
scope scope to inject local variable values
depth depth of a frame to inject local variable values
receiver a receiver when the script has been evaluated once
    public static void inject(BiVariableMap mapRuby runtimeManyVarsDynamicScope scopeint depthIRubyObject receiver) {
        // lvar might not be given while parsing but be given when evaluating.
        // to avoid ArrayIndexOutOfBoundsException, checks the length of scope.getValues()
        if (scope != null && scope.getValues().length > 0) {
            IRubyObject[] values4Injection = map.getLocalVarValues();
            if (values4Injection != null && values4Injection.length > 0) {
                for (int i = 0; i < values4Injection.lengthi++) {
                    scope.setValue(ivalues4Injection[i], depth);
                }
            }
        }
        List<BiVariablevariables = map.getVariables();
        if (variables == nullreturn;
        for (int i=0; i<variables.size(); i++) {
            variables.get(i).inject();
        }
    }

    
Retrieves variable/constant names and values after the evaluation or method invocation.

Parameters:
map variable map that holds retrieved name-value pairs.
runtime Ruby runtime
receiver a receiver when the script has been evaluated once
    public static void retrieve(LocalVariableBehavior behaviorBiVariableMap mapRubyObject receiver) {
        Argv.retrieve(receivermap);
        switch (behavior) {
            case :
                LocalGlobalVariable.retrieve(receivermap);
                break;
            case :
                PersistentLocalVariable.retrieve(receivermap);
                break;
            case :
                PersistentLocalVariable.retrieve(receivermap);
            // continues to the default case
            default:
                InstanceVariable.retrieve(receivermap);
                GlobalVariable.retrieve(receivermap);
                ClassVariable.retrieve(receivermap);
                Constant.retrieve(receivermap);
        }
    }

    
Retrieves specified variable/constant name and value after the evaluation or method invocation only when it is requested.

Parameters:
map variable map that holds retrieved name-value pairs.
runtime Ruby runtime
receiver a receiver when the script has been evaluated once
:
    public static void tryLazyRetrieval(LocalVariableBehavior behaviorBiVariableMap mapIRubyObject receiverObject key) {
        if (Argv.isValidName(key)) {
            Argv.retrieveByKey((RubyObject)receivermap, (String)key);
            return;
        }
        switch (behavior) {
            case :
                if (LocalGlobalVariable.isValidName(key)) {
                    LocalGlobalVariable.retrieveByKey(receiver.getRuntime(), map, (String)key);
                }
                break;
            case :
                break;
            case :
            default:
                if (GlobalVariable.isValidName(key)) {
                    GlobalVariable.retrieveByKey(receiver.getRuntime(), map, (String)key);
                } else if (InstanceVariable.isValidName(key)) {
                    InstanceVariable.retrieveByKey((RubyObjectreceiver,map, (String)key);
                } else if (ClassVariable.isValidName(key)) {
                    ClassVariable.retrieveByKey((RubyObject)receivermap, (String)key);
                } else if (Constant.isValidName(key)) {
                    Constant.retrieveByKey((RubyObject)receivermap, (String)key);
                }
        }
    }

    
Clears global variable values from Ruby runtime to behave the same as JSR 223 reference implementation.

Parameters:
variables a variable list to be cleared from Ruby runtime
runtime Ruby runtime
    public static void terminateGlobalVariables(LocalVariableBehavior behaviorList<BiVariablevariablesRuby runtime) {
        if (variables == nullreturn;
        if (. == behavior) {
            for (int i = 0; i < variables.size(); i++) {
                if (.. == variables.get(i).getType()) {
                    String name = variables.get(i).getName();
                    name = name.startsWith("$") ? name : "$" + name;
                    runtime.getGlobalVariables().set(nameruntime.getNil());
                }
            }
        }
    }

    
Clears local variables form the variable map so that old local variable name-value pairs are not to be used in successive evaluations.

Parameters:
varNames variable name list to be cleared
variables variable value list to be cleared
    public static void terminateLocalVariables(LocalVariableBehavior behaviorList<StringvarNamesList<BiVariablevariables) {
        if (variables == nullreturn;
        if (. == behavior) {
            for (int i = 0; i < variables.size(); i++) {
                if (.. == variables.get(i).getType()) {
                    varNames.remove(i);
                    variables.remove(i);
                }
            }
        }
    }

    
Checks the given name is whether a legal Ruby variable/constant name or not.

Parameters:
name a given name to be checked
Returns:
true when the name is a legal Ruby variable/constant name, otherwise false.
    public static boolean isKindOfRubyVariable(LocalVariableBehavior behaviorString name) {
        if ("ARGV".equals(name)) return true;
        switch (behavior) {
            case :
                return LocalGlobalVariable.isValidName(name);
            case :
                if (PersistentLocalVariable.isValidName(name)) {
                    return true;
                } else if (GlobalVariable.isValidName(name)) {
                    return true;
                }
                return false;
            case :
                if (GlobalVariable.isValidName(name)) {
                    return true;
                } else if (PersistentLocalVariable.isValidName(name)) {
                    return true;
                } else if (InstanceVariable.isValidName(name)) {
                    return true;
                } else if (Constant.isValidName(name)) {
                    return true;
                } else if (ClassVariable.isValidName(name)) {
                    return true;
                }
                return false;
            default:
                if (GlobalVariable.isValidName(name)) {
                    return true;
                } else if (TransientLocalVariable.isValidName(name)) {
                    return true;
                } else if (InstanceVariable.isValidName(name)) {
                    return true;
                } else if (Constant.isValidName(name)) {
                    return true;
                } else if (ClassVariable.isValidName(name)) {
                    return true;
                }
                return false;
        }
    }
New to GrepCode? Check out our FAQ X