001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2006 Kresten Krab Thorup <krab@gnu.org>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby.javasupport.proxy;
028:
029: import org.jruby.Ruby;
030: import org.jruby.RubyArray;
031: import org.jruby.RubyClass;
032: import org.jruby.RubyFixnum;
033: import org.jruby.RubyObject;
034: import org.jruby.RubyString;
035: import org.jruby.javasupport.JavaClass;
036: import org.jruby.javasupport.JavaObject;
037: import org.jruby.runtime.CallbackFactory;
038: import org.jruby.runtime.builtin.IRubyObject;
039:
040: public class JavaProxyReflectionObject extends RubyObject {
041:
042: public JavaProxyReflectionObject(Ruby runtime, RubyClass metaClass) {
043: super (runtime, metaClass, false);
044: }
045:
046: protected static void registerRubyMethods(Ruby runtime,
047: RubyClass result) {
048: CallbackFactory callbackFactory = runtime
049: .callbackFactory(JavaProxyReflectionObject.class);
050:
051: result.defineFastMethod("to_s", callbackFactory
052: .getFastMethod("to_s"));
053: result.defineFastMethod("==", callbackFactory.getFastMethod(
054: "equal", IRubyObject.class));
055: result.defineFastMethod("eql?", callbackFactory.getFastMethod(
056: "equal", IRubyObject.class));
057: result.defineFastMethod("equal?", callbackFactory
058: .getFastMethod("same", IRubyObject.class));
059: result.defineFastMethod("hash", callbackFactory
060: .getFastMethod("hash"));
061: result.defineFastMethod("java_type", callbackFactory
062: .getFastMethod("java_type"));
063: result.defineFastMethod("java_class", callbackFactory
064: .getFastMethod("java_class"));
065: result.defineFastMethod("java_proxy?", callbackFactory
066: .getFastMethod("is_java_proxy"));
067: result.defineFastMethod("length", callbackFactory
068: .getFastMethod("length"));
069: result.defineFastMethod("[]", callbackFactory.getFastMethod(
070: "aref", IRubyObject.class));
071: result.defineFastMethod("[]=", callbackFactory.getFastMethod(
072: "aset", IRubyObject.class, IRubyObject.class));
073:
074: result.getMetaClass().defineAlias("__j_allocate", "allocate");
075: }
076:
077: public RubyFixnum hash() {
078: return getRuntime().newFixnum(hashCode());
079: }
080:
081: public IRubyObject to_s() {
082: return getRuntime().newString(toString());
083: }
084:
085: public IRubyObject equal(IRubyObject other) {
086: if (!(other instanceof JavaProxyReflectionObject)) {
087: other = other.getInstanceVariable("@java_object");
088: if (!(other instanceof JavaObject)) {
089: return getRuntime().getFalse();
090: }
091: }
092:
093: boolean isEqual = equals(other);
094: return isEqual ? getRuntime().getTrue() : getRuntime()
095: .getFalse();
096: }
097:
098: public int hashCode() {
099: return getClass().hashCode();
100: }
101:
102: public String toString() {
103: return getClass().getName();
104: }
105:
106: public boolean equals(Object other) {
107: return this == other;
108: }
109:
110: public IRubyObject same(IRubyObject other) {
111: if (!(other instanceof JavaObject)) {
112: other = other.getInstanceVariable("@java_object");
113: if (!(other instanceof JavaObject)) {
114: return getRuntime().getFalse();
115: }
116: }
117:
118: boolean isSame = this == other;
119: return isSame ? getRuntime().getTrue() : getRuntime()
120: .getFalse();
121: }
122:
123: public RubyString java_type() {
124: return getRuntime().newString(getJavaClass().getName());
125: }
126:
127: public IRubyObject java_class() {
128: return JavaClass.get(getRuntime(), getJavaClass());
129: }
130:
131: public RubyFixnum length() {
132: throw getRuntime().newTypeError("not a java array");
133: }
134:
135: public IRubyObject aref(IRubyObject index) {
136: throw getRuntime().newTypeError("not a java array");
137: }
138:
139: public IRubyObject aset(IRubyObject index, IRubyObject someValue) {
140: throw getRuntime().newTypeError("not a java array");
141: }
142:
143: public IRubyObject is_java_proxy() {
144: return getRuntime().getFalse();
145: }
146:
147: //
148: // utility methods
149: //
150:
151: protected RubyArray buildRubyArray(IRubyObject[] constructors) {
152: RubyArray result = getRuntime().newArray(constructors.length);
153: for (int i = 0; i < constructors.length; i++) {
154: result.append(constructors[i]);
155: }
156: return result;
157: }
158:
159: protected RubyArray buildRubyArray(Class[] classes) {
160: RubyArray result = getRuntime().newArray(classes.length);
161: for (int i = 0; i < classes.length; i++) {
162: result.append(JavaClass.get(getRuntime(), classes[i]));
163: }
164: return result;
165: }
166:
167: }
|