KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jruby.Ruby;
32 import org.jruby.RubyArray;
33 import org.jruby.RubyClass;
34 import org.jruby.RubyFixnum;
35 import org.jruby.RubyObject;
36 import org.jruby.RubyString;
37 import org.jruby.javasupport.JavaClass;
38 import org.jruby.javasupport.JavaObject;
39 import org.jruby.runtime.CallbackFactory;
40 import org.jruby.runtime.builtin.IRubyObject;
41
42 public class JavaProxyReflectionObject extends RubyObject {
43
44     public JavaProxyReflectionObject(Ruby runtime, RubyClass metaClass) {
45         super(runtime, metaClass, false);
46     }
47
48     protected static void registerRubyMethods(Ruby runtime, RubyClass result) {
49         CallbackFactory callbackFactory = runtime
50                 .callbackFactory(JavaProxyReflectionObject.class);
51
52         result.defineFastMethod("to_s", callbackFactory.getFastMethod("to_s"));
53         result.defineFastMethod("==", callbackFactory.getFastMethod("equal",
54                 IRubyObject.class));
55         result.defineFastMethod("eql?", callbackFactory.getFastMethod("equal",
56                 IRubyObject.class));
57         result.defineFastMethod("equal?", callbackFactory.getFastMethod("same",
58                 IRubyObject.class));
59         result.defineFastMethod("hash", callbackFactory.getFastMethod("hash"));
60         result
61                 .defineFastMethod("java_type", callbackFactory
62                         .getFastMethod("java_type"));
63         result.defineFastMethod("java_class", callbackFactory
64                 .getFastMethod("java_class"));
65         result.defineFastMethod("java_proxy?", callbackFactory
66                 .getFastMethod("is_java_proxy"));
67         result.defineFastMethod("length", callbackFactory.getFastMethod("length"));
68         result.defineFastMethod("[]", callbackFactory.getFastMethod("aref",
69                 IRubyObject.class));
70         result.defineFastMethod("[]=", callbackFactory.getFastMethod("aset",
71                 IRubyObject.class, IRubyObject.class));
72
73         result.getMetaClass().defineAlias("__j_allocate","allocate");
74     }
75
76     public RubyFixnum hash() {
77         return getRuntime().newFixnum(hashCode());
78     }
79
80     public IRubyObject to_s() {
81         return getRuntime().newString(toString());
82     }
83
84     public IRubyObject equal(IRubyObject other) {
85         if (!(other instanceof JavaProxyReflectionObject)) {
86             other = other.getInstanceVariable("@java_object");
87             if (!(other instanceof JavaObject)) {
88                 return getRuntime().getFalse();
89             }
90         }
91
92         boolean isEqual = equals(other);
93         return isEqual ? getRuntime().getTrue() : getRuntime().getFalse();
94     }
95     
96     public int hashCode() {
97         return getClass().hashCode();
98     }
99
100     public String JavaDoc toString() {
101         return getClass().getName();
102     }
103
104     public boolean equals(Object JavaDoc other) {
105         return this == other;
106     }
107     
108     public IRubyObject same(IRubyObject other) {
109         if (!(other instanceof JavaObject)) {
110             other = other.getInstanceVariable("@java_object");
111             if (!(other instanceof JavaObject)) {
112                 return getRuntime().getFalse();
113             }
114         }
115
116         boolean isSame = this == other;
117         return isSame ? getRuntime().getTrue() : getRuntime().getFalse();
118     }
119
120     public RubyString java_type() {
121         return getRuntime().newString(getJavaClass().getName());
122     }
123
124     public IRubyObject java_class() {
125         return JavaClass.get(getRuntime(), getJavaClass());
126     }
127
128     public RubyFixnum length() {
129         throw getRuntime().newTypeError("not a java array");
130     }
131
132     public IRubyObject aref(IRubyObject index) {
133         throw getRuntime().newTypeError("not a java array");
134     }
135
136     public IRubyObject aset(IRubyObject index, IRubyObject someValue) {
137         throw getRuntime().newTypeError("not a java array");
138     }
139
140     public IRubyObject is_java_proxy() {
141         return getRuntime().getFalse();
142     }
143
144     //
145
// utility methods
146
//
147

148     protected RubyArray buildRubyArray(IRubyObject[] constructors) {
149         RubyArray result = getRuntime().newArray(constructors.length);
150         for (int i = 0; i < constructors.length; i++) {
151             result.append(constructors[i]);
152         }
153         return result;
154     }
155
156     protected RubyArray buildRubyArray(Class JavaDoc[] classes) {
157         RubyArray result = getRuntime().newArray(classes.length);
158         for (int i = 0; i < classes.length; i++) {
159             result.append(JavaClass.get(getRuntime(), classes[i]));
160         }
161         return result;
162     }
163
164 }
165
Popular Tags