PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/aspectj-1.6.9/aspectjtools1.6.9/org/aspectj/org/eclipse/jdt/internal/eval/EvaluationResult.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 232 lines | 121 code | 10 blank | 101 comment | 21 complexity | f1c8e746234d955cf8d3321260dfe990 MD5 | raw file
  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.aspectj.org.eclipse.jdt.internal.eval;
  12. import org.aspectj.org.eclipse.jdt.core.compiler.CategorizedProblem;
  13. /**
  14. * An EvaluationResult is the result of a code snippet evaluation, a global
  15. * variable evaluation or it is used to report problems against imports and
  16. * package declaration.
  17. * It primarily contains the representation of the resulting value (eg. its
  18. * toString() representation). However if the code snippet, a global variable
  19. * definition, an import or the package declaration could not be compiled, it
  20. * contains the corresponding compilation problems.
  21. */
  22. public class EvaluationResult {
  23. static final CategorizedProblem[] NO_PROBLEMS = new CategorizedProblem[0];
  24. char[] evaluationID;
  25. int evaluationType;
  26. CategorizedProblem[] problems;
  27. char[] displayString;
  28. char[] typeName;
  29. /**
  30. * The evaluation result contains the value of a variable or
  31. * it reports a problem on a variable. Note that if the problem is
  32. * on the type of the variable, the source line number is -1. If the
  33. * problem is on the name of the variable, the source line number is 0.
  34. * Otherwise the source line number is relative to the initializer code.
  35. */
  36. public static final int T_VARIABLE = 1;
  37. /**
  38. * The evaluation result contains the value of a code snippet or
  39. * it reports a problem on a code snippet.
  40. */
  41. public static final int T_CODE_SNIPPET = 2;
  42. /**
  43. * The evaluation result reports a problem on an import declaration.
  44. */
  45. public static final int T_IMPORT = 3;
  46. /**
  47. * The evaluation result reports a problem on a package declaration.
  48. */
  49. public static final int T_PACKAGE = 4;
  50. /**
  51. * The evaluation result reports an internal problem.
  52. */
  53. public static final int T_INTERNAL = 5;
  54. public EvaluationResult(char[] evaluationID, int evaluationType, char[] displayString, char[] typeName) {
  55. this.evaluationID = evaluationID;
  56. this.evaluationType = evaluationType;
  57. this.displayString = displayString;
  58. this.typeName = typeName;
  59. this.problems = NO_PROBLEMS;
  60. }
  61. public EvaluationResult(char[] evaluationID, int evaluationType, CategorizedProblem[] problems) {
  62. this.evaluationID = evaluationID;
  63. this.evaluationType = evaluationType;
  64. this.problems = problems;
  65. }
  66. /**
  67. * Adds the given problem to the list of problems of this evaluation result.
  68. */
  69. void addProblem(CategorizedProblem problem) {
  70. CategorizedProblem[] existingProblems = this.problems;
  71. int existingLength = existingProblems.length;
  72. this.problems = new CategorizedProblem[existingLength + 1];
  73. System.arraycopy(existingProblems, 0, this.problems, 0, existingLength);
  74. this.problems[existingLength] = problem;
  75. }
  76. /**
  77. * Returns the ID of the evaluation.
  78. * If the result is about a global variable, returns the name of the variable.
  79. * If the result is about a code snippet, returns the code snippet.
  80. * If the result is about an import, returns the import.
  81. * If the result is about a package declaration, returns the package declaration.
  82. */
  83. public char[] getEvaluationID() {
  84. return this.evaluationID;
  85. }
  86. /**
  87. * Returns the type of evaluation this result is about.
  88. * This indicates if the result is about a global variable,
  89. * a code snippet, an import or a package declaration.
  90. * Use getEvaluationID() to get the object itself.
  91. */
  92. public int getEvaluationType() {
  93. return this.evaluationType;
  94. }
  95. /**
  96. * Returns an array of problems (errors and warnings) encountered
  97. * during the compilation of a code snippet or a global variable definition,
  98. * or during the analysis of a package name or an import.
  99. * Returns an empty array if there are no problems.
  100. */
  101. public CategorizedProblem[] getProblems() {
  102. return this.problems;
  103. }
  104. /**
  105. * Returns a proxy object on this result's value.
  106. * Returns null if the result's value is null.
  107. * The returned value is undefined if there is no result.
  108. * The proxy object is expected to answer questions like:
  109. * - What is the proxy type for this object?
  110. * - What is the toString() representation for this object?
  111. * - What are the field names of this object?
  112. * - What is the value for a given field name?
  113. * Special proxy objects are expected if the value is a primitive type.
  114. */
  115. public Object getValue() {
  116. return null; // Not yet implemented
  117. }
  118. /**
  119. * Returns the displayable representation of this result's value.
  120. * This is obtained by sending toString() to the result object on the target side
  121. * if it is not a primitive value. If it is a primitive value, the corresponding
  122. * static toString(...) is used, eg. Integer.toString(int n) if it is an int.
  123. * Returns null if there is no value.
  124. */
  125. public char[] getValueDisplayString() {
  126. return this.displayString;
  127. }
  128. /**
  129. * Returns the dot-separated fully qualified name of this result's value type.
  130. * If the value is a primitive value, returns the toString() representation of its type
  131. * (eg. "int", "boolean", etc.)
  132. * Returns null if there is no value.
  133. */
  134. public char[] getValueTypeName() {
  135. return this.typeName;
  136. }
  137. /**
  138. * Returns whether there are errors in the code snippet or the global variable definition.
  139. */
  140. public boolean hasErrors() {
  141. if (this.problems == null) {
  142. return false;
  143. } else {
  144. for (int i = 0; i < this.problems.length; i++) {
  145. if (this.problems[i].isError()) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. }
  152. /**
  153. * Returns whether there are problems in the code snippet or the global variable definition.
  154. */
  155. public boolean hasProblems() {
  156. return (this.problems != null) && (this.problems.length != 0);
  157. }
  158. /**
  159. * Returns whether this result has a value.
  160. */
  161. public boolean hasValue() {
  162. return this.displayString != null;
  163. }
  164. /**
  165. * Returns whether there are warnings in the code snippet or the global variable definition.
  166. */
  167. public boolean hasWarnings() {
  168. if (this.problems == null) {
  169. return false;
  170. } else {
  171. for (int i = 0; i < this.problems.length; i++) {
  172. if (this.problems[i].isWarning()) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. }
  179. /**
  180. * Returns a readable representation of this result.
  181. * This is for debugging purpose only.
  182. */
  183. public String toString() {
  184. StringBuffer buffer = new StringBuffer();
  185. switch (this.evaluationType) {
  186. case T_CODE_SNIPPET:
  187. buffer.append("Code snippet"); //$NON-NLS-1$
  188. break;
  189. case T_IMPORT:
  190. buffer.append("Import"); //$NON-NLS-1$
  191. break;
  192. case T_INTERNAL:
  193. buffer.append("Internal problem"); //$NON-NLS-1$
  194. break;
  195. case T_PACKAGE:
  196. buffer.append("Package"); //$NON-NLS-1$
  197. break;
  198. case T_VARIABLE:
  199. buffer.append("Global variable"); //$NON-NLS-1$
  200. break;
  201. }
  202. buffer.append(": "); //$NON-NLS-1$
  203. buffer.append(this.evaluationID == null ? "<unknown>".toCharArray() : this.evaluationID); //$NON-NLS-1$
  204. buffer.append("\n"); //$NON-NLS-1$
  205. if (hasProblems()) {
  206. buffer.append("Problems:\n"); //$NON-NLS-1$
  207. for (int i = 0; i < this.problems.length; i++) {
  208. buffer.append(this.problems[i].toString());
  209. }
  210. } else {
  211. if (hasValue()) {
  212. buffer.append("("); //$NON-NLS-1$
  213. buffer.append(this.typeName);
  214. buffer.append(") "); //$NON-NLS-1$
  215. buffer.append(this.displayString);
  216. } else {
  217. buffer.append("(No explicit return value)"); //$NON-NLS-1$
  218. }
  219. }
  220. return buffer.toString();
  221. }
  222. }