KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > internal > runtime > methods > FullFunctionReflectedMethod


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common 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/cpl-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) 2005 Thomas E Enebo <enebo@acm.org>
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 CPL, 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 CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.internal.runtime.methods;
29
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 import org.jruby.Ruby;
35 import org.jruby.RubyModule;
36 import org.jruby.exceptions.JumpException;
37 import org.jruby.exceptions.MainExitException;
38 import org.jruby.exceptions.RaiseException;
39 import org.jruby.exceptions.ThreadKill;
40 import org.jruby.runtime.Arity;
41 import org.jruby.runtime.Block;
42 import org.jruby.runtime.ThreadContext;
43 import org.jruby.runtime.Visibility;
44 import org.jruby.runtime.builtin.IRubyObject;
45
46 public class FullFunctionReflectedMethod extends DynamicMethod {
47     private Method JavaDoc method;
48     private Class JavaDoc type;
49     private String JavaDoc methodName;
50     private Arity arity;
51     
52     public FullFunctionReflectedMethod(RubyModule implementationClass, Class JavaDoc type, String JavaDoc methodName,
53         Arity arity, Visibility visibility) {
54         super(implementationClass, visibility);
55         this.type = type;
56         this.methodName = methodName;
57         this.arity = arity;
58         
59         assert type != null;
60         assert methodName != null;
61         assert arity != null;
62
63         Class JavaDoc[] parameterTypes;
64         if (arity.isFixed()) {
65             parameterTypes = new Class JavaDoc[arity.getValue()+1];
66             Arrays.fill(parameterTypes, IRubyObject.class);
67             parameterTypes[arity.getValue()] = Block.class;
68         } else {
69             parameterTypes = new Class JavaDoc[2];
70             parameterTypes[0] = IRubyObject[].class;
71             parameterTypes[1] = Block.class;
72         }
73         try {
74             method = type.getMethod(methodName, parameterTypes);
75         } catch (NoSuchMethodException JavaDoc e) {
76             assert false : e;
77         } catch (SecurityException JavaDoc e) {
78             assert false : e;
79         }
80         
81         assert method != null;
82     }
83
84     public void preMethod(ThreadContext context, RubyModule klazz, IRubyObject self, String JavaDoc name, IRubyObject[] args, boolean noSuper, Block block) {
85         context.preReflectedMethodInternalCall(implementationClass, klazz, self, name, args, noSuper, block);
86     }
87     
88     public void postMethod(ThreadContext context) {
89         context.postReflectedMethodInternalCall();
90     }
91     
92     public IRubyObject internalCall(ThreadContext context, RubyModule klazz, IRubyObject self, String JavaDoc name, IRubyObject[] args, boolean noSuper, Block block) {
93         Ruby runtime = context.getRuntime();
94         arity.checkArity(runtime, args);
95         
96         assert self != null;
97         assert args != null;
98         assert method != null;
99         
100         Object JavaDoc[] methodArgs;
101         if (!arity.isFixed()) {
102             methodArgs = new Object JavaDoc[]{args, block};
103         } else {
104             methodArgs = new Object JavaDoc[args.length + 1];
105             System.arraycopy(args, 0, methodArgs, 0, args.length);
106             methodArgs[args.length] = block;
107         }
108
109         try {
110             return (IRubyObject) method.invoke(self, methodArgs);
111         } catch (InvocationTargetException JavaDoc e) {
112             if (e.getTargetException() instanceof RaiseException) {
113                 throw (RaiseException) e.getTargetException();
114             } else if (e.getTargetException() instanceof JumpException) {
115                 throw (JumpException) e.getTargetException();
116             } else if (e.getTargetException() instanceof ThreadKill) {
117                 // allow it to bubble up
118
throw (ThreadKill) e.getTargetException();
119             } else if (e.getTargetException() instanceof Exception JavaDoc) {
120                 if(e.getTargetException() instanceof MainExitException) {
121                     throw (RuntimeException JavaDoc)e.getTargetException();
122                 }
123                 runtime.getJavaSupport().handleNativeException(e.getTargetException());
124                 return runtime.getNil();
125             } else {
126                 throw (Error JavaDoc) e.getTargetException();
127             }
128         } catch (IllegalAccessException JavaDoc e) {
129             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
130             message.append(e.getMessage());
131             message.append(':');
132             message.append(" methodName=").append(methodName);
133             message.append(" recv=").append(self.toString());
134             message.append(" type=").append(type.getName());
135             message.append(" methodArgs=[");
136             for (int i = 0; i < methodArgs.length; i++) {
137                 message.append(methodArgs[i]);
138                 message.append(' ');
139             }
140             message.append(']');
141             assert false : message.toString();
142             return null;
143         } catch (final IllegalArgumentException JavaDoc e) {
144 /* StringBuffer message = new StringBuffer();
145             message.append(e.getMessage());
146             message.append(':');
147             message.append(" methodName=").append(methodName);
148             message.append(" recv=").append(recv.toString());
149             message.append(" type=").append(type.getName());
150             message.append(" methodArgs=[");
151             for (int i = 0; i < methodArgs.length; i++) {
152                 message.append(methodArgs[i]);
153                 message.append(' ');
154             }
155             message.append(']');*/

156             assert false : e;
157             return null;
158         }
159     }
160
161     public DynamicMethod dup() {
162         FullFunctionReflectedMethod newMethod =
163             new FullFunctionReflectedMethod(getImplementationClass(), type, methodName, arity, getVisibility());
164         
165         newMethod.method = method;
166         
167         return newMethod;
168     }
169
170     // TODO: Perhaps abstract method should contain this and all other Methods should pass in decent value
171
public Arity getArity() {
172         return arity;
173     }
174 }
175
Popular Tags