PageRenderTime 87ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.debug.source_3.7.1.v20110802_r371/jdimodelsrc/org/eclipse/jdt/internal/debug/eval/ast/engine/EvaluationSourceGenerator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 229 lines | 162 code | 26 blank | 41 comment | 22 complexity | 92fdbf8b4748696017416264454cbef2 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 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.engine;
  12. import java.util.Iterator;
  13. import java.util.Map;
  14. import org.eclipse.core.runtime.CoreException;
  15. import org.eclipse.core.runtime.IStatus;
  16. import org.eclipse.core.runtime.Status;
  17. import org.eclipse.debug.core.DebugException;
  18. import org.eclipse.jdt.core.IJavaProject;
  19. import org.eclipse.jdt.core.IType;
  20. import org.eclipse.jdt.core.JavaCore;
  21. import org.eclipse.jdt.core.dom.AST;
  22. import org.eclipse.jdt.core.dom.ASTParser;
  23. import org.eclipse.jdt.core.dom.CompilationUnit;
  24. import org.eclipse.jdt.debug.core.IJavaReferenceType;
  25. import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
  26. import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
  27. import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType;
  28. /**
  29. * Creates the source code necessary to evaluate a code snippet.
  30. * The (simplified) structure of the source is as follows:
  31. * [package <package name>;]
  32. * [import <import name>;]*
  33. * public class <code snippet class name> extends <global variable class name> {
  34. * public void run() {
  35. * <code snippet>
  36. * }
  37. * }
  38. */
  39. public class EvaluationSourceGenerator {
  40. private String fCodeSnippet;
  41. private String[] fLocalVariableTypeNames;
  42. private String[] fLocalVariableNames;
  43. private String fSource;
  44. private String fCompilationUnitName;
  45. private int fSnippetStartPosition;
  46. private int fRunMethodStartPosition;
  47. private int fRunMethodLength;
  48. /**
  49. * Rebuild source in presence of external local variables
  50. */
  51. public EvaluationSourceGenerator(String[] localVariableTypesNames, String[] localVariableNames, String codeSnippet) {
  52. fLocalVariableTypeNames = localVariableTypesNames;
  53. fLocalVariableNames = localVariableNames;
  54. fCodeSnippet= getCompleteSnippet(codeSnippet);
  55. }
  56. public EvaluationSourceGenerator(String codeSnippet) {
  57. this(new String[0], new String[0], codeSnippet);
  58. }
  59. protected String getCompleteSnippet(String codeSnippet) {
  60. if (isExpression(codeSnippet)) {
  61. codeSnippet = "return " + codeSnippet + ';'; //$NON-NLS-1$
  62. }
  63. return codeSnippet;
  64. }
  65. /**
  66. * Returns whether the given snippet represents an expression.
  67. * This is determined by examining the snippet for non-quoted semicolons.
  68. *
  69. * Returns <code>true</code> if the snippet is an expression, or
  70. * <code>false</code> if the expresssion contains a statement.
  71. */
  72. protected boolean isExpression(String codeSnippet) {
  73. boolean inString= false;
  74. byte[] chars= codeSnippet.getBytes();
  75. for (int i= 0, numChars= chars.length; i < numChars; i++) {
  76. switch (chars[i]) {
  77. case '\\':
  78. if (inString) { // skip the char after an escape char
  79. i++;
  80. }
  81. break;
  82. case '\"':
  83. case '\'':
  84. inString= !inString;
  85. break;
  86. case ';':
  87. if (!inString) {
  88. return false;
  89. }
  90. break;
  91. }
  92. }
  93. return true;
  94. }
  95. public String getCompilationUnitName() {
  96. return fCompilationUnitName;
  97. }
  98. public int getSnippetStart() {
  99. return fSnippetStartPosition;
  100. }
  101. public int getRunMethodStart() {
  102. return fRunMethodStartPosition;
  103. }
  104. public int getRunMethodLength() {
  105. return fRunMethodLength;
  106. }
  107. protected void setSnippetStart(int position) {
  108. fSnippetStartPosition= position;
  109. }
  110. protected void setRunMethodStart(int position) {
  111. fRunMethodStartPosition= position;
  112. }
  113. protected void setRunMethodLength(int length) {
  114. fRunMethodLength= length;
  115. }
  116. public String getSnippet() {
  117. return fCodeSnippet;
  118. }
  119. private void createEvaluationSourceFromSource(String source, IType type, boolean createInAStaticMethod, IJavaProject project) throws DebugException {
  120. ASTParser parser = ASTParser.newParser(AST.JLS4);
  121. parser.setSource(source.toCharArray());
  122. Map options=getCompilerOptions(project);
  123. String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
  124. parser.setCompilerOptions(options);
  125. CompilationUnit unit= (CompilationUnit)parser.createAST(null);
  126. SourceBasedSourceGenerator visitor= new SourceBasedSourceGenerator(type, createInAStaticMethod, fLocalVariableTypeNames, fLocalVariableNames, fCodeSnippet, sourceLevel);
  127. unit.accept(visitor);
  128. if (visitor.hasError()) {
  129. throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, visitor.getError(), null));
  130. }
  131. String sourceRes= visitor.getSource();
  132. if (sourceRes == null) {
  133. return;
  134. }
  135. setSource(sourceRes);
  136. setCompilationUnitName(visitor.getCompilationUnitName());
  137. setSnippetStart(visitor.getSnippetStart());
  138. setRunMethodStart(visitor.getRunMethodStart());
  139. setRunMethodLength(visitor.getRunMethodLength());
  140. }
  141. /**
  142. * Returns the compiler options used for compiling the expression.
  143. * <p>
  144. * Turns all errors and warnings into ignore and disables task tags. The customizable set of
  145. * compiler options only contains additional Eclipse options. The standard JDK compiler options
  146. * can't be changed anyway.
  147. *
  148. * @param element an element (not the Java model)
  149. * @return compiler options
  150. */
  151. public static Map getCompilerOptions(IJavaProject project) {
  152. Map options= project.getOptions(true);
  153. for (Iterator iter= options.keySet().iterator(); iter.hasNext();) {
  154. String key= (String)iter.next();
  155. String value= (String)options.get(key);
  156. if (JavaCore.ERROR.equals(value) || JavaCore.WARNING.equals(value)) {
  157. options.put(key, JavaCore.IGNORE);
  158. }
  159. }
  160. options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
  161. return options;
  162. }
  163. private void createEvaluationSourceFromJDIObject(BinaryBasedSourceGenerator objectToEvaluationSourceMapper) {
  164. setCompilationUnitName(objectToEvaluationSourceMapper.getCompilationUnitName());
  165. setSnippetStart(objectToEvaluationSourceMapper.getSnippetStart());
  166. setRunMethodStart(objectToEvaluationSourceMapper.getRunMethodStart());
  167. setRunMethodLength(fCodeSnippet.length() + objectToEvaluationSourceMapper.getRunMethodLength());
  168. setSource(objectToEvaluationSourceMapper.getSource().insert(objectToEvaluationSourceMapper.getCodeSnippetPosition(), fCodeSnippet).toString());
  169. }
  170. private BinaryBasedSourceGenerator getInstanceSourceMapper(JDIReferenceType referenceType, boolean isInStaticMethod, IJavaProject project) {
  171. String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
  172. BinaryBasedSourceGenerator objectToEvaluationSourceMapper = new BinaryBasedSourceGenerator(fLocalVariableTypeNames, fLocalVariableNames, isInStaticMethod, sourceLevel);
  173. objectToEvaluationSourceMapper.buildSource(referenceType);
  174. return objectToEvaluationSourceMapper;
  175. }
  176. public String getSource(IJavaReferenceType type, IJavaProject javaProject, boolean isStatic) throws CoreException {
  177. if (fSource == null) {
  178. IType iType = JavaDebugUtils.resolveType(type);
  179. if (iType != null && !iType.isInterface()) {
  180. String baseSource = null;
  181. if (iType.isBinary()) {
  182. baseSource = iType.getClassFile().getSource();
  183. } else {
  184. baseSource = iType.getCompilationUnit().getSource();
  185. }
  186. if (baseSource != null) {
  187. createEvaluationSourceFromSource(baseSource, iType, isStatic, javaProject);
  188. }
  189. }
  190. if (fSource == null) {
  191. BinaryBasedSourceGenerator mapper= getInstanceSourceMapper((JDIReferenceType) type, isStatic, javaProject);
  192. createEvaluationSourceFromJDIObject(mapper);
  193. }
  194. }
  195. return fSource;
  196. }
  197. protected void setCompilationUnitName(String name) {
  198. fCompilationUnitName= name;
  199. }
  200. protected void setSource(String source) {
  201. fSource= source;
  202. }
  203. }