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.ICompilerRequestor; 19 import org.eclipse.jdt.internal.compiler.IProblemFactory; 20 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 21 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 22 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 23 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 24 25 29 public class VariablesEvaluator extends Evaluator implements EvaluationConstants { 30 33 VariablesEvaluator(EvaluationContext context, INameEnvironment environment, Map options, IRequestor requestor, IProblemFactory problemFactory) { 34 super(context, environment, options, requestor, problemFactory); 35 } 36 39 protected void addEvaluationResultForCompilationProblem(Map resultsByIDs, CategorizedProblem problem, char[] cuSource) { 40 char[] evaluationID = cuSource; 42 int evaluationType = EvaluationResult.T_INTERNAL; 43 44 int pbLine = problem.getSourceLineNumber(); 45 int currentLine = 1; 46 47 char[] packageName = getPackageName(); 49 if (packageName.length > 0) { 50 if (pbLine == 1) { 51 evaluationID = packageName; 53 evaluationType = EvaluationResult.T_PACKAGE; 54 55 problem.setSourceLineNumber(1); 57 problem.setSourceStart(0); 58 problem.setSourceEnd(evaluationID.length - 1); 59 } 60 currentLine++; 61 } 62 63 char[][] imports = this.context.imports; 65 if ((currentLine <= pbLine) && (pbLine < (currentLine + imports.length))) { 66 evaluationID = imports[pbLine - currentLine]; 68 evaluationType = EvaluationResult.T_IMPORT; 69 70 problem.setSourceLineNumber(1); 72 problem.setSourceStart(0); 73 problem.setSourceEnd(evaluationID.length - 1); 74 } 75 currentLine += imports.length + 1; 77 int varCount = this.context.variableCount; 79 if ((currentLine <= pbLine) && (pbLine < currentLine + varCount)) { 80 GlobalVariable var = this.context.variables[pbLine - currentLine]; 81 82 evaluationID = var.getName(); 84 evaluationType = EvaluationResult.T_VARIABLE; 85 86 int pbStart = problem.getSourceStart() - var.declarationStart; 88 int pbEnd = problem.getSourceEnd() - var.declarationStart; 89 int typeLength = var.getTypeName().length; 90 if ((0 <= pbStart) && (pbEnd < typeLength)) { 91 problem.setSourceLineNumber(-1); 93 } else { 94 pbStart -= typeLength + 1; pbEnd -= typeLength + 1; problem.setSourceLineNumber(0); 98 } 99 problem.setSourceStart(pbStart); 100 problem.setSourceEnd(pbEnd); 101 } 102 currentLine = -1; 104 for (int i = 0; i < varCount; i++) { 106 GlobalVariable var = this.context.variables[i]; 107 char[] initializer = var.getInitializer(); 108 int initializerLength = initializer == null ? 0 : initializer.length; 109 if ((var.initializerStart <= problem.getSourceStart()) && (problem.getSourceEnd() < var.initializerStart + var.name.length)) { 110 114 return; 115 } else if ((var.initExpressionStart <= problem.getSourceStart()) && (problem.getSourceEnd() < var.initExpressionStart + initializerLength)) { 116 evaluationID = var.name; 118 evaluationType = EvaluationResult.T_VARIABLE; 119 120 problem.setSourceLineNumber(pbLine - var.initializerLineStart + 1); 122 problem.setSourceStart(problem.getSourceStart() - var.initExpressionStart); 123 problem.setSourceEnd(problem.getSourceEnd() - var.initExpressionStart); 124 125 break; 126 } 127 } 128 129 EvaluationResult result = (EvaluationResult)resultsByIDs.get(evaluationID); 130 if (result == null) { 131 resultsByIDs.put(evaluationID, new EvaluationResult(evaluationID, evaluationType, new CategorizedProblem[] {problem})); 132 } else { 133 result.addProblem(problem); 134 } 135 } 136 139 protected char[] getClassName() { 140 return CharOperation.concat(EvaluationConstants.GLOBAL_VARS_CLASS_NAME_PREFIX, Integer.toString(EvaluationContext.VAR_CLASS_COUNTER + 1).toCharArray()); 141 } 142 145 Compiler getCompiler(ICompilerRequestor compilerRequestor) { 146 Compiler compiler = super.getCompiler(compilerRequestor); 147 148 IBinaryType binaryType = this.context.getRootCodeSnippetBinary(); 150 if (binaryType != null) { 151 compiler.lookupEnvironment.cacheBinaryType(binaryType, null ); 152 } 153 154 VariablesInfo installedVars = this.context.installedVars; 156 if (installedVars != null) { 157 ClassFile[] classFiles = installedVars.classFiles; 158 for (int i = 0; i < classFiles.length; i++) { 159 ClassFile classFile = classFiles[i]; 160 IBinaryType binary = null; 161 try { 162 binary = new ClassFileReader(classFile.getBytes(), null); 163 } catch (ClassFormatException e) { 164 e.printStackTrace(); } 166 compiler.lookupEnvironment.cacheBinaryType(binary, null ); 167 } 168 } 169 170 return compiler; 171 } 172 175 protected char[] getPackageName() { 176 return this.context.packageName; 177 } 178 181 protected char[] getSource() { 182 StringBuffer buffer = new StringBuffer (); 183 int lineNumberOffset = 1; 184 185 char[] packageName = getPackageName(); 187 if (packageName.length != 0) { 188 buffer.append("package "); buffer.append(packageName); 190 buffer.append(';').append(this.context.lineSeparator); 191 lineNumberOffset++; 192 } 193 194 char[][] imports = this.context.imports; 196 for (int i = 0; i < imports.length; i++) { 197 buffer.append("import "); buffer.append(imports[i]); 199 buffer.append(';').append(this.context.lineSeparator); 200 lineNumberOffset++; 201 } 202 203 buffer.append("public class "); buffer.append(getClassName()); 206 buffer.append(" extends "); buffer.append(PACKAGE_NAME); 208 buffer.append("."); buffer.append(ROOT_CLASS_NAME); 210 buffer.append(" {").append(this.context.lineSeparator); lineNumberOffset++; 212 213 GlobalVariable[] vars = this.context.variables; 215 VariablesInfo installedVars = this.context.installedVars; 216 for (int i = 0; i < this.context.variableCount; i++){ 217 GlobalVariable var = vars[i]; 218 buffer.append("\tpublic static "); var.declarationStart = buffer.length(); 220 buffer.append(var.typeName); 221 buffer.append(" "); char[] varName = var.name; 223 buffer.append(varName); 224 buffer.append(';').append(this.context.lineSeparator); 225 lineNumberOffset++; 226 } 227 228 buffer.append("\tstatic {").append(this.context.lineSeparator); lineNumberOffset++; 231 for (int i = 0; i < this.context.variableCount; i++){ 232 GlobalVariable var = vars[i]; 233 char[] varName = var.name; 234 GlobalVariable installedVar = installedVars == null ? null : installedVars.varNamed(varName); 235 if (installedVar == null || !CharOperation.equals(installedVar.typeName, var.typeName)) { 236 char[] initializer = var.initializer; 238 if (initializer != null) { 239 buffer.append("\t\ttry {").append(this.context.lineSeparator); lineNumberOffset++; 241 var.initializerLineStart = lineNumberOffset; 242 buffer.append("\t\t\t"); var.initializerStart = buffer.length(); 244 buffer.append(varName); 245 buffer.append("= "); var.initExpressionStart = buffer.length(); 247 buffer.append(initializer); 248 lineNumberOffset += numberOfCRs(initializer); 249 buffer.append(';').append(this.context.lineSeparator); 250 buffer.append("\t\t} catch (Throwable e) {").append(this.context.lineSeparator); buffer.append("\t\t\te.printStackTrace();").append(this.context.lineSeparator); buffer.append("\t\t}").append(this.context.lineSeparator); lineNumberOffset += 4; } 255 } else { 256 buffer.append("\t\t"); buffer.append(varName); 259 buffer.append("= "); char[] installedPackageName = installedVars.packageName; 261 if (installedPackageName != null && installedPackageName.length != 0) { 262 buffer.append(installedPackageName); 263 buffer.append("."); } 265 buffer.append(installedVars.className); 266 buffer.append("."); buffer.append(varName); 268 buffer.append(';').append(this.context.lineSeparator); 269 lineNumberOffset++; 270 } 271 } 272 buffer.append("\t}").append(this.context.lineSeparator); 274 buffer.append('}').append(this.context.lineSeparator); 276 277 int length = buffer.length(); 279 char[] result = new char[length]; 280 buffer.getChars(0, length, result, 0); 281 return result; 282 } 283 286 private int numberOfCRs(char[] source) { 287 int numberOfCRs = 0; 288 boolean lastWasCR = false; 289 for (int i = 0; i < source.length; i++) { 290 char currentChar = source[i]; 291 switch(currentChar){ 292 case '\r' : 293 lastWasCR = true; 294 numberOfCRs++; 295 break; 296 case '\n' : 297 if (!lastWasCR) numberOfCRs++; lastWasCR = false; 299 break; 300 default : 301 lastWasCR = false; 302 } 303 } 304 return numberOfCRs; 305 } 306 } 307 | Popular Tags |