1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler;
14 * A compilation result consists of all information returned by the compiler for
15 * a single compiled compilation source unit. This includes:
17 * <li> the compilation unit that was compiled
18 * <li> for each type produced by compiling the compilation unit, its binary and optionally its principal structure
19 * <li> any problems (errors or warnings) produced
20 * <li> dependency info
23 * The principle structure and binary may be null if the compiler could not produce them.
24 * If neither could be produced, there is no corresponding entry for the type.
26 * The dependency info includes type references such as supertypes, field types, method
27 * parameter and return types, local variable types, types of intermediate expressions, etc.
28 * It also includes the namespaces (packages) in which names were looked up.
29 * It does <em>not</em> include finer grained dependencies such as information about
30 * specific fields and methods which were referenced, but does contain their
31 * declaring types and any other types used to locate such fields or methods.
34 import org.eclipse.jdt.core.compiler.*;
35 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
36 import org.eclipse.jdt.internal.compiler.env.*;
37 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
41 public class CompilationResult {
43 public IProblem problems[];
44 public IProblem tasks[];
45 public int problemCount;
47 public ICompilationUnit compilationUnit;
48 private Map problemsMap;
49 private Map firstErrorsMap;
50 private int maxProblemPerUnit;
51 public char[][][] qualifiedReferences;
52 public char[][] simpleNameReferences;
54 public int lineSeparatorPositions[];
55 public Hashtable compiledTypes = new Hashtable(11);
56 public int unitIndex, totalUnitsKnown;
57 public boolean hasBeenAccepted = false;
58 public char[] fileName;
60 public CompilationResult(
64 int maxProblemPerUnit){
66 this.fileName = fileName;
67 this.unitIndex = unitIndex;
68 this.totalUnitsKnown = totalUnitsKnown;
69 this.maxProblemPerUnit = maxProblemPerUnit;
72 public CompilationResult(
73 ICompilationUnit compilationUnit,
76 int maxProblemPerUnit){
78 this.fileName = compilationUnit.getFileName();
79 this.compilationUnit = compilationUnit;
80 this.unitIndex = unitIndex;
81 this.totalUnitsKnown = totalUnitsKnown;
82 this.maxProblemPerUnit = maxProblemPerUnit;
85 private int computePriority(IProblem problem){
87 final int P_STATIC = 10000;
88 final int P_OUTSIDE_METHOD = 40000;
89 final int P_FIRST_ERROR = 20000;
90 final int P_ERROR = 100000;
92 int priority = 10000 - problem.getSourceLineNumber(); // early problems first
93 if (priority < 0) priority = 0;
94 if (problem.isError()){
97 ReferenceContext context = problemsMap == null ? null : (ReferenceContext) problemsMap.get(problem);
99 if (context instanceof AbstractMethodDeclaration){
100 AbstractMethodDeclaration method = (AbstractMethodDeclaration) context;
101 if (method.isStatic()) {
102 priority += P_STATIC;
105 priority += P_OUTSIDE_METHOD;
108 priority += P_OUTSIDE_METHOD;
110 if (firstErrorsMap.containsKey(problem)){
111 priority += P_FIRST_ERROR;
117 public IProblem[] getAllProblems() {
118 IProblem[] onlyProblems = this.getProblems();
119 int onlyProblemCount = onlyProblems != null ? onlyProblems.length : 0;
120 IProblem[] onlyTasks = this.getTasks();
121 int onlyTaskCount = onlyTasks != null ? onlyTasks.length : 0;
122 if (onlyTaskCount == 0) {
125 if (onlyProblemCount == 0) {
129 int totalNumberOfProblem = onlyProblemCount + onlyTaskCount;
130 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
131 int allProblemIndex = 0;
133 int problemIndex = 0;
134 while (taskIndex + problemIndex < totalNumberOfProblem) {
135 IProblem nextTask = null;
136 IProblem nextProblem = null;
137 if (taskIndex < onlyTaskCount) {
138 nextTask = onlyTasks[taskIndex];
140 if (problemIndex < onlyProblemCount) {
141 nextProblem = onlyProblems[problemIndex];
143 // select the next problem
144 IProblem currentProblem = null;
145 if (nextProblem != null) {
146 if (nextTask != null) {
147 if (nextProblem.getSourceStart() < nextTask.getSourceStart()) {
148 currentProblem = nextProblem;
151 currentProblem = nextTask;
155 currentProblem = nextProblem;
159 if (nextTask != null) {
160 currentProblem = nextTask;
164 allProblems[allProblemIndex++] = currentProblem;
169 public ClassFile[] getClassFiles() {
170 Enumeration files = compiledTypes.elements();
171 ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
173 while (files.hasMoreElements()){
174 classFiles[index++] = (ClassFile)files.nextElement();
180 * Answer the initial compilation unit corresponding to the present compilation result
182 public ICompilationUnit getCompilationUnit(){
183 return compilationUnit;
187 * Answer the initial file name
189 public char[] getFileName(){
194 * Answer the errors encountered during compilation.
196 public IProblem[] getErrors() {
198 IProblem[] reportedProblems = getProblems();
200 for (int i = 0; i < this.problemCount; i++) {
201 if (reportedProblems[i].isError()) errorCount++;
203 if (errorCount == this.problemCount) return reportedProblems;
204 IProblem[] errors = new IProblem[errorCount];
206 for (int i = 0; i < this.problemCount; i++) {
207 if (reportedProblems[i].isError()) errors[index++] = reportedProblems[i];
213 * Answer the problems (errors and warnings) encountered during compilation.
215 * This is not a compiler internal API - it has side-effects !
216 * It is intended to be used only once all problems have been detected,
217 * and makes sure the problems slot as the exact size of the number of
220 public IProblem[] getProblems() {
222 // Re-adjust the size of the problems if necessary.
223 if (problems != null) {
225 if (this.problemCount != problems.length) {
226 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
229 if (this.maxProblemPerUnit > 0 && this.problemCount > this.maxProblemPerUnit){
230 quickPrioritize(problems, 0, problemCount - 1);
231 this.problemCount = this.maxProblemPerUnit;
232 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
235 // Sort problems per source positions.
236 quickSort(problems, 0, problems.length-1);
242 * Answer the tasks (TO-DO, ...) encountered during compilation.
244 * This is not a compiler internal API - it has side-effects !
245 * It is intended to be used only once all problems have been detected,
246 * and makes sure the problems slot as the exact size of the number of
249 public IProblem[] getTasks() {
251 // Re-adjust the size of the tasks if necessary.
252 if (this.tasks != null) {
254 if (this.taskCount != this.tasks.length) {
255 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
257 quickSort(tasks, 0, tasks.length-1);
262 public boolean hasErrors() {
264 if (problems != null)
265 for (int i = 0; i < problemCount; i++) {
266 if (problems[i].isError())
272 public boolean hasProblems() {
274 return problemCount != 0;
277 public boolean hasSyntaxError(){
279 if (problems != null)
280 for (int i = 0; i < problemCount; i++) {
281 IProblem problem = problems[i];
282 if ((problem.getID() & IProblem.Syntax) != 0 && problem.isError())
288 public boolean hasTasks() {
289 return this.taskCount != 0;
292 public boolean hasWarnings() {
294 if (problems != null)
295 for (int i = 0; i < problemCount; i++) {
296 if (problems[i].isWarning())
302 private static void quickSort(IProblem[] list, int left, int right) {
304 if (left >= right) return;
306 // sort the problems by their source start position... starting with 0
307 int original_left = left;
308 int original_right = right;
309 int mid = list[(left + right) / 2].getSourceStart();
311 while (list[left].getSourceStart() < mid)
313 while (mid < list[right].getSourceStart())
316 IProblem tmp = list[left];
317 list[left] = list[right];
322 } while (left <= right);
323 if (original_left < right)
324 quickSort(list, original_left, right);
325 if (left < original_right)
326 quickSort(list, left, original_right);
329 private void quickPrioritize(IProblem[] list, int left, int right) {
331 if (left >= right) return;
333 // sort the problems by their priority... starting with the highest priority
334 int original_left = left;
335 int original_right = right;
336 int mid = computePriority(list[(left + right) / 2]);
338 while (computePriority(list[right]) < mid)
340 while (mid < computePriority(list[left]))
343 IProblem tmp = list[left];
344 list[left] = list[right];
349 } while (left <= right);
350 if (original_left < right)
351 quickPrioritize(list, original_left, right);
352 if (left < original_right)
353 quickPrioritize(list, left, original_right);
357 * For now, remember the compiled type using its compound name.
359 public void record(char[] typeName, ClassFile classFile) {
361 compiledTypes.put(typeName, classFile);
364 public void record(IProblem newProblem, ReferenceContext referenceContext) {
366 if (newProblem.getID() == IProblem.Task) {
367 recordTask(newProblem);
370 if (problemCount == 0) {
371 problems = new IProblem[5];
372 } else if (problemCount == problems.length) {
373 System.arraycopy(problems, 0, (problems = new IProblem[problemCount * 2]), 0, problemCount);
375 problems[problemCount++] = newProblem;
376 if (referenceContext != null){
377 if (problemsMap == null) problemsMap = new Hashtable(5);
378 if (firstErrorsMap == null) firstErrorsMap = new Hashtable(5);
379 if (newProblem.isError() && !referenceContext.hasErrors()) firstErrorsMap.put(newProblem, newProblem);
380 problemsMap.put(newProblem, referenceContext);
384 private void recordTask(IProblem newProblem) {
385 if (this.taskCount == 0) {
386 this.tasks = new IProblem[5];
387 } else if (this.taskCount == this.tasks.length) {
388 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount * 2]), 0, this.taskCount);
390 this.tasks[this.taskCount++] = newProblem;
393 public CompilationResult tagAsAccepted(){
395 this.hasBeenAccepted = true;
396 this.problemsMap = null; // flush
400 public String toString(){
402 StringBuffer buffer = new StringBuffer();
403 if (this.fileName != null){
404 buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
406 if (this.compiledTypes != null){
407 buffer.append("COMPILED type(s) \n"); //$NON-NLS-1$
408 Enumeration typeNames = this.compiledTypes.keys();
409 while (typeNames.hasMoreElements()) {
410 char[] typeName = (char[]) typeNames.nextElement();
411 buffer.append("\t - ").append(typeName).append('\n'); //$NON-NLS-1$
415 buffer.append("No COMPILED type\n"); //$NON-NLS-1$
417 if (problems != null){
418 buffer.append(this.problemCount).append(" PROBLEM(s) detected \n"); //$NON-NLS-1$//$NON-NLS-2$
419 for (int i = 0; i < this.problemCount; i++){
420 buffer.append("\t - ").append(this.problems[i]).append('\n'); //$NON-NLS-1$
423 buffer.append("No PROBLEM\n"); //$NON-NLS-1$
425 return buffer.toString();