PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.core.source_3.7.1.v_B76_R37x/org/eclipse/jdt/internal/eval/VariablesEvaluator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 306 lines | 216 code | 25 blank | 65 comment | 43 complexity | dab7c7685758b7a1aa544f1803e223c6 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2009 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. import java.util.Map;
  13. import org.eclipse.jdt.core.compiler.*;
  14. import org.eclipse.jdt.internal.compiler.ClassFile;
  15. import org.eclipse.jdt.internal.compiler.Compiler;
  16. import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
  17. import org.eclipse.jdt.internal.compiler.IProblemFactory;
  18. import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
  19. import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
  20. import org.eclipse.jdt.internal.compiler.env.IBinaryType;
  21. import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
  22. /**
  23. * A variables evaluator compiles the global variables of an evaluation context and returns
  24. * the corresponding class files. Or it reports problems against these variables.
  25. */
  26. public class VariablesEvaluator extends Evaluator implements EvaluationConstants {
  27. /**
  28. * Creates a new global variables evaluator.
  29. */
  30. VariablesEvaluator(EvaluationContext context, INameEnvironment environment, Map options, IRequestor requestor, IProblemFactory problemFactory) {
  31. super(context, environment, options, requestor, problemFactory);
  32. }
  33. /**
  34. * @see org.eclipse.jdt.internal.eval.Evaluator
  35. */
  36. protected void addEvaluationResultForCompilationProblem(Map resultsByIDs, CategorizedProblem problem, char[] cuSource) {
  37. // set evaluation id and type to an internal problem by default
  38. char[] evaluationID = cuSource;
  39. int evaluationType = EvaluationResult.T_INTERNAL;
  40. int pbLine = problem.getSourceLineNumber();
  41. int currentLine = 1;
  42. // check package declaration
  43. char[] packageName = getPackageName();
  44. if (packageName.length > 0) {
  45. if (pbLine == 1) {
  46. // set evaluation id and type
  47. evaluationID = packageName;
  48. evaluationType = EvaluationResult.T_PACKAGE;
  49. // shift line number, source start and source end
  50. problem.setSourceLineNumber(1);
  51. problem.setSourceStart(0);
  52. problem.setSourceEnd(evaluationID.length - 1);
  53. }
  54. currentLine++;
  55. }
  56. // check imports
  57. char[][] imports = this.context.imports;
  58. if ((currentLine <= pbLine) && (pbLine < (currentLine + imports.length))) {
  59. // set evaluation id and type
  60. evaluationID = imports[pbLine - currentLine];
  61. evaluationType = EvaluationResult.T_IMPORT;
  62. // shift line number, source start and source end
  63. problem.setSourceLineNumber(1);
  64. problem.setSourceStart(0);
  65. problem.setSourceEnd(evaluationID.length - 1);
  66. }
  67. currentLine += imports.length + 1; // + 1 to skip the class declaration line
  68. // check variable declarations
  69. int varCount = this.context.variableCount;
  70. if ((currentLine <= pbLine) && (pbLine < currentLine + varCount)) {
  71. GlobalVariable var = this.context.variables[pbLine - currentLine];
  72. // set evaluation id and type
  73. evaluationID = var.getName();
  74. evaluationType = EvaluationResult.T_VARIABLE;
  75. // shift line number, source start and source end
  76. int pbStart = problem.getSourceStart() - var.declarationStart;
  77. int pbEnd = problem.getSourceEnd() - var.declarationStart;
  78. int typeLength = var.getTypeName().length;
  79. if ((0 <= pbStart) && (pbEnd < typeLength)) {
  80. // problem on the type of the variable
  81. problem.setSourceLineNumber(-1);
  82. } else {
  83. // problem on the name of the variable
  84. pbStart -= typeLength + 1; // type length + space
  85. pbEnd -= typeLength + 1; // type length + space
  86. problem.setSourceLineNumber(0);
  87. }
  88. problem.setSourceStart(pbStart);
  89. problem.setSourceEnd(pbEnd);
  90. }
  91. currentLine = -1; // not needed any longer
  92. // check variable initializers
  93. for (int i = 0; i < varCount; i++) {
  94. GlobalVariable var = this.context.variables[i];
  95. char[] initializer = var.getInitializer();
  96. int initializerLength = initializer == null ? 0 : initializer.length;
  97. if ((var.initializerStart <= problem.getSourceStart()) && (problem.getSourceEnd() < var.initializerStart + var.name.length)) {
  98. /* Problem with the variable name.
  99. Ignore because it must have already been reported
  100. when checking the declaration.
  101. */
  102. return;
  103. } else if ((var.initExpressionStart <= problem.getSourceStart()) && (problem.getSourceEnd() < var.initExpressionStart + initializerLength)) {
  104. // set evaluation id and type
  105. evaluationID = var.name;
  106. evaluationType = EvaluationResult.T_VARIABLE;
  107. // shift line number, source start and source end
  108. problem.setSourceLineNumber(pbLine - var.initializerLineStart + 1);
  109. problem.setSourceStart(problem.getSourceStart() - var.initExpressionStart);
  110. problem.setSourceEnd(problem.getSourceEnd() - var.initExpressionStart);
  111. break;
  112. }
  113. }
  114. EvaluationResult result = (EvaluationResult)resultsByIDs.get(evaluationID);
  115. if (result == null) {
  116. resultsByIDs.put(evaluationID, new EvaluationResult(evaluationID, evaluationType, new CategorizedProblem[] {problem}));
  117. } else {
  118. result.addProblem(problem);
  119. }
  120. }
  121. /**
  122. * @see org.eclipse.jdt.internal.eval.Evaluator
  123. */
  124. protected char[] getClassName() {
  125. return CharOperation.concat(EvaluationConstants.GLOBAL_VARS_CLASS_NAME_PREFIX, Integer.toString(EvaluationContext.VAR_CLASS_COUNTER + 1).toCharArray());
  126. }
  127. /**
  128. * Creates and returns a compiler for this evaluator.
  129. */
  130. Compiler getCompiler(ICompilerRequestor compilerRequestor) {
  131. Compiler compiler = super.getCompiler(compilerRequestor);
  132. // Initialize the compiler's lookup environment with the already compiled super class
  133. IBinaryType binaryType = this.context.getRootCodeSnippetBinary();
  134. if (binaryType != null) {
  135. compiler.lookupEnvironment.cacheBinaryType(binaryType, null /*no access restriction*/);
  136. }
  137. // and the installed global variable classes
  138. VariablesInfo installedVars = this.context.installedVars;
  139. if (installedVars != null) {
  140. ClassFile[] classFiles = installedVars.classFiles;
  141. for (int i = 0; i < classFiles.length; i++) {
  142. ClassFile classFile = classFiles[i];
  143. IBinaryType binary = null;
  144. try {
  145. binary = new ClassFileReader(classFile.getBytes(), null);
  146. } catch (ClassFormatException e) {
  147. e.printStackTrace(); // Should never happen since we compiled this type
  148. }
  149. compiler.lookupEnvironment.cacheBinaryType(binary, null /*no access restriction*/);
  150. }
  151. }
  152. return compiler;
  153. }
  154. /**
  155. * Returns the name of package of the current compilation unit.
  156. */
  157. protected char[] getPackageName() {
  158. return this.context.packageName;
  159. }
  160. /**
  161. * @see org.eclipse.jdt.internal.eval.Evaluator
  162. */
  163. protected char[] getSource() {
  164. StringBuffer buffer = new StringBuffer();
  165. int lineNumberOffset = 1;
  166. // package declaration
  167. char[] packageName = getPackageName();
  168. if (packageName.length != 0) {
  169. buffer.append("package "); //$NON-NLS-1$
  170. buffer.append(packageName);
  171. buffer.append(';').append(this.context.lineSeparator);
  172. lineNumberOffset++;
  173. }
  174. // import declarations
  175. char[][] imports = this.context.imports;
  176. for (int i = 0; i < imports.length; i++) {
  177. buffer.append("import "); //$NON-NLS-1$
  178. buffer.append(imports[i]);
  179. buffer.append(';').append(this.context.lineSeparator);
  180. lineNumberOffset++;
  181. }
  182. // class declaration
  183. buffer.append("public class "); //$NON-NLS-1$
  184. buffer.append(getClassName());
  185. buffer.append(" extends "); //$NON-NLS-1$
  186. buffer.append(PACKAGE_NAME);
  187. buffer.append("."); //$NON-NLS-1$
  188. buffer.append(ROOT_CLASS_NAME);
  189. buffer.append(" {").append(this.context.lineSeparator); //$NON-NLS-1$
  190. lineNumberOffset++;
  191. // field declarations
  192. GlobalVariable[] vars = this.context.variables;
  193. VariablesInfo installedVars = this.context.installedVars;
  194. for (int i = 0; i < this.context.variableCount; i++){
  195. GlobalVariable var = vars[i];
  196. buffer.append("\tpublic static "); //$NON-NLS-1$
  197. var.declarationStart = buffer.length();
  198. buffer.append(var.typeName);
  199. buffer.append(" "); //$NON-NLS-1$
  200. char[] varName = var.name;
  201. buffer.append(varName);
  202. buffer.append(';').append(this.context.lineSeparator);
  203. lineNumberOffset++;
  204. }
  205. // field initializations
  206. buffer.append("\tstatic {").append(this.context.lineSeparator); //$NON-NLS-1$
  207. lineNumberOffset++;
  208. for (int i = 0; i < this.context.variableCount; i++){
  209. GlobalVariable var = vars[i];
  210. char[] varName = var.name;
  211. GlobalVariable installedVar = installedVars == null ? null : installedVars.varNamed(varName);
  212. if (installedVar == null || !CharOperation.equals(installedVar.typeName, var.typeName)) {
  213. // Initialize with initializer if there was no previous value
  214. char[] initializer = var.initializer;
  215. if (initializer != null) {
  216. buffer.append("\t\ttry {").append(this.context.lineSeparator); //$NON-NLS-1$
  217. lineNumberOffset++;
  218. var.initializerLineStart = lineNumberOffset;
  219. buffer.append("\t\t\t"); //$NON-NLS-1$
  220. var.initializerStart = buffer.length();
  221. buffer.append(varName);
  222. buffer.append("= "); //$NON-NLS-1$
  223. var.initExpressionStart = buffer.length();
  224. buffer.append(initializer);
  225. lineNumberOffset += numberOfCRs(initializer);
  226. buffer.append(';').append(this.context.lineSeparator);
  227. buffer.append("\t\t} catch (Throwable e) {").append(this.context.lineSeparator); //$NON-NLS-1$
  228. buffer.append("\t\t\te.printStackTrace();").append(this.context.lineSeparator); //$NON-NLS-1$
  229. buffer.append("\t\t}").append(this.context.lineSeparator); //$NON-NLS-1$
  230. lineNumberOffset += 4; // 4 CRs
  231. }
  232. } else {
  233. // Initialize with previous value if name and type are the same
  234. buffer.append("\t\t"); //$NON-NLS-1$
  235. buffer.append(varName);
  236. buffer.append("= "); //$NON-NLS-1$
  237. char[] installedPackageName = installedVars.packageName;
  238. if (installedPackageName != null && installedPackageName.length != 0) {
  239. buffer.append(installedPackageName);
  240. buffer.append("."); //$NON-NLS-1$
  241. }
  242. buffer.append(installedVars.className);
  243. buffer.append("."); //$NON-NLS-1$
  244. buffer.append(varName);
  245. buffer.append(';').append(this.context.lineSeparator);
  246. lineNumberOffset++;
  247. }
  248. }
  249. buffer.append("\t}").append(this.context.lineSeparator); //$NON-NLS-1$
  250. // end of class declaration
  251. buffer.append('}').append(this.context.lineSeparator);
  252. // return result
  253. int length = buffer.length();
  254. char[] result = new char[length];
  255. buffer.getChars(0, length, result, 0);
  256. return result;
  257. }
  258. /**
  259. * Returns the number of cariage returns included in the given source.
  260. */
  261. private int numberOfCRs(char[] source) {
  262. int numberOfCRs = 0;
  263. boolean lastWasCR = false;
  264. for (int i = 0; i < source.length; i++) {
  265. char currentChar = source[i];
  266. switch(currentChar){
  267. case '\r' :
  268. lastWasCR = true;
  269. numberOfCRs++;
  270. break;
  271. case '\n' :
  272. if (!lastWasCR) numberOfCRs++; // merge CR-LF
  273. lastWasCR = false;
  274. break;
  275. default :
  276. lastWasCR = false;
  277. }
  278. }
  279. return numberOfCRs;
  280. }
  281. }