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) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
015: * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
016: * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
017: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
018: *
019: * Alternatively, the contents of this file may be used under the terms of
020: * either of the GNU General Public License Version 2 or later (the "GPL"),
021: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022: * in which case the provisions of the GPL or the LGPL are applicable instead
023: * of those above. If you wish to allow use of your version of this file only
024: * under the terms of either the GPL or the LGPL, and not to allow others to
025: * use your version of this file under the terms of the CPL, indicate your
026: * decision by deleting the provisions above and replace them with the notice
027: * and other provisions required by the GPL or the LGPL. If you do not delete
028: * the provisions above, a recipient may use your version of this file under
029: * the terms of any one of the CPL, the GPL or the LGPL.
030: ***** END LICENSE BLOCK *****/package org.jruby.javasupport;
031:
032: import java.lang.reflect.Array;
033:
034: import org.jruby.Ruby;
035: import org.jruby.RubyClass;
036: import org.jruby.RubyFixnum;
037: import org.jruby.RubyInteger;
038: import org.jruby.RubyModule;
039: import org.jruby.runtime.ObjectAllocator;
040: import org.jruby.runtime.builtin.IRubyObject;
041:
042: public class JavaArray extends JavaObject {
043:
044: public JavaArray(Ruby runtime, Object array) {
045: super (runtime, runtime.getJavaSupport().getJavaArrayClass(),
046: array);
047: assert array.getClass().isArray();
048: }
049:
050: public static RubyClass createJavaArrayClass(Ruby runtime,
051: RubyModule javaModule) {
052: // FIXME: NOT_ALLOCATABLE_ALLOCATOR is probably not right here, since we might
053: // eventually want JavaArray to be marshallable. JRUBY-414
054: return javaModule.defineClassUnder("JavaArray", javaModule
055: .getClass("JavaObject"),
056: ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
057: }
058:
059: public RubyFixnum length() {
060: return getRuntime().newFixnum(getLength());
061: }
062:
063: private int getLength() {
064: return Array.getLength(getValue());
065: }
066:
067: public IRubyObject aref(IRubyObject index) {
068: if (!(index instanceof RubyInteger)) {
069: throw getRuntime().newTypeError(index,
070: getRuntime().getClass("Integer"));
071: }
072: int intIndex = (int) ((RubyInteger) index).getLongValue();
073: if (intIndex < 0 || intIndex >= getLength()) {
074: throw getRuntime().newArgumentError(
075: "index out of bounds for java array (" + intIndex
076: + " for length " + getLength() + ")");
077: }
078: Object result = Array.get(getValue(), intIndex);
079: if (result == null) {
080: return getRuntime().getNil();
081: }
082: return JavaObject.wrap(getRuntime(), result);
083: }
084:
085: public IRubyObject aset(IRubyObject index, IRubyObject value) {
086: if (!(index instanceof RubyInteger)) {
087: throw getRuntime().newTypeError(index,
088: getRuntime().getClass("Integer"));
089: }
090: int intIndex = (int) ((RubyInteger) index).getLongValue();
091: if (!(value instanceof JavaObject)) {
092: throw getRuntime().newTypeError(
093: "not a java object:" + value);
094: }
095: Object javaObject = ((JavaObject) value).getValue();
096: try {
097: Array.set(getValue(), intIndex, javaObject);
098: } catch (IndexOutOfBoundsException e) {
099: throw getRuntime().newArgumentError(
100: "index out of bounds for java array (" + intIndex
101: + " for length " + getLength() + ")");
102: } catch (ArrayStoreException e) {
103: throw getRuntime().newArgumentError(
104: "wrong element type " + javaObject.getClass()
105: + "(array is " + getValue().getClass()
106: + ")");
107: }
108: return value;
109: }
110:
111: public IRubyObject afill(IRubyObject beginIndex,
112: IRubyObject endIndex, IRubyObject value) {
113: if (!(beginIndex instanceof RubyInteger)) {
114: throw getRuntime().newTypeError(beginIndex,
115: getRuntime().getClass("Integer"));
116: }
117: int intIndex = (int) ((RubyInteger) beginIndex).getLongValue();
118: if (!(endIndex instanceof RubyInteger)) {
119: throw getRuntime().newTypeError(endIndex,
120: getRuntime().getClass("Integer"));
121: }
122: int intEndIndex = (int) ((RubyInteger) endIndex).getLongValue();
123: if (!(value instanceof JavaObject)) {
124: throw getRuntime().newTypeError(
125: "not a java object:" + value);
126: }
127: Object javaObject = ((JavaObject) value).getValue();
128: Object self = getValue();
129: try {
130: for (; intIndex < intEndIndex; intIndex++) {
131: Array.set(self, intIndex, javaObject);
132: }
133: } catch (IndexOutOfBoundsException e) {
134: throw getRuntime().newArgumentError(
135: "index out of bounds for java array (" + intIndex
136: + " for length " + getLength() + ")");
137: } catch (ArrayStoreException e) {
138: throw getRuntime().newArgumentError(
139: "wrong element type " + javaObject.getClass()
140: + "(array is " + getValue().getClass()
141: + ")");
142: }
143: return value;
144: }
145: }
|