KickJava   Java API By Example, From Geeks To Geeks.

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


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 SimpleReflectedMethod extends DynamicMethod {
47     private Method JavaDoc method;
48     private Class JavaDoc type;
49     private String JavaDoc methodName;
50     private Arity arity;
51     
52     public SimpleReflectedMethod(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()];
66             Arrays.fill(parameterTypes, IRubyObject.class);
67         } else {
68             parameterTypes = new Class JavaDoc[1];
69             parameterTypes[0] = IRubyObject[].class;
70         }
71         try {
72             method = type.getMethod(methodName, parameterTypes);
73         } catch (NoSuchMethodException JavaDoc e) {
74             assert false : e;
75         } catch (SecurityException JavaDoc e) {
76             assert false : e;
77         }
78         
79         assert method != null;
80     }
81
82     public void preMethod(ThreadContext context, RubyModule klazz, IRubyObject self, String JavaDoc name, IRubyObject[] args, boolean noSuper, Block block) {
83     }
84     
85     public void postMethod(ThreadContext context) {
86     }
87     
88     public IRubyObject internalCall(ThreadContext context, RubyModule klazz, IRubyObject self, String JavaDoc name, IRubyObject[] args, boolean noSuper, Block block) {
89         assert false;
90         return null;
91     }
92     
93     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String JavaDoc name, IRubyObject[] args, boolean noSuper, Block block) {
94         Ruby runtime = context.getRuntime();
95         arity.checkArity(runtime, args);
96         
97         assert self != null;
98         assert args != null;
99         assert method != null;
100         
101         Object JavaDoc[] methodArgs = !arity.isFixed() ? new Object JavaDoc[]{args} : args;
102
103         try {
104             return (IRubyObject) method.invoke(self, methodArgs);
105         } catch (InvocationTargetException JavaDoc e) {
106             if (e.getTargetException() instanceof RaiseException) {
107                 throw (RaiseException) e.getTargetException();
108             } else if (e.getTargetException() instanceof JumpException) {
109                 throw (JumpException) e.getTargetException();
110             } else if (e.getTargetException() instanceof ThreadKill) {
111                 // allow it to bubble up
112
throw (ThreadKill) e.getTargetException();
113             } else if (e.getTargetException() instanceof Exception JavaDoc) {
114                 if(e.getTargetException() instanceof MainExitException) {
115                     throw (RuntimeException JavaDoc)e.getTargetException();
116                 }
117                 runtime.getJavaSupport().handleNativeException(e.getTargetException());
118                 return runtime.getNil();
119             } else {
120                 throw (Error JavaDoc) e.getTargetException();
121             }
122         } catch (IllegalAccessException JavaDoc e) {
123             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
124             message.append(e.getMessage());
125             message.append(':');
126             message.append(" methodName=").append(methodName);
127             message.append(" recv=").append(self.toString());
128             message.append(" type=").append(type.getName());
129             message.append(" methodArgs=[");
130             for (int i = 0; i < methodArgs.length; i++) {
131                 message.append(methodArgs[i]);
132                 message.append(' ');
133             }
134             message.append(']');
135             assert false : message.toString();
136             return null;
137         } catch (final IllegalArgumentException JavaDoc e) {
138 /* StringBuffer message = new StringBuffer();
139             message.append(e.getMessage());
140             message.append(':');
141             message.append(" methodName=").append(methodName);
142             message.append(" recv=").append(recv.toString());
143             message.append(" type=").append(type.getName());
144             message.append(" methodArgs=[");
145             for (int i = 0; i < methodArgs.length; i++) {
146                 message.append(methodArgs[i]);
147                 message.append(' ');
148             }
149             message.append(']');*/

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