1 33 34 35 package bsh; 36 37 import java.lang.reflect.Array ; 38 39 43 class BSHArrayDimensions extends SimpleNode 44 { 45 public Class baseType; 46 public int numDefinedDims; 47 public int numUndefinedDims; 48 53 public int [] definedDimensions; 54 55 BSHArrayDimensions(int id) { super(id); } 56 57 public void addDefinedDimension() { numDefinedDims++; } 58 public void addUndefinedDimension() { numUndefinedDims++; } 59 60 public Object eval( 61 Class type, CallStack callstack, Interpreter interpreter ) 62 throws EvalError 63 { 64 if ( Interpreter.DEBUG ) Interpreter.debug("array base type = "+type); 65 baseType = type; 66 return eval( callstack, interpreter ); 67 } 68 69 81 public Object eval( CallStack callstack, Interpreter interpreter ) 82 throws EvalError 83 { 84 SimpleNode child = (SimpleNode)jjtGetChild(0); 85 86 93 if (child instanceof BSHArrayInitializer) 94 { 95 if ( baseType == null ) 96 throw new EvalError( 97 "Internal Array Eval err: unknown base type", 98 this, callstack ); 99 100 Object initValue = ((BSHArrayInitializer)child).eval( 101 baseType, numUndefinedDims, callstack, interpreter); 102 103 Class arrayClass = initValue.getClass(); 104 int actualDimensions = Reflect.getArrayDimensions(arrayClass); 105 definedDimensions = new int[ actualDimensions ]; 106 107 if ( definedDimensions.length != numUndefinedDims ) 110 throw new EvalError( 111 "Incompatible initializer. Allocation calls for a " + 112 numUndefinedDims+ " dimensional array, but initializer is a " + 113 actualDimensions + " dimensional array", this, callstack ); 114 115 Object arraySlice = initValue; 117 for ( int i = 0; i < definedDimensions.length; i++ ) { 118 definedDimensions[i] = Array.getLength( arraySlice ); 119 if ( definedDimensions[i] > 0 ) 120 arraySlice = Array.get(arraySlice, 0); 121 } 122 123 return initValue; 124 } 125 else 126 { 128 definedDimensions = new int[ numDefinedDims ]; 129 130 for(int i = 0; i < numDefinedDims; i++) 131 { 132 try { 133 Object length = ((SimpleNode)jjtGetChild(i)).eval( 134 callstack, interpreter); 135 definedDimensions[i] = ((Primitive)length).intValue(); 136 } 137 catch(Exception e) 138 { 139 throw new EvalError( 140 "Array index: " + i + 141 " does not evaluate to an integer", this, callstack ); 142 } 143 } 144 } 145 146 return Primitive.VOID; 147 } 148 } 149 | Popular Tags |