1 11 package org.eclipse.jdt.internal.eval; 12 13 import java.util.Map ; 14 15 import org.eclipse.jdt.core.compiler.*; 16 import org.eclipse.jdt.internal.compiler.ClassFile; 17 import org.eclipse.jdt.internal.compiler.Compiler; 18 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies; 19 import org.eclipse.jdt.internal.compiler.ICompilerRequestor; 20 import org.eclipse.jdt.internal.compiler.IProblemFactory; 21 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 22 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 23 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 24 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 25 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 26 27 31 public class CodeSnippetEvaluator extends Evaluator implements EvaluationConstants { 32 36 static final boolean DEVELOPMENT_MODE = false; 37 38 41 char[] codeSnippet; 42 43 46 CodeSnippetToCuMapper mapper; 47 50 CodeSnippetEvaluator(char[] codeSnippet, EvaluationContext context, INameEnvironment environment, Map options, IRequestor requestor, IProblemFactory problemFactory) { 51 super(context, environment, options, requestor, problemFactory); 52 this.codeSnippet = codeSnippet; 53 } 54 57 protected void addEvaluationResultForCompilationProblem(Map resultsByIDs, CategorizedProblem problem, char[] cuSource) { 58 CodeSnippetToCuMapper sourceMapper = getMapper(); 59 int pbLineNumber = problem.getSourceLineNumber(); 60 int evaluationType = sourceMapper.getEvaluationType(pbLineNumber); 61 62 char[] evaluationID = null; 63 switch(evaluationType) { 64 case EvaluationResult.T_PACKAGE: 65 evaluationID = this.context.packageName; 66 67 problem.setSourceLineNumber(1); 69 problem.setSourceStart(0); 70 problem.setSourceEnd(evaluationID.length - 1); 71 break; 72 73 case EvaluationResult.T_IMPORT: 74 evaluationID = sourceMapper.getImport(pbLineNumber); 75 76 problem.setSourceLineNumber(1); 78 problem.setSourceStart(0); 79 problem.setSourceEnd(evaluationID.length - 1); 80 break; 81 82 case EvaluationResult.T_CODE_SNIPPET: 83 evaluationID = this.codeSnippet; 84 85 problem.setSourceLineNumber(pbLineNumber - this.mapper.lineNumberOffset); 87 problem.setSourceStart(problem.getSourceStart() - this.mapper.startPosOffset); 88 problem.setSourceEnd(problem.getSourceEnd() - this.mapper.startPosOffset); 89 break; 90 91 case EvaluationResult.T_INTERNAL: 92 evaluationID = cuSource; 93 break; 94 } 95 96 EvaluationResult result = (EvaluationResult)resultsByIDs.get(evaluationID); 97 if (result == null) { 98 resultsByIDs.put(evaluationID, new EvaluationResult(evaluationID, evaluationType, new CategorizedProblem[] {problem})); 99 } else { 100 result.addProblem(problem); 101 } 102 } 103 106 protected char[] getClassName() { 107 return CharOperation.concat(CODE_SNIPPET_CLASS_NAME_PREFIX, Integer.toString(EvaluationContext.CODE_SNIPPET_COUNTER + 1).toCharArray()); 108 } 109 112 Compiler getCompiler(ICompilerRequestor compilerRequestor) { 113 Compiler compiler = null; 114 if (!DEVELOPMENT_MODE) { 115 119 CompilerOptions compilerOptions = new CompilerOptions(this.options); 120 compilerOptions.performMethodsFullRecovery = true; 121 compilerOptions.performStatementsRecovery = true; 122 compiler = 123 new CodeSnippetCompiler( 124 this.environment, 125 DefaultErrorHandlingPolicies.exitAfterAllProblems(), 126 compilerOptions, 127 compilerRequestor, 128 this.problemFactory, 129 this.context, 130 getMapper().startPosOffset, 131 getMapper().startPosOffset + this.codeSnippet.length - 1); 132 ((CodeSnippetParser) compiler.parser).lineSeparatorLength = this.context.lineSeparator.length(); 133 IBinaryType binary = this.context.getRootCodeSnippetBinary(); 135 if (binary != null) { 136 compiler.lookupEnvironment.cacheBinaryType(binary, null ); 137 } 138 VariablesInfo installedVars = this.context.installedVars; 139 if (installedVars != null) { 140 ClassFile[] globalClassFiles = installedVars.classFiles; 141 for (int i = 0; i < globalClassFiles.length; i++) { 142 ClassFileReader binaryType = null; 143 try { 144 binaryType = new ClassFileReader(globalClassFiles[i].getBytes(), null); 145 } catch (ClassFormatException e) { 146 e.printStackTrace(); } 148 compiler.lookupEnvironment.cacheBinaryType(binaryType, null ); 149 } 150 } 151 } else { 152 156 CompilerOptions compilerOptions = new CompilerOptions(this.options); 157 compilerOptions.performMethodsFullRecovery = true; 158 compilerOptions.performStatementsRecovery = true; 159 compiler = new Compiler ( 160 getWrapperEnvironment(), 161 DefaultErrorHandlingPolicies.exitAfterAllProblems(), 162 compilerOptions, 163 compilerRequestor, 164 this.problemFactory); 165 } 166 return compiler; 167 } 168 private CodeSnippetToCuMapper getMapper() { 169 if (this.mapper == null) { 170 char[] varClassName = null; 171 VariablesInfo installedVars = this.context.installedVars; 172 if (installedVars != null) { 173 char[] superPackageName = installedVars.packageName; 174 if (superPackageName != null && superPackageName.length != 0) { 175 varClassName = CharOperation.concat(superPackageName, installedVars.className, '.'); 176 } else { 177 varClassName = installedVars.className; 178 } 179 180 } 181 this.mapper = new CodeSnippetToCuMapper( 182 this.codeSnippet, 183 this.context.packageName, 184 this.context.imports, 185 getClassName(), 186 varClassName, 187 this.context.localVariableNames, 188 this.context.localVariableTypeNames, 189 this.context.localVariableModifiers, 190 this.context.declaringTypeName, 191 this.context.lineSeparator 192 ); 193 194 } 195 return this.mapper; 196 } 197 200 protected char[] getSource() { 201 return getMapper().cuSource; 202 } 203 209 private INameEnvironment getWrapperEnvironment() { 210 return new CodeSnippetEnvironment(this.environment, this.context); 211 } 212 } 213 | Popular Tags |