001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgueni Brevnov
019: * @version $Revision: 1.1.2.1.4.4 $
020: */package java.lang.reflect;
021:
022: /**
023: * @com.intel.drl.spec_ref
024: */
025: public final class Array {
026:
027: private Array() {
028: }
029:
030: /**
031: * @com.intel.drl.spec_ref
032: */
033: public static Object get(Object array, int index)
034: throws IllegalArgumentException,
035: ArrayIndexOutOfBoundsException {
036: try {
037: return ((Object[]) array)[index];
038: } catch (ClassCastException e) {
039: if (array instanceof int[]) {
040: return new Integer(((int[]) array)[index]);
041: }
042: if (array instanceof boolean[]) {
043: return ((boolean[]) array)[index] ? Boolean.TRUE
044: : Boolean.FALSE;
045: }
046: if (array instanceof float[]) {
047: return new Float(((float[]) array)[index]);
048: }
049: if (array instanceof char[]) {
050: return new Character(((char[]) array)[index]);
051: }
052: if (array instanceof double[]) {
053: return new Double(((double[]) array)[index]);
054: }
055: if (array instanceof long[]) {
056: return new Long(((long[]) array)[index]);
057: }
058: if (array instanceof short[]) {
059: return new Short(((short[]) array)[index]);
060: }
061: if (array instanceof byte[]) {
062: return new Byte(((byte[]) array)[index]);
063: }
064: }
065: throw new IllegalArgumentException(
066: "Specified argument is not an array");
067: }
068:
069: /**
070: * @com.intel.drl.spec_ref
071: */
072: public static boolean getBoolean(Object array, int index)
073: throws IllegalArgumentException,
074: ArrayIndexOutOfBoundsException {
075: try {
076: return ((boolean[]) array)[index];
077: } catch (ClassCastException e) {
078: throw new IllegalArgumentException(e.getMessage());
079: }
080: }
081:
082: /**
083: * @com.intel.drl.spec_ref
084: */
085: public static byte getByte(Object array, int index)
086: throws IllegalArgumentException,
087: ArrayIndexOutOfBoundsException {
088: try {
089: return ((byte[]) array)[index];
090: } catch (ClassCastException e) {
091: throw new IllegalArgumentException(e.getMessage());
092: }
093: }
094:
095: /**
096: * @com.intel.drl.spec_ref
097: */
098: public static char getChar(Object array, int index)
099: throws IllegalArgumentException,
100: ArrayIndexOutOfBoundsException {
101: try {
102: return ((char[]) array)[index];
103: } catch (ClassCastException e) {
104: throw new IllegalArgumentException(e.getMessage());
105: }
106: }
107:
108: /**
109: * @com.intel.drl.spec_ref
110: */
111: public static double getDouble(Object array, int index)
112: throws IllegalArgumentException,
113: ArrayIndexOutOfBoundsException {
114: if (array instanceof double[]) {
115: return ((double[]) array)[index];
116: }
117: return getFloat(array, index);
118: }
119:
120: /**
121: * @com.intel.drl.spec_ref
122: */
123: public static float getFloat(Object array, int index)
124: throws IllegalArgumentException,
125: ArrayIndexOutOfBoundsException {
126: if (array instanceof float[]) {
127: return ((float[]) array)[index];
128: }
129: return getLong(array, index);
130: }
131:
132: /**
133: * @com.intel.drl.spec_ref
134: */
135: public static int getInt(Object array, int index)
136: throws IllegalArgumentException,
137: ArrayIndexOutOfBoundsException {
138: if (array instanceof int[]) {
139: return ((int[]) array)[index];
140: }
141: if (array instanceof char[]) {
142: return ((char[]) array)[index];
143: }
144: return getShort(array, index);
145: }
146:
147: /**
148: * @com.intel.drl.spec_ref
149: */
150: public static int getLength(Object array)
151: throws IllegalArgumentException {
152: try {
153: return ((Object[]) array).length;
154: } catch (ClassCastException e) {
155: if (array instanceof int[]) {
156: return ((int[]) array).length;
157: }
158: if (array instanceof boolean[]) {
159: return ((boolean[]) array).length;
160: }
161: if (array instanceof float[]) {
162: return ((float[]) array).length;
163: }
164: if (array instanceof char[]) {
165: return ((char[]) array).length;
166: }
167: if (array instanceof double[]) {
168: return ((double[]) array).length;
169: }
170: if (array instanceof long[]) {
171: return ((long[]) array).length;
172: }
173: if (array instanceof short[]) {
174: return ((short[]) array).length;
175: }
176: if (array instanceof byte[]) {
177: return ((byte[]) array).length;
178: }
179: }
180: throw new IllegalArgumentException(
181: "Specified argument is not an array");
182: }
183:
184: /**
185: * @com.intel.drl.spec_ref
186: */
187: public static long getLong(Object array, int index)
188: throws IllegalArgumentException,
189: ArrayIndexOutOfBoundsException {
190: if (array instanceof long[]) {
191: return ((long[]) array)[index];
192: }
193: return getInt(array, index);
194: }
195:
196: /**
197: * @com.intel.drl.spec_ref
198: */
199: public static short getShort(Object array, int index)
200: throws IllegalArgumentException,
201: ArrayIndexOutOfBoundsException {
202: if (array instanceof short[]) {
203: return ((short[]) array)[index];
204: }
205: return getByte(array, index);
206: }
207:
208: /**
209: * @com.intel.drl.spec_ref
210: */
211: public static Object newInstance(Class<?> componentType, int length)
212: throws NegativeArraySizeException {
213: return newInstance(componentType, new int[] { length });
214: }
215:
216: /**
217: * @com.intel.drl.spec_ref
218: */
219: public static Object newInstance(Class<?> componentType,
220: int[] dimensions) throws IllegalArgumentException,
221: NegativeArraySizeException {
222: if (componentType == null) {
223: throw new NullPointerException();
224: }
225: if (componentType == Void.TYPE || dimensions.length == 0) {
226: throw new IllegalArgumentException(
227: "Can not create new array instance for the specified arguments");
228: }
229: return VMReflection.newArrayInstance(componentType, dimensions);
230: }
231:
232: /**
233: * @com.intel.drl.spec_ref
234: */
235: public static void set(Object array, int index, Object value)
236: throws IllegalArgumentException,
237: ArrayIndexOutOfBoundsException {
238:
239: if (array == null) {
240: throw new NullPointerException();
241: }
242:
243: try {
244: ((Object[]) array)[index] = value;
245: return;
246: } catch (ClassCastException e) {
247: if (value instanceof Number) {
248: if (value instanceof Integer) {
249: setInt(array, index, ((Integer) value).intValue());
250: return;
251: } else if (value instanceof Float) {
252: setFloat(array, index, ((Float) value).floatValue());
253: return;
254: } else if (value instanceof Double) {
255: setDouble(array, index, ((Double) value)
256: .doubleValue());
257: return;
258: } else if (value instanceof Long) {
259: setLong(array, index, ((Long) value).longValue());
260: return;
261: } else if (value instanceof Short) {
262: setShort(array, index, ((Short) value).shortValue());
263: return;
264: } else if (value instanceof Byte) {
265: setByte(array, index, ((Byte) value).byteValue());
266: return;
267: }
268: } else if (value instanceof Boolean) {
269: setBoolean(array, index, ((Boolean) value)
270: .booleanValue());
271: return;
272: } else if (value instanceof Character) {
273: setChar(array, index, ((Character) value).charValue());
274: return;
275: }
276: } catch (ArrayStoreException e) {
277: throw new IllegalArgumentException(e.getMessage());
278: }
279: throw new IllegalArgumentException(
280: "Can not assign the specified value to the specified array component");
281: }
282:
283: /**
284: * @com.intel.drl.spec_ref
285: */
286: public static void setBoolean(Object array, int index, boolean value)
287: throws IllegalArgumentException,
288: ArrayIndexOutOfBoundsException {
289: try {
290: ((boolean[]) array)[index] = value;
291: } catch (ClassCastException e) {
292: throw new IllegalArgumentException(e.getMessage());
293: }
294: }
295:
296: /**
297: * @com.intel.drl.spec_ref
298: */
299: public static void setByte(Object array, int index, byte value)
300: throws IllegalArgumentException,
301: ArrayIndexOutOfBoundsException {
302: if (array instanceof byte[]) {
303: ((byte[]) array)[index] = value;
304: return;
305: }
306: setShort(array, index, value);
307: }
308:
309: /**
310: * @com.intel.drl.spec_ref
311: */
312: public static void setChar(Object array, int index, char value)
313: throws IllegalArgumentException,
314: ArrayIndexOutOfBoundsException {
315: if (array instanceof char[]) {
316: ((char[]) array)[index] = value;
317: return;
318: }
319: setInt(array, index, value);
320: }
321:
322: /**
323: * @com.intel.drl.spec_ref
324: */
325: public static void setDouble(Object array, int index, double value)
326: throws IllegalArgumentException,
327: ArrayIndexOutOfBoundsException {
328: try {
329: ((double[]) array)[index] = value;
330: } catch (ClassCastException e) {
331: throw new IllegalArgumentException(e.getMessage());
332: }
333: }
334:
335: /**
336: * @com.intel.drl.spec_ref
337: */
338: public static void setFloat(Object array, int index, float value)
339: throws IllegalArgumentException,
340: ArrayIndexOutOfBoundsException {
341: if (array instanceof float[]) {
342: ((float[]) array)[index] = value;
343: return;
344: }
345: setDouble(array, index, value);
346: }
347:
348: /**
349: * @com.intel.drl.spec_ref
350: */
351: public static void setInt(Object array, int index, int value)
352: throws IllegalArgumentException,
353: ArrayIndexOutOfBoundsException {
354: if (array instanceof int[]) {
355: ((int[]) array)[index] = value;
356: return;
357: }
358: setLong(array, index, value);
359: }
360:
361: /**
362: * @com.intel.drl.spec_ref
363: */
364: public static void setLong(Object array, int index, long value)
365: throws IllegalArgumentException,
366: ArrayIndexOutOfBoundsException {
367: if (array instanceof long[]) {
368: ((long[]) array)[index] = value;
369: return;
370: }
371: setFloat(array, index, value);
372: }
373:
374: /**
375: * @com.intel.drl.spec_ref
376: */
377: public static void setShort(Object array, int index, short value)
378: throws IllegalArgumentException,
379: ArrayIndexOutOfBoundsException {
380: if (array instanceof short[]) {
381: ((short[]) array)[index] = value;
382: return;
383: }
384: setInt(array, index, value);
385: }
386: }
|