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) 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 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.embed.internal;
 
 import org.jruby.Ruby;
Implementation of org.jruby.embed.EmbedRubyObjectAdapter. Users get an instance of this class by newObjectAdapter() method of org.jruby.embed.ScriptingContainer.

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 public class EmbedRubyObjectAdapterImpl implements EmbedRubyObjectAdapter {
     private RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
     private ScriptingContainer container;
 
     public enum MethodType {
         CALLMETHOD_NOARG,
         CALLMETHOD,
         CALLMETHOD_WITHBLOCK,
         CALLSUPER,
         CALLSUPER_WITHBLOCK
     }
 
     public EmbedRubyObjectAdapterImpl(ScriptingContainer container) {
         this. = container;
     }
 
     public boolean isKindOf(IRubyObject valueRubyModule rubyModule) {
         return .isKindOf(valuerubyModule);
     }
     
     public IRubyObject[] convertToJavaArray(IRubyObject array) {
         return .convertToJavaArray(array);
     }
 
     public RubyInteger convertToRubyInteger(IRubyObject obj) {
         return .convertToRubyInteger(obj);
     }
     
     public RubyString convertToRubyString(IRubyObject obj) {
         return .convertToRubyString(obj);
     }
 
     public IRubyObject setInstanceVariable(IRubyObject objString variableNameIRubyObject value) {
         BiVariableMap map = .getVarMap();
         synchronized (map) {
             if (map.containsKey(variableName)) {
                 BiVariable bv = map.getVariable((RubyObject).getProvider().getRuntime().getTopSelf(), variableName);
                bv.setRubyObject(value);
            } else {
                InstanceVariable iv = new InstanceVariable(objvariableNamevalue);
                map.update(variableNameiv);
            }
        }
        return obj.getInstanceVariables().setInstanceVariable(variableNamevalue);
    }
    public IRubyObject getInstanceVariable(IRubyObject objString variableName) {
        BiVariableMap map = .getVarMap();
        synchronized (map) {
            if (map.containsKey(variableName)) {
                BiVariable bv = map.getVariable((RubyObject).getProvider().getRuntime().getTopSelf(), variableName);
                return bv.getRubyObject();
            }
        }
        return null;
    }
    public IRubyObject callMethod(IRubyObject receiverString methodName) {
        return .callMethod(receivermethodName);
    }
    public IRubyObject callMethod(IRubyObject receiverString methodNameIRubyObject singleArg) {
        return .callMethod(receivermethodNamesingleArg);
    }
    public IRubyObject callMethod(IRubyObject receiverString methodNameIRubyObject[] args) {
        return .callMethod(receivermethodNameargs);
    }
    public IRubyObject callMethod(IRubyObject receiverString methodNameIRubyObject[] argsBlock block) {
        return .callMethod(receivermethodNameargsblock);
    }
    public IRubyObject callSuper(IRubyObject receiverIRubyObject[] args) {
        return .callSuper(receiverargs);
    }
    public IRubyObject callSuper(IRubyObject receiverIRubyObject[] argsBlock block) {
        return .callSuper(receiverargsblock);
    }
    public <T> T callMethod(Object receiverString methodNameClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNamenullnull);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callMethod(Object receiverString methodNameObject singleArgClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNamenullnullsingleArg);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callMethod(Object receiverString methodNameObject[] argsClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNamenullnullargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callMethod(Object receiverString methodNameObject[] argsBlock blockClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNameblocknullargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callMethod(Object receiverString methodNameClass<T> returnTypeEmbedEvalUnit unit) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNamenullunit);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    
    public <T> T callMethod(Object receiverString methodNameObject[] argsClass<T> returnTypeEmbedEvalUnit unit) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNamenullunitargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callMethod(Object receiverString methodNameObject[] argsBlock blockClass<T> returnTypeEmbedEvalUnit unit) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivermethodNameblockunitargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callSuper(Object receiverObject[] argsClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivernullnullnullargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public <T> T callSuper(Object receiverObject[] argsBlock blockClass<T> returnType) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.returnTyperubyReceivernullblocknullargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public Object callMethod(Object receiverString methodNameObject... args) {
        try {
            RubyObject rubyReceiver = getReceiverObject(receiver);
            if (args.length == 0) {
                return call(.Object.classrubyReceivermethodNamenullnull);
            } else {
                return call(.Object.classrubyReceivermethodNamenullnullargs);
            }
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    public Object callMethod(Object receiverString methodNameBlock blockObject... args) {
        try {
            if (args.length == 0) {
                throw new IllegalArgumentException("needs at least one argument in a method");
            }
            RubyObject rubyReceiver = getReceiverObject(receiver);
            return call(.Object.classrubyReceivermethodNameblocknullargs);
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    
    public <T> T runRubyMethod(Class<T> returnTypeObject receiverString methodNameBlock blockObject... args) {
        try {
            RubyObject rubyReceiver = (RubyObject)JavaEmbedUtils.javaToRuby(.getProvider().getRuntime(), receiver);
            if (args.length == 0) {
                return call(.returnTyperubyReceivermethodNameblocknull);
            } else {
                return call(.returnTyperubyReceivermethodNameblocknullargs);
            }
        } catch (InvokeFailedException e) {
            throw e;
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        }
    }
    private <T> T call(MethodType typeClass<T> returnTypeRubyObject rubyReceiverString methodNameBlock blockEmbedEvalUnit unitObject... args) {
        if (methodName == null || methodName.length()==0) {
            return null;
        }
        Ruby runtime = .getProvider().getRuntime();
        
        boolean sharing_variables = true;
        if (obj != null && obj instanceof Boolean && ((Booleanobj) == false) {
            sharing_variables = false;
        }
        try {
            if (sharing_variables) {
                ManyVarsDynamicScope scope;
                if (unit != null && unit.getScope() != nullscope = unit.getScope();
                else scope = EmbedRubyRuntimeAdapterImpl.getManyVarsDynamicScope(, 0);
                .getVarMap().inject(scope, 0, rubyReceiver);
                runtime.getCurrentContext().pushScope(scope);
            }
            IRubyObject result = callEachType(typerubyReceivermethodNameblockargs);
            if (sharing_variables) {
                .getVarMap().retrieve(rubyReceiver);
            }
            if (!(result instanceof RubyNil) && returnType != null) {
                Object ret = JavaEmbedUtils.rubyToJava(runtimeresultreturnType);
                return ret != null ? returnType.cast(ret) : null;
            }
            return null;
        } catch (RaiseException e) {
            runtime.printError(e.getException());
            throw new InvokeFailedException(e.getMessage(), e);
        } catch (Throwable e) {
            throw new InvokeFailedException(e);
        } finally {
            if (sharing_variables) {
                runtime.getCurrentContext().popScope();
            }
        }
    }
    
    private RubyObject getReceiverObject(Object receiver) {
        Ruby runtime = .getProvider().getRuntime();
        if (receiver == null || !(receiver instanceof IRubyObject)) {
            return (RubyObject)runtime.getTopSelf();
        }
        else if (receiver instanceof RubyObjectreturn (RubyObject)receiver;
        else return (RubyObject)((IRubyObject)receiver).getRuntime().getTopSelf();
    }
    private IRubyObject callEachType(MethodType typeIRubyObject rubyReceiverString methodNameBlock blockObject... args) {
        Ruby runtime = rubyReceiver.getRuntime();
        IRubyObject[] rubyArgs = null;
        if (args != null && args.length > 0) {
            rubyArgs = JavaUtil.convertJavaArrayToRuby(runtimeargs);
            for (int i = 0; i < rubyArgs.lengthi++) {
                IRubyObject obj = rubyArgs[i];
                if (obj instanceof JavaObject) {
                    rubyArgs[i] = Java.wrap(runtimeobj);
                }
            }
        }
        ThreadContext context = runtime.getCurrentContext();
            switch (type) {
                case :
                    return RuntimeHelpers.invoke(contextrubyReceivermethodName);
                case :
                    return RuntimeHelpers.invoke(contextrubyReceivermethodNamerubyArgs);
                case :
                    return RuntimeHelpers.invoke(contextrubyReceivermethodNamerubyArgsblock);
                case :
                    return RuntimeHelpers.invokeSuper(contextrubyReceiverrubyArgs.);
                case :
                    return RuntimeHelpers.invokeSuper(contextrubyReceiverrubyArgsblock);
                default:
                    break;
            }
        return null;
    }
New to GrepCode? Check out our FAQ X