KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.dom.Message;
19 import org.eclipse.jdt.debug.eval.ICompiledExpression;
20
21 public class InstructionSequence implements ICompiledExpression {
22
23     private List JavaDoc fInstructions;
24     /**
25      * A collection of error messages (<code>String</code>) that occurred while
26      * creating this expression
27      */

28     private List JavaDoc fErrors;
29     private String JavaDoc fSnippet;
30     private CoreException fException;
31     
32     public InstructionSequence(String JavaDoc snippet) {
33         fInstructions= new ArrayList JavaDoc(10);
34         fErrors= new ArrayList JavaDoc();
35         fSnippet= snippet;
36     }
37     
38     /**
39      * Returns the runtime exception that occurred while evaluating this expression
40      * or <code>null</code> if no exception occurred.
41      */

42     public CoreException getException() {
43         return fException;
44     }
45     
46     /**
47      * @see ICompiledExpression#getSnippet()
48      */

49     public String JavaDoc getSnippet() {
50         return fSnippet;
51     }
52     
53     /**
54      * Adds the given error to the list of errors that occurred
55      * while compiling this instruction sequence
56      */

57     public void addError(String JavaDoc error) {
58         fErrors.add(error);
59     }
60     
61     /**
62      * @see ICompiledExpression#hasErrors()
63      */

64     public boolean hasErrors() {
65         return !fErrors.isEmpty();
66     }
67     
68     /**
69      * @see ICompiledExpression#getErrors()
70      * @deprecated
71      */

72     public Message[] getErrors() {
73         Message[] messages= new Message[fErrors.size()];
74         int i= 0;
75         for (Iterator JavaDoc iter= fErrors.iterator(); iter.hasNext();) {
76             messages[i++]= new Message((String JavaDoc) iter.next(), -1);
77         }
78         return messages;
79     }
80     
81     /**
82      * @see org.eclipse.jdt.debug.eval.ICompiledExpression#getErrorMessages()
83      */

84     public String JavaDoc[] getErrorMessages() {
85         return (String JavaDoc[])fErrors.toArray(new String JavaDoc[fErrors.size()]);
86     }
87
88     /**
89      * Answers the array of instructions, or an empty array.
90      */

91     public Instruction[] getInstructions() {
92         int size= fInstructions.size();
93         Instruction[] instructions= new Instruction[size];
94         if (size > 0) {
95             fInstructions.toArray(instructions);
96         }
97         return instructions;
98     }
99     
100     /**
101      * Answer the instruction at the given address
102      */

103     public Instruction getInstruction(int address) {
104         return (Instruction)fInstructions.get(address);
105     }
106     
107     /**
108      * Add the given instruction to the end of the list
109      */

110     public void add(Instruction instruction) {
111         fInstructions.add(instruction);
112     }
113     
114     public int indexOf(Instruction instruction) {
115         return fInstructions.indexOf(instruction);
116     }
117
118     /**
119      * Answers true if there are no instructions in this sequence
120      */

121     public boolean isEmpty() {
122         return fInstructions.isEmpty();
123     }
124
125     /**
126      * Inserts the instruction at the given index. If
127      * the index is less than 0 or greater than the current
128      * instruction count, the instruction is added at the end
129      * of the sequence.
130      *
131      * Instructs the instructions to update their program counters.
132      */

133     public void insert(Instruction instruction, int index) {
134         fInstructions.add(index, instruction);
135     }
136     
137     public Instruction get(int address) {
138         return (Instruction)fInstructions.get(address);
139     }
140     
141     public int getEnd() {
142         return fInstructions.size() - 1;
143     }
144 }
145
Popular Tags