private static Object dereferenceArrayDims( Interp interp, // Current // interpreter. Object arrayObj, // Array to dereference. Must be an array. int numDerefDims, // Number of dimensions to dereference. TclObject indexListObj) // Index to dereference in each dim. throws TclException // May encounter bad array index or // dereference a null array value. { // Before derefencing any dimensions, check that the indexList isn't too // large--we want to return an array. int numDims = JavaInfoCmd.getNumDimsFromClass(arrayObj.getClass()); if (numDims < numDerefDims) { throw new TclException( interp, "bad indexList \"" + indexListObj.toString() + "\": javaObj only has " + numDims + " dimension(s)"); } Object subArrayObj = arrayObj; for (int dim = 0; dim < numDerefDims - 1; dim++) { int index = TclInteger.getInt(interp, TclList.index(interp, indexListObj, dim)); try { subArrayObj = Array.get(subArrayObj, index); } catch (ArrayIndexOutOfBoundsException e) { int max = Array.getLength(subArrayObj) - 1; throw new TclException( interp, "array index \"" + index + "\" is out of bounds: must be between 0 and " + max); } if (subArrayObj == null) { throw new TclException( interp, "null value in dimension " + dim + ": can't dereference " + numDims + " dimensions"); } } return subArrayObj; }
private static Class dereferenceClassDims( Interp interp, // Current // interpreter. Class arrayClass, // Array class type. int numDerefDims) // Number of dimensions to dereference. throws TclException // May encounter bad array index or // dereference a null array value. { // Before derefencing class, check that the numDerefDims isn't too large int numDims = JavaInfoCmd.getNumDimsFromClass(arrayClass); if (numDims < numDerefDims) { throw new TclException( interp, "bad class dereference class only has " + numDims + " dimension(s)"); } Class subArrayClass = arrayClass; for (int dim = 0; dim < numDerefDims; dim++) { subArrayClass = subArrayClass.getComponentType(); } return subArrayClass; }