KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > JavaConstructor


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) 2001 Chad Fowler <chadfowler@chadfowler.com>
15  * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
16  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
18  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
19  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
20  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
21  * Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either of the GNU General Public License Version 2 or later (the "GPL"),
25  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the CPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the CPL, the GPL or the LGPL.
34  ***** END LICENSE BLOCK *****/

35 package org.jruby.javasupport;
36
37 import java.lang.reflect.AccessibleObject JavaDoc;
38 import java.lang.reflect.Constructor JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40
41 import org.jruby.Ruby;
42 import org.jruby.RubyClass;
43 import org.jruby.RubyModule;
44 import org.jruby.runtime.CallbackFactory;
45 import org.jruby.runtime.ObjectAllocator;
46 import org.jruby.runtime.builtin.IRubyObject;
47
48 public class JavaConstructor extends JavaCallable {
49     private Constructor JavaDoc constructor;
50
51     public static RubyClass createJavaConstructorClass(Ruby runtime, RubyModule javaModule) {
52         // TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here, since we don't intend for people to monkey with
53
// this type and it can't be marshalled. Confirm. JRUBY-415
54
RubyClass result =
55                 javaModule.defineClassUnder("JavaConstructor", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
56         CallbackFactory callbackFactory = runtime.callbackFactory(JavaConstructor.class);
57
58         JavaCallable.registerRubyMethods(runtime, result, JavaConstructor.class);
59         result.defineFastMethod("arity", callbackFactory.getFastMethod("arity"));
60         result.defineFastMethod("inspect", callbackFactory.getFastMethod("inspect"));
61         result.defineFastMethod("argument_types", callbackFactory.getFastMethod("argument_types"));
62         result.defineFastMethod("new_instance", callbackFactory.getFastOptMethod("new_instance"));
63         
64         return result;
65     }
66
67     public JavaConstructor(Ruby runtime, Constructor JavaDoc constructor) {
68         super(runtime, runtime.getModule("Java").getClass("JavaConstructor"));
69         this.constructor = constructor;
70     }
71
72     public int getArity() {
73         return constructor.getParameterTypes().length;
74     }
75
76     public IRubyObject new_instance(IRubyObject[] args) {
77         if (args.length != getArity()) {
78             throw getRuntime().newArgumentError(args.length, getArity());
79         }
80         Object JavaDoc[] constructorArguments = new Object JavaDoc[args.length];
81         Class JavaDoc[] types = constructor.getParameterTypes();
82         for (int i = 0; i < args.length; i++) {
83             constructorArguments[i] = JavaUtil.convertArgument(args[i], types[i]);
84         }
85         try {
86             Object JavaDoc result = constructor.newInstance(constructorArguments);
87             return JavaObject.wrap(getRuntime(), result);
88
89         } catch (IllegalArgumentException JavaDoc iae) {
90             throw getRuntime().newTypeError("expected " + argument_types().inspect() +
91                                               ", got [" + constructorArguments[0].getClass().getName() + ", ...]");
92         } catch (IllegalAccessException JavaDoc iae) {
93             throw getRuntime().newTypeError("illegal access");
94         } catch (InvocationTargetException JavaDoc ite) {
95             getRuntime().getJavaSupport().handleNativeException(ite.getTargetException());
96             // not reached
97
assert false;
98             return null;
99         } catch (InstantiationException JavaDoc ie) {
100             throw getRuntime().newTypeError("can't make instance of " + constructor.getDeclaringClass().getName());
101         }
102     }
103
104
105     protected String JavaDoc nameOnInspection() {
106         return getType().toString();
107     }
108
109     protected Class JavaDoc[] parameterTypes() {
110         return constructor.getParameterTypes();
111     }
112
113     protected int getModifiers() {
114         return constructor.getModifiers();
115     }
116
117     protected AccessibleObject JavaDoc accesibleObject() {
118         return constructor;
119     }
120 }
121
Popular Tags