KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > proxy > JavaProxyConstructor


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) 2006 Kresten Krab Thorup <krab@gnu.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
29 package org.jruby.javasupport.proxy;
30
31 import java.lang.reflect.Constructor JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33
34 import org.jruby.Ruby;
35 import org.jruby.RubyArray;
36 import org.jruby.RubyClass;
37 import org.jruby.RubyFixnum;
38 import org.jruby.RubyModule;
39 import org.jruby.RubyObject;
40 import org.jruby.RubyProc;
41 import org.jruby.exceptions.RaiseException;
42 import org.jruby.javasupport.Java;
43 import org.jruby.javasupport.JavaObject;
44 import org.jruby.javasupport.JavaUtil;
45 import org.jruby.runtime.Block;
46 import org.jruby.runtime.CallbackFactory;
47 import org.jruby.runtime.ObjectAllocator;
48 import org.jruby.runtime.builtin.IRubyObject;
49
50 public class JavaProxyConstructor extends JavaProxyReflectionObject {
51
52     private final Constructor JavaDoc proxyConstructor;
53
54     private final JavaProxyClass declaringProxyClass;
55
56     JavaProxyConstructor(Ruby runtime, JavaProxyClass pClass,
57             Constructor JavaDoc constructor) {
58         super(runtime, runtime.getModule("Java").getClass(
59                 "JavaProxyConstructor"));
60         this.declaringProxyClass = pClass;
61         this.proxyConstructor = constructor;
62     }
63
64     public Class JavaDoc[] getParameterTypes() {
65         Class JavaDoc[] decl = proxyConstructor.getParameterTypes();
66         Class JavaDoc[] result = new Class JavaDoc[decl.length - 1];
67         System.arraycopy(decl, 0, result, 0, result.length);
68         return result;
69     }
70
71     public JavaProxyClass getDeclaringClass() {
72         return declaringProxyClass;
73     }
74
75     public Object JavaDoc newInstance(Object JavaDoc[] args, JavaProxyInvocationHandler handler)
76             throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc,
77             IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
78         if (args.length + 1 != proxyConstructor.getParameterTypes().length) {
79             throw new IllegalArgumentException JavaDoc("wrong number of parameters");
80         }
81
82         Object JavaDoc[] realArgs = new Object JavaDoc[args.length + 1];
83         System.arraycopy(args, 0, realArgs, 0, args.length);
84         realArgs[args.length] = handler;
85
86         return proxyConstructor.newInstance(realArgs);
87     }
88
89     public static RubyClass createJavaProxyConstructorClass(Ruby runtime,
90             RubyModule javaProxyModule) {
91         RubyClass result = javaProxyModule.defineClassUnder(
92                                                             "JavaProxyConstructor", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
93
94         CallbackFactory callbackFactory = runtime
95                 .callbackFactory(JavaProxyConstructor.class);
96
97         JavaProxyReflectionObject.registerRubyMethods(runtime, result);
98
99         result.defineFastMethod("argument_types", callbackFactory
100                 .getFastMethod("argument_types"));
101
102         result.defineFastMethod("declaring_class", callbackFactory
103                 .getFastMethod("getDeclaringClass"));
104
105         result.defineMethod("new_instance", callbackFactory
106                 .getOptMethod("new_instance"));
107
108         result.defineFastMethod("arity", callbackFactory.getFastMethod("arity"));
109
110         return result;
111
112     }
113
114     public RubyFixnum arity() {
115         return getRuntime().newFixnum(getParameterTypes().length);
116     }
117
118     protected String JavaDoc nameOnInspection() {
119         return getDeclaringClass().nameOnInspection();
120     }
121
122     public IRubyObject inspect() {
123         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
124         result.append(nameOnInspection());
125         Class JavaDoc[] parameterTypes = getParameterTypes();
126         for (int i = 0; i < parameterTypes.length; i++) {
127             result.append(parameterTypes[i].getName());
128             if (i < parameterTypes.length - 1) {
129                 result.append(',');
130             }
131         }
132         result.append(")>");
133         return getRuntime().newString(result.toString());
134     }
135
136     public RubyArray argument_types() {
137         return buildRubyArray(getParameterTypes());
138     }
139
140     public RubyObject new_instance(IRubyObject[] args, Block block) {
141         int size = this.checkArgumentCount(args, 1, 2) - 1;
142         final RubyProc proc;
143
144         // Is there a supplied proc argument or do we assume a block was
145
// supplied
146
if (args[size] instanceof RubyProc) {
147             proc = (RubyProc) args[size];
148         } else {
149             proc = getRuntime().newProc(false,block);
150             size++;
151         }
152
153         RubyArray constructor_args = (RubyArray) args[0];
154         Class JavaDoc[] parameterTypes = getParameterTypes();
155
156         int count = (int) constructor_args.length().getLongValue();
157         Object JavaDoc[] converted = new Object JavaDoc[count];
158         for (int i = 0; i < count; i++) {
159             // TODO: call ruby method
160
IRubyObject ith = constructor_args.aref(new IRubyObject[] { getRuntime().newFixnum(i) });
161             converted[i] = JavaUtil.convertArgument(Java.ruby_to_java(this, ith, Block.NULL_BLOCK), parameterTypes[i]);
162         }
163
164         final IRubyObject recv = this;
165
166         JavaProxyInvocationHandler handler = new JavaProxyInvocationHandler() {
167
168             public Object JavaDoc invoke(Object JavaDoc proxy, JavaProxyMethod method,
169                     Object JavaDoc[] nargs) throws Throwable JavaDoc {
170                 int length = nargs == null ? 0 : nargs.length;
171                 IRubyObject[] rubyArgs = new IRubyObject[length + 2];
172                 rubyArgs[0] = JavaObject.wrap(recv.getRuntime(), proxy);
173                 rubyArgs[1] = method;
174                 for (int i = 0; i < length; i++) {
175                     rubyArgs[i + 2] = JavaUtil.convertJavaToRuby(getRuntime(),
176                             nargs[i]);
177                 }
178                 IRubyObject call_result = proc.call(rubyArgs);
179                 Object JavaDoc converted_result = JavaUtil.convertRubyToJava(
180                         call_result, method.getReturnType());
181                 return converted_result;
182             }
183
184         };
185
186         Object JavaDoc result;
187         try {
188             result = newInstance(converted, handler);
189         } catch (Exception JavaDoc e) {
190             RaiseException ex = getRuntime().newArgumentError(
191                     "Constructor invocation failed: " + e.getMessage());
192             ex.initCause(e);
193             throw ex;
194         }
195
196         return JavaObject.wrap(getRuntime(), result);
197
198     }
199
200 }
201
Popular Tags