KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > eval > EvaluationResult


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.eval;
12
13 import org.eclipse.jdt.core.compiler.CategorizedProblem;
14
15 /**
16  * An EvaluationResult is the result of a code snippet evaluation, a global
17  * variable evaluation or it is used to report problems against imports and
18  * package declaration.
19  * It primarily contains the representation of the resulting value (eg. its
20  * toString() representation). However if the code snippet, a global variable
21  * definition, an import or the package declaration could not be compiled, it
22  * contains the corresponding compilation problems.
23  */

24 public class EvaluationResult {
25
26     static final CategorizedProblem[] NO_PROBLEMS = new CategorizedProblem[0];
27     
28     char[] evaluationID;
29     int evaluationType;
30     CategorizedProblem[] problems;
31     char[] displayString;
32     char[] typeName;
33
34     /**
35      * The evaluation result contains the value of a variable or
36      * it reports a problem on a variable. Note that if the problem is
37      * on the type of the variable, the source line number is -1. If the
38      * problem is on the name of the variable, the source line number is 0.
39      * Otherwise the source line number is relative to the initializer code.
40      */

41     public static final int T_VARIABLE = 1;
42
43     /**
44      * The evaluation result contains the value of a code snippet or
45      * it reports a problem on a code snippet.
46      */

47     public static final int T_CODE_SNIPPET = 2;
48
49     /**
50      * The evaluation result reports a problem on an import declaration.
51      */

52     public static final int T_IMPORT = 3;
53
54     /**
55      * The evaluation result reports a problem on a package declaration.
56      */

57     public static final int T_PACKAGE = 4;
58
59     /**
60      * The evaluation result reports an internal problem.
61      */

62     public static final int T_INTERNAL = 5;
63     
64 public EvaluationResult(char[] evaluationID, int evaluationType, char[] displayString, char[] typeName) {
65     this.evaluationID = evaluationID;
66     this.evaluationType = evaluationType;
67     this.displayString = displayString;
68     this.typeName = typeName;
69     this.problems = NO_PROBLEMS;
70 }
71 public EvaluationResult(char[] evaluationID, int evaluationType, CategorizedProblem[] problems) {
72     this.evaluationID = evaluationID;
73     this.evaluationType = evaluationType;
74     this.problems = problems;
75 }
76 /**
77  * Adds the given problem to the list of problems of this evaluation result.
78  */

79 void addProblem(CategorizedProblem problem) {
80     CategorizedProblem[] existingProblems = this.problems;
81     int existingLength = existingProblems.length;
82     this.problems = new CategorizedProblem[existingLength + 1];
83     System.arraycopy(existingProblems, 0, this.problems, 0, existingLength);
84     this.problems[existingLength] = problem;
85 }
86 /**
87  * Returns the ID of the evaluation.
88  * If the result is about a global variable, returns the name of the variable.
89  * If the result is about a code snippet, returns the code snippet.
90  * If the result is about an import, returns the import.
91  * If the result is about a package declaration, returns the package declaration.
92  */

93 public char[] getEvaluationID() {
94     return this.evaluationID;
95 }
96 /**
97  * Returns the type of evaluation this result is about.
98  * This indicates if the result is about a global variable,
99  * a code snippet, an import or a package declaration.
100  * Use getEvaluationID() to get the object itself.
101  */

102 public int getEvaluationType() {
103     return this.evaluationType;
104 }
105 /**
106  * Returns an array of problems (errors and warnings) encountered
107  * during the compilation of a code snippet or a global variable definition,
108  * or during the analysis of a package name or an import.
109  * Returns an empty array if there are no problems.
110  */

111 public CategorizedProblem[] getProblems() {
112     return this.problems;
113 }
114 /**
115  * Returns a proxy object on this result's value.
116  * Returns null if the result's value is null.
117  * The returned value is undefined if there is no result.
118  * The proxy object is expected to answer questions like:
119  * - What is the proxy type for this object?
120  * - What is the toString() representation for this object?
121  * - What are the field names of this object?
122  * - What is the value for a given field name?
123  * Special proxy objects are expected if the value is a primitive type.
124  */

125 public Object JavaDoc getValue() {
126     return null; // Not yet implemented
127
}
128 /**
129  * Returns the displayable representation of this result's value.
130  * This is obtained by sending toString() to the result object on the target side
131  * if it is not a primitive value. If it is a primitive value, the corresponding
132  * static toString(...) is used, eg. Integer.toString(int n) if it is an int.
133  * Returns null if there is no value.
134  */

135 public char[] getValueDisplayString() {
136     return this.displayString;
137 }
138 /**
139  * Returns the dot-separated fully qualified name of this result's value type.
140  * If the value is a primitive value, returns the toString() representation of its type
141  * (eg. "int", "boolean", etc.)
142  * Returns null if there is no value.
143  */

144 public char[] getValueTypeName() {
145     return this.typeName;
146 }
147 /**
148  * Returns whether there are errors in the code snippet or the global variable definition.
149  */

150 public boolean hasErrors() {
151     if (this.problems == null) {
152         return false;
153     } else {
154         for (int i = 0; i < this.problems.length; i++) {
155             if (this.problems[i].isError()) {
156                 return true;
157             }
158         }
159         return false;
160     }
161 }
162 /**
163  * Returns whether there are problems in the code snippet or the global variable definition.
164  */

165 public boolean hasProblems() {
166     return (this.problems != null) && (this.problems.length != 0);
167 }
168 /**
169  * Returns whether this result has a value.
170  */

171 public boolean hasValue() {
172     return this.displayString != null;
173 }
174 /**
175  * Returns whether there are warnings in the code snippet or the global variable definition.
176  */

177 public boolean hasWarnings() {
178     if (this.problems == null) {
179         return false;
180     } else {
181         for (int i = 0; i < this.problems.length; i++) {
182             if (this.problems[i].isWarning()) {
183                 return true;
184             }
185         }
186         return false;
187     }
188 }
189 /**
190  * Returns a readable representation of this result.
191  * This is for debugging purpose only.
192  */

193 public String JavaDoc toString() {
194     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
195     switch (this.evaluationType) {
196         case T_CODE_SNIPPET:
197             buffer.append("Code snippet"); //$NON-NLS-1$
198
break;
199         case T_IMPORT:
200             buffer.append("Import"); //$NON-NLS-1$
201
break;
202         case T_INTERNAL:
203             buffer.append("Internal problem"); //$NON-NLS-1$
204
break;
205         case T_PACKAGE:
206             buffer.append("Package"); //$NON-NLS-1$
207
break;
208         case T_VARIABLE:
209             buffer.append("Global variable"); //$NON-NLS-1$
210
break;
211     }
212     buffer.append(": "); //$NON-NLS-1$
213
buffer.append(this.evaluationID == null ? "<unknown>".toCharArray() : this.evaluationID); //$NON-NLS-1$
214
buffer.append("\n"); //$NON-NLS-1$
215
if (hasProblems()) {
216         buffer.append("Problems:\n"); //$NON-NLS-1$
217
for (int i = 0; i < this.problems.length; i++) {
218             buffer.append(this.problems[i].toString());
219         }
220     } else {
221         if (hasValue()) {
222             buffer.append("("); //$NON-NLS-1$
223
buffer.append(this.typeName);
224             buffer.append(") "); //$NON-NLS-1$
225
buffer.append(this.displayString);
226         } else {
227             buffer.append("(No explicit return value)"); //$NON-NLS-1$
228
}
229     }
230     return buffer.toString();
231 }
232 }
233
Popular Tags