KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.jdt.core.compiler.*;
19 import org.eclipse.jdt.internal.compiler.ClassFile;
20 import org.eclipse.jdt.internal.compiler.CompilationResult;
21 import org.eclipse.jdt.internal.compiler.Compiler;
22 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
23 import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
24 import org.eclipse.jdt.internal.compiler.IProblemFactory;
25 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
26 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
27 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
28 import org.eclipse.jdt.internal.core.util.Util;
29
30 /**
31  * A evaluator builds a compilation unit and compiles it into class files.
32  * If the compilation unit has problems, reports the problems using the
33  * requestor.
34  */

35 public abstract class Evaluator {
36     EvaluationContext context;
37     INameEnvironment environment;
38     Map JavaDoc options;
39     IRequestor requestor;
40     IProblemFactory problemFactory;
41 /**
42  * Creates a new evaluator.
43  */

44 Evaluator(EvaluationContext context, INameEnvironment environment, Map JavaDoc options, IRequestor requestor, IProblemFactory problemFactory) {
45     this.context = context;
46     this.environment = environment;
47     this.options = options;
48     this.requestor = requestor;
49     this.problemFactory = problemFactory;
50 }
51 /**
52  * Adds the given problem to the corresponding evaluation result in the given table. If the evaluation
53  * result doesn't exist yet, adds it in the table. Its evaluation id and evaluation type
54  * are computed so that they correspond to the given problem. If it is found to be an internal problem,
55  * then the evaluation id of the result is the given compilation unit source.
56  */

57 protected abstract void addEvaluationResultForCompilationProblem(Map JavaDoc resultsByIDs,CategorizedProblem problem, char[] cuSource);
58 /**
59  * Returns the evaluation results that converts the given compilation result that has problems.
60  * If the compilation result has more than one problem, then the problems are broken down so that
61  * each evaluation result has the same evaluation id.
62  */

63 protected EvaluationResult[] evaluationResultsForCompilationProblems(CompilationResult result, char[] cuSource) {
64     // Break down the problems and group them by ids in evaluation results
65
CategorizedProblem[] problems = result.getAllProblems();
66     HashMap JavaDoc resultsByIDs = new HashMap JavaDoc(5);
67     for (int i = 0; i < problems.length; i++) {
68         addEvaluationResultForCompilationProblem(resultsByIDs, problems[i], cuSource);
69     }
70
71     // Copy results
72
int size = resultsByIDs.size();
73     EvaluationResult[] evalResults = new EvaluationResult[size];
74     Iterator JavaDoc results = resultsByIDs.values().iterator();
75     for (int i = 0; i < size; i++) {
76         evalResults[i] = (EvaluationResult)results.next();
77     }
78
79     return evalResults;
80 }
81 /**
82  * Compiles and returns the class definitions for the current compilation unit.
83  * Returns null if there are any errors.
84  */

85 ClassFile[] getClasses() {
86     final char[] source = getSource();
87     final ArrayList JavaDoc classDefinitions = new ArrayList JavaDoc();
88
89     // The requestor collects the class definitions and problems
90
class CompilerRequestor implements ICompilerRequestor {
91         boolean hasErrors = false;
92         public void acceptResult(CompilationResult result) {
93             if (result.hasProblems()) {
94                 EvaluationResult[] evalResults = evaluationResultsForCompilationProblems(result, source);
95                 for (int i = 0; i < evalResults.length; i++) {
96                     EvaluationResult evalResult = evalResults[i];
97                     CategorizedProblem[] problems = evalResult.getProblems();
98                     for (int j = 0; j < problems.length; j++) {
99                         Evaluator.this.requestor.acceptProblem(problems[j], evalResult.getEvaluationID(), evalResult.getEvaluationType());
100                     }
101                 }
102             }
103             if (result.hasErrors()) {
104                 this.hasErrors = true;
105             } else {
106                 ClassFile[] classFiles = result.getClassFiles();
107                 for (int i = 0; i < classFiles.length; i++) {
108                     ClassFile classFile = classFiles[i];
109 /*
110             
111                     char[] filename = classFile.fileName();
112                     int length = filename.length;
113                     char[] relativeName = new char[length + 6];
114                     System.arraycopy(filename, 0, relativeName, 0, length);
115                     System.arraycopy(".class".toCharArray(), 0, relativeName, length, 6);
116                     CharOperation.replace(relativeName, '/', java.io.File.separatorChar);
117                     ClassFile.writeToDisk("d:/test/snippet", new String(relativeName), classFile.getBytes());
118                     String str = "d:/test/snippet" + "/" + new String(relativeName);
119                     System.out.println(org.eclipse.jdt.core.tools.classfmt.disassembler.ClassFileDisassembler.disassemble(str));
120  */

121                     classDefinitions.add(classFile);
122                 }
123             }
124         }
125     }
126
127     // Compile compilation unit
128
CompilerRequestor compilerRequestor = new CompilerRequestor();
129     Compiler JavaDoc compiler = getCompiler(compilerRequestor);
130     compiler.compile(new ICompilationUnit[] {new ICompilationUnit() {
131         public char[] getFileName() {
132              // Name of class is name of CU
133
return CharOperation.concat(Evaluator.this.getClassName(), Util.defaultJavaExtension().toCharArray());
134         }
135         public char[] getContents() {
136             return source;
137         }
138         public char[] getMainTypeName() {
139             return Evaluator.this.getClassName();
140         }
141         public char[][] getPackageName() {
142             return null;
143         }
144     }});
145     if (compilerRequestor.hasErrors) {
146         return null;
147     } else {
148         ClassFile[] result = new ClassFile[classDefinitions.size()];
149         classDefinitions.toArray(result);
150         return result;
151     }
152 }
153 /**
154  * Returns the name of the current class. This is the simple name of the class.
155  * This doesn't include the extension ".java" nor the name of the package.
156  */

157 protected abstract char[] getClassName();
158 /**
159  * Creates and returns a compiler for this evaluator.
160  */

161 Compiler JavaDoc getCompiler(ICompilerRequestor compilerRequestor) {
162     CompilerOptions compilerOptions = new CompilerOptions(this.options);
163     compilerOptions.performMethodsFullRecovery = true;
164     compilerOptions.performStatementsRecovery = true;
165     return new Compiler JavaDoc(
166         this.environment,
167         DefaultErrorHandlingPolicies.exitAfterAllProblems(),
168         compilerOptions,
169         compilerRequestor,
170         this.problemFactory);
171 }
172 /**
173  * Builds and returns the source for the current compilation unit.
174  */

175 protected abstract char[] getSource();
176 }
177
Popular Tags