KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > eval > CodeSnippetEvaluator


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.eclipse.jdt.internal.eval;
12
13 import java.util.Map JavaDoc;
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 /**
28  * A code snippet evaluator compiles and returns class file for a code snippet.
29  * Or it reports problems against the code snippet.
30  */

31 public class CodeSnippetEvaluator extends Evaluator implements EvaluationConstants {
32     /**
33      * Whether the code snippet support classes should be found in the provided name environment
34      * or on disk.
35      */

36     static final boolean DEVELOPMENT_MODE = false;
37
38     /**
39      * The code snippet to evaluate.
40      */

41     char[] codeSnippet;
42
43     /**
44      * The code snippet to generated compilation unit mapper
45      */

46     CodeSnippetToCuMapper mapper;
47 /**
48  * Creates a new code snippet evaluator.
49  */

50 CodeSnippetEvaluator(char[] codeSnippet, EvaluationContext context, INameEnvironment environment, Map JavaDoc options, IRequestor requestor, IProblemFactory problemFactory) {
51     super(context, environment, options, requestor, problemFactory);
52     this.codeSnippet = codeSnippet;
53 }
54 /**
55  * @see org.eclipse.jdt.internal.eval.Evaluator
56  */

57 protected void addEvaluationResultForCompilationProblem(Map JavaDoc 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             // shift line number, source start and source end
68
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             // shift line number, source start and source end
77
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             // shift line number, source start and source end
86
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 /**
104  * @see org.eclipse.jdt.internal.eval.Evaluator
105  */

106 protected char[] getClassName() {
107     return CharOperation.concat(CODE_SNIPPET_CLASS_NAME_PREFIX, Integer.toString(EvaluationContext.CODE_SNIPPET_COUNTER + 1).toCharArray());
108 }
109 /**
110  * @see Evaluator
111  */

112 Compiler JavaDoc getCompiler(ICompilerRequestor compilerRequestor) {
113     Compiler JavaDoc compiler = null;
114     if (!DEVELOPMENT_MODE) {
115         // If we are not developping the code snippet support classes,
116
// use a regular compiler and feed its lookup environment with
117
// the code snippet support classes
118

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         // Initialize the compiler's lookup environment with the already compiled super classes
134
IBinaryType binary = this.context.getRootCodeSnippetBinary();
135         if (binary != null) {
136             compiler.lookupEnvironment.cacheBinaryType(binary, null /*no access restriction*/);
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(); // Should never happen since we compiled this type
147
}
148                 compiler.lookupEnvironment.cacheBinaryType(binaryType, null /*no access restriction*/);
149             }
150         }
151     } else {
152         // If we are developping the code snippet support classes,
153
// use a wrapped environment so that if the code snippet classes are not found
154
// then a default implementation is provided.
155

156         CompilerOptions compilerOptions = new CompilerOptions(this.options);
157         compilerOptions.performMethodsFullRecovery = true;
158         compilerOptions.performStatementsRecovery = true;
159         compiler = new Compiler JavaDoc(
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 /**
198  * @see org.eclipse.jdt.internal.eval.Evaluator
199  */

200 protected char[] getSource() {
201     return getMapper().cuSource;
202 }
203 /**
204  * Returns an environment that wraps the client's name environment.
205  * This wrapper always considers the wrapped environment then if the name is
206  * not found, it search in the code snippet support. This includes the superclass
207  * org.eclipse.jdt.internal.eval.target.CodeSnippet as well as the global variable classes.
208  */

209 private INameEnvironment getWrapperEnvironment() {
210     return new CodeSnippetEnvironment(this.environment, this.context);
211 }
212 }
213
Popular Tags