1 11 package org.eclipse.jdt.internal.debug.eval.ast.instructions; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.jdt.debug.core.IJavaArray; 15 import org.eclipse.jdt.debug.core.IJavaArrayType; 16 import org.eclipse.jdt.debug.core.IJavaPrimitiveValue; 17 import org.eclipse.jdt.debug.core.IJavaType; 18 19 public class ArrayAllocation extends ArrayInstruction { 20 21 private int fDimension; 22 23 private int fExprDimension; 24 25 private boolean fHasInitializer; 26 27 private IJavaArrayType[] fCachedArrayTypes; 28 29 33 public ArrayAllocation(int dimension, int exprDimension, boolean hasInitializer, int start) { 34 super(start); 35 fDimension = dimension; 36 fExprDimension = exprDimension; 37 fHasInitializer = hasInitializer; 38 } 39 40 43 public void execute() throws CoreException { 44 if (fHasInitializer) { 45 IJavaArray array = (IJavaArray) popValue(); 46 pop(); push(array); 48 } else { 49 50 int[] exprDimensions = new int[fExprDimension]; 51 52 for (int i = fExprDimension - 1; i >= 0; i--) { 53 exprDimensions[i] = ((IJavaPrimitiveValue)popValue()).getIntValue(); 54 } 55 56 IJavaType type = (IJavaType) pop(); 57 58 fCachedArrayTypes = new IJavaArrayType[fDimension + 1]; 59 60 for (int i =fDimension, lim = fDimension - fExprDimension ; i > lim; i--) { 61 fCachedArrayTypes[i] = (IJavaArrayType) type; 62 type = ((IJavaArrayType)type).getComponentType(); 63 } 64 65 IJavaArray array = createArray(fDimension, exprDimensions); 66 67 push(array); 68 } 69 } 70 71 74 private IJavaArray createArray(int dimension, int[] exprDimensions) throws CoreException { 75 76 IJavaArray array = fCachedArrayTypes[dimension].newInstance(exprDimensions[0]); 77 78 if (exprDimensions.length > 1) { 79 int[] newExprDimension = new int[exprDimensions.length - 1]; 80 for (int i = 0; i < newExprDimension.length; i++) { 81 newExprDimension[i] = exprDimensions[i + 1]; 82 } 83 84 for (int i = 0; i < exprDimensions[0]; i++) { 85 array.setValue(i, createArray(dimension - 1, newExprDimension)); 86 } 87 88 } 89 90 return array; 91 } 92 93 public String toString() { 94 return InstructionsEvaluationMessages.ArrayAllocation_array_allocation_1; 95 } 96 97 } 98 | Popular Tags |