1 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 toString() { 101 return getClass().getName(); 102 } 103 104 public boolean equals(Object 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 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 [] 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 |