1 31 package org.jruby.javasupport; 32 33 import java.lang.reflect.Array ; 34 35 import org.jruby.Ruby; 36 import org.jruby.RubyClass; 37 import org.jruby.RubyFixnum; 38 import org.jruby.RubyInteger; 39 import org.jruby.RubyModule; 40 import org.jruby.runtime.ObjectAllocator; 41 import org.jruby.runtime.builtin.IRubyObject; 42 43 public class JavaArray extends JavaObject { 44 45 public JavaArray(Ruby runtime, Object array) { 46 super(runtime, runtime.getModule("Java").getClass("JavaArray"), array); 47 assert array.getClass().isArray(); 48 } 49 50 public static RubyClass createJavaArrayClass(Ruby runtime, RubyModule javaModule) { 51 return javaModule.defineClassUnder("JavaArray", javaModule.getClass("JavaObject"), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 54 } 55 56 public RubyFixnum length() { 57 return getRuntime().newFixnum(getLength()); 58 } 59 60 private int getLength() { 61 return Array.getLength(getValue()); 62 } 63 64 public IRubyObject aref(IRubyObject index) { 65 if (! (index instanceof RubyInteger)) { 66 throw getRuntime().newTypeError(index, getRuntime().getClass("Integer")); 67 } 68 int intIndex = (int) ((RubyInteger) index).getLongValue(); 69 if (intIndex < 0 || intIndex >= getLength()) { 70 throw getRuntime().newArgumentError( 71 "index out of bounds for java array (" + intIndex + 72 " for length " + getLength() + ")"); 73 } 74 Object result = Array.get(getValue(), intIndex); 75 if (result == null) { 76 return getRuntime().getNil(); 77 } 78 return JavaObject.wrap(getRuntime(), result); 79 } 80 81 public IRubyObject aset(IRubyObject index, IRubyObject value) { 82 if (! (index instanceof RubyInteger)) { 83 throw getRuntime().newTypeError(index, getRuntime().getClass("Integer")); 84 } 85 int intIndex = (int) ((RubyInteger) index).getLongValue(); 86 if (! (value instanceof JavaObject)) { 87 throw getRuntime().newTypeError("not a java object:" + value); 88 } 89 Object javaObject = ((JavaObject) value).getValue(); 90 try { 91 Array.set(getValue(), intIndex, javaObject); 92 } catch (IndexOutOfBoundsException e) { 93 throw getRuntime().newArgumentError( 94 "index out of bounds for java array (" + intIndex + 95 " for length " + getLength() + ")"); 96 } catch (ArrayStoreException e) { 97 throw getRuntime().newArgumentError( 98 "wrong element type " + javaObject.getClass() + "(array is " + 99 getValue().getClass() + ")"); 100 } 101 return value; 102 } 103 104 public IRubyObject afill(IRubyObject beginIndex, IRubyObject endIndex, IRubyObject value) { 105 if (! (beginIndex instanceof RubyInteger)) { 106 throw getRuntime().newTypeError(beginIndex, getRuntime().getClass("Integer")); 107 } 108 int intIndex = (int) ((RubyInteger) beginIndex).getLongValue(); 109 if (! (endIndex instanceof RubyInteger)) { 110 throw getRuntime().newTypeError(endIndex, getRuntime().getClass("Integer")); 111 } 112 int intEndIndex = (int) ((RubyInteger) endIndex).getLongValue(); 113 if (! (value instanceof JavaObject)) { 114 throw getRuntime().newTypeError("not a java object:" + value); 115 } 116 Object javaObject = ((JavaObject) value).getValue(); 117 Object self = getValue(); 118 try { 119 for ( ; intIndex < intEndIndex; intIndex++) { 120 Array.set(self, intIndex, javaObject); 121 } 122 } catch (IndexOutOfBoundsException e) { 123 throw getRuntime().newArgumentError( 124 "index out of bounds for java array (" + intIndex + 125 " for length " + getLength() + ")"); 126 } catch (ArrayStoreException e) { 127 throw getRuntime().newArgumentError( 128 "wrong element type " + javaObject.getClass() + "(array is " + 129 getValue().getClass() + ")"); 130 } 131 return value; 132 } 133 } 134 | Popular Tags |