KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > instructions > ArrayAllocation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /**
30      * Constructor for ArrayAllocation.
31      * @param start
32      */

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     /*
41      * @see Instruction#execute()
42      */

43     public void execute() throws CoreException {
44         if (fHasInitializer) {
45             IJavaArray array = (IJavaArray) popValue();
46             pop(); // pop the type
47
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     /**
72      * Create and populate an array.
73      */

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 JavaDoc toString() {
94         return InstructionsEvaluationMessages.ArrayAllocation_array_allocation_1;
95     }
96
97 }
98
Popular Tags