ReflectedCompiledMethod.java

  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) 2007 Peter Brant <peter.brant@gmail.com>
  15.  *
  16.  * Alternatively, the contents of this file may be used under the terms of
  17.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  18.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  19.  * in which case the provisions of the GPL or the LGPL are applicable instead
  20.  * of those above. If you wish to allow use of your version of this file only
  21.  * under the terms of either the GPL or the LGPL, and not to allow others to
  22.  * use your version of this file under the terms of the EPL, indicate your
  23.  * decision by deleting the provisions above and replace them with the notice
  24.  * and other provisions required by the GPL or the LGPL. If you do not delete
  25.  * the provisions above, a recipient may use your version of this file under
  26.  * the terms of any one of the EPL, the GPL or the LGPL.
  27.  ***** END LICENSE BLOCK *****/
  28. package org.jruby.internal.runtime.methods;

  29. import java.lang.reflect.InvocationTargetException;
  30. import java.lang.reflect.Method;

  31. import org.jruby.Ruby;
  32. import org.jruby.RubyModule;
  33. import org.jruby.exceptions.JumpException;
  34. import org.jruby.exceptions.RaiseException;
  35. import org.jruby.lexer.yacc.ISourcePosition;
  36. import org.jruby.parser.StaticScope;
  37. import org.jruby.runtime.Arity;
  38. import org.jruby.runtime.Block;
  39. import org.jruby.runtime.RubyEvent;
  40. import org.jruby.runtime.ThreadContext;
  41. import org.jruby.runtime.Visibility;
  42. import org.jruby.runtime.builtin.IRubyObject;

  43. public class ReflectedCompiledMethod extends CompiledMethod {
  44.     private final Method method;
  45.    
  46.     public ReflectedCompiledMethod(RubyModule implementationClass, Arity arity,
  47.             Visibility visibility, StaticScope staticScope, Object scriptObject, Method method, CallConfiguration callConfig, ISourcePosition position, String parameterDesc) {
  48.         super(null);
  49.         init(implementationClass, arity, visibility, staticScope, scriptObject, callConfig, position, parameterDesc);
  50.        
  51.         this.method = method;
  52.     }

  53.     @Override
  54.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name,
  55.             IRubyObject[] args, Block block) {
  56.         callConfig.pre(context, self, getImplementationClass(), name, block, staticScope);

  57.         Ruby runtime = context.runtime;
  58.         int callNumber = context.callNumber;
  59.         try {
  60.             boolean isTrace = runtime.hasEventHooks();
  61.             try {
  62.                 if (isTrace) {
  63.                     runtime.callEventHooks(context, RubyEvent.CALL, position.getFile(), position.getLine(), name, getImplementationClass());
  64.                 }
  65.                 return (IRubyObject)method.invoke(null, $scriptObject, context, self, args, block);
  66.             } finally {
  67.                 if (isTrace) {
  68.                     runtime.callEventHooks(context, RubyEvent.RETURN, context.getFile(), context.getLine(), name, getImplementationClass());
  69.                 }
  70.             }
  71.            
  72.         } catch (IllegalArgumentException e) {
  73.             throw RaiseException.createNativeRaiseException(runtime, e, method);
  74.         } catch (IllegalAccessException e) {
  75.             throw RaiseException.createNativeRaiseException(runtime, e, method);
  76.         } catch (InvocationTargetException e) {
  77.             Throwable cause = e.getCause();
  78.             if (cause instanceof JumpException.ReturnJump) {
  79.                 return handleReturn(context, (JumpException.ReturnJump)cause, callNumber);
  80.             } else if (cause instanceof JumpException.RedoJump) {
  81.                 return handleRedo(runtime);
  82.             } else if (cause instanceof RuntimeException) {
  83.                 throw (RuntimeException)cause;
  84.             } else if (cause instanceof Error) {
  85.                 throw (Error)cause;                
  86.             } else {
  87.                 throw RaiseException.createNativeRaiseException(runtime, cause, method);
  88.             }
  89.         } finally {
  90.             callConfig.post(context);
  91.         }
  92.     }
  93. }