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;
38 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
42 public class CompilationResult {
44 public IProblem problems[];
45 public IProblem tasks[];
46 public int problemCount;
48 public ICompilationUnit compilationUnit;
49 private Map problemsMap;
50 private Map firstErrorsMap;
51 private int maxProblemPerUnit;
52 public char[][][] qualifiedReferences;
53 public char[][] simpleNameReferences;
55 public int lineSeparatorPositions[];
56 public Hashtable compiledTypes = new Hashtable(11);
57 public int unitIndex, totalUnitsKnown;
58 public boolean hasBeenAccepted = false;
59 public char[] fileName;
60 public boolean hasInconsistentToplevelHierarchies = false; // record the fact some toplevel types have inconsistent hierarchies
62 public CompilationResult(
66 int maxProblemPerUnit){
68 this.fileName = fileName;
69 this.unitIndex = unitIndex;
70 this.totalUnitsKnown = totalUnitsKnown;
71 this.maxProblemPerUnit = maxProblemPerUnit;
74 public CompilationResult(
75 ICompilationUnit compilationUnit,
78 int maxProblemPerUnit){
80 this.fileName = compilationUnit.getFileName();
81 this.compilationUnit = compilationUnit;
82 this.unitIndex = unitIndex;
83 this.totalUnitsKnown = totalUnitsKnown;
84 this.maxProblemPerUnit = maxProblemPerUnit;
87 private int computePriority(IProblem problem){
89 final int P_STATIC = 10000;
90 final int P_OUTSIDE_METHOD = 40000;
91 final int P_FIRST_ERROR = 20000;
92 final int P_ERROR = 100000;
94 int priority = 10000 - problem.getSourceLineNumber(); // early problems first
95 if (priority < 0) priority = 0;
96 if (problem.isError()){
99 ReferenceContext context = problemsMap == null ? null : (ReferenceContext) problemsMap.get(problem);
100 if (context != null){
101 if (context instanceof AbstractMethodDeclaration){
102 AbstractMethodDeclaration method = (AbstractMethodDeclaration) context;
103 if (method.isStatic()) {
104 priority += P_STATIC;
107 priority += P_OUTSIDE_METHOD;
110 priority += P_OUTSIDE_METHOD;
112 if (firstErrorsMap.containsKey(problem)){
113 priority += P_FIRST_ERROR;
119 public IProblem[] getAllProblems() {
120 IProblem[] onlyProblems = this.getProblems();
121 int onlyProblemCount = onlyProblems != null ? onlyProblems.length : 0;
122 IProblem[] onlyTasks = this.getTasks();
123 int onlyTaskCount = onlyTasks != null ? onlyTasks.length : 0;
124 if (onlyTaskCount == 0) {
127 if (onlyProblemCount == 0) {
131 int totalNumberOfProblem = onlyProblemCount + onlyTaskCount;
132 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
133 int allProblemIndex = 0;
135 int problemIndex = 0;
136 while (taskIndex + problemIndex < totalNumberOfProblem) {
137 IProblem nextTask = null;
138 IProblem nextProblem = null;
139 if (taskIndex < onlyTaskCount) {
140 nextTask = onlyTasks[taskIndex];
142 if (problemIndex < onlyProblemCount) {
143 nextProblem = onlyProblems[problemIndex];
145 // select the next problem
146 IProblem currentProblem = null;
147 if (nextProblem != null) {
148 if (nextTask != null) {
149 if (nextProblem.getSourceStart() < nextTask.getSourceStart()) {
150 currentProblem = nextProblem;
153 currentProblem = nextTask;
157 currentProblem = nextProblem;
161 if (nextTask != null) {
162 currentProblem = nextTask;
166 allProblems[allProblemIndex++] = currentProblem;
171 public ClassFile[] getClassFiles() {
172 Enumeration files = compiledTypes.elements();
173 ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
175 while (files.hasMoreElements()){
176 classFiles[index++] = (ClassFile)files.nextElement();
182 * Answer the initial compilation unit corresponding to the present compilation result
184 public ICompilationUnit getCompilationUnit(){
185 return compilationUnit;
189 * Answer the initial file name
191 public char[] getFileName(){
196 * Answer the errors encountered during compilation.
198 public IProblem[] getErrors() {
200 IProblem[] reportedProblems = getProblems();
202 for (int i = 0; i < this.problemCount; i++) {
203 if (reportedProblems[i].isError()) errorCount++;
205 if (errorCount == this.problemCount) return reportedProblems;
206 IProblem[] errors = new IProblem[errorCount];
208 for (int i = 0; i < this.problemCount; i++) {
209 if (reportedProblems[i].isError()) errors[index++] = reportedProblems[i];
215 * Answer the problems (errors and warnings) encountered during compilation.
217 * This is not a compiler internal API - it has side-effects !
218 * It is intended to be used only once all problems have been detected,
219 * and makes sure the problems slot as the exact size of the number of
222 public IProblem[] getProblems() {
224 // Re-adjust the size of the problems if necessary.
225 if (problems != null) {
227 if (this.problemCount != problems.length) {
228 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
231 if (this.maxProblemPerUnit > 0 && this.problemCount > this.maxProblemPerUnit){
232 quickPrioritize(problems, 0, problemCount - 1);
233 this.problemCount = this.maxProblemPerUnit;
234 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
237 // Sort problems per source positions.
238 quickSort(problems, 0, problems.length-1);
244 * Answer the tasks (TO-DO, ...) encountered during compilation.
246 * This is not a compiler internal API - it has side-effects !
247 * It is intended to be used only once all problems have been detected,
248 * and makes sure the problems slot as the exact size of the number of
251 public IProblem[] getTasks() {
253 // Re-adjust the size of the tasks if necessary.
254 if (this.tasks != null) {
256 if (this.taskCount != this.tasks.length) {
257 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
259 quickSort(tasks, 0, tasks.length-1);
264 public boolean hasErrors() {
266 if (problems != null)
267 for (int i = 0; i < problemCount; i++) {
268 if (problems[i].isError())
274 public boolean hasProblems() {
276 return problemCount != 0;
279 public boolean hasSyntaxError(){
281 if (problems != null)
282 for (int i = 0; i < problemCount; i++) {
283 IProblem problem = problems[i];
284 if ((problem.getID() & IProblem.Syntax) != 0 && problem.isError())
290 public boolean hasTasks() {
291 return this.taskCount != 0;
294 public boolean hasWarnings() {
296 if (problems != null)
297 for (int i = 0; i < problemCount; i++) {
298 if (problems[i].isWarning())
304 private static void quickSort(IProblem[] list, int left, int right) {
306 if (left >= right) return;
308 // sort the problems by their source start position... starting with 0
309 int original_left = left;
310 int original_right = right;
311 int mid = list[(left + right) / 2].getSourceStart();
313 while (list[left].getSourceStart() < mid)
315 while (mid < list[right].getSourceStart())
318 IProblem tmp = list[left];
319 list[left] = list[right];
324 } while (left <= right);
325 if (original_left < right)
326 quickSort(list, original_left, right);
327 if (left < original_right)
328 quickSort(list, left, original_right);
331 private void quickPrioritize(IProblem[] list, int left, int right) {
333 if (left >= right) return;
335 // sort the problems by their priority... starting with the highest priority
336 int original_left = left;
337 int original_right = right;
338 int mid = computePriority(list[(left + right) / 2]);
340 while (computePriority(list[right]) < mid)
342 while (mid < computePriority(list[left]))
345 IProblem tmp = list[left];
346 list[left] = list[right];
351 } while (left <= right);
352 if (original_left < right)
353 quickPrioritize(list, original_left, right);
354 if (left < original_right)
355 quickPrioritize(list, left, original_right);
359 * For now, remember the compiled type using its compound name.
361 public void record(char[] typeName, ClassFile classFile) {
363 SourceTypeBinding sourceType = classFile.referenceBinding;
364 if (!sourceType.isLocalType() && sourceType.isHierarchyInconsistent()) {
365 this.hasInconsistentToplevelHierarchies = true;
367 compiledTypes.put(typeName, classFile);
370 public void record(IProblem newProblem, ReferenceContext referenceContext) {
372 if (newProblem.getID() == IProblem.Task) {
373 recordTask(newProblem);
376 if (problemCount == 0) {
377 problems = new IProblem[5];
378 } else if (problemCount == problems.length) {
379 System.arraycopy(problems, 0, (problems = new IProblem[problemCount * 2]), 0, problemCount);
381 problems[problemCount++] = newProblem;
382 if (referenceContext != null){
383 if (problemsMap == null) problemsMap = new Hashtable(5);
384 if (firstErrorsMap == null) firstErrorsMap = new Hashtable(5);
385 if (newProblem.isError() && !referenceContext.hasErrors()) firstErrorsMap.put(newProblem, newProblem);
386 problemsMap.put(newProblem, referenceContext);
390 private void recordTask(IProblem newProblem) {
391 if (this.taskCount == 0) {
392 this.tasks = new IProblem[5];
393 } else if (this.taskCount == this.tasks.length) {
394 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount * 2]), 0, this.taskCount);
396 this.tasks[this.taskCount++] = newProblem;
399 public CompilationResult tagAsAccepted(){
401 this.hasBeenAccepted = true;
402 this.problemsMap = null; // flush
406 public String toString(){
408 StringBuffer buffer = new StringBuffer();
409 if (this.fileName != null){
410 buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
412 if (this.compiledTypes != null){
413 buffer.append("COMPILED type(s) \n"); //$NON-NLS-1$
414 Enumeration typeNames = this.compiledTypes.keys();
415 while (typeNames.hasMoreElements()) {
416 char[] typeName = (char[]) typeNames.nextElement();
417 buffer.append("\t - ").append(typeName).append('\n'); //$NON-NLS-1$
421 buffer.append("No COMPILED type\n"); //$NON-NLS-1$
423 if (problems != null){
424 buffer.append(this.problemCount).append(" PROBLEM(s) detected \n"); //$NON-NLS-1$//$NON-NLS-2$
425 for (int i = 0; i < this.problemCount; i++){
426 buffer.append("\t - ").append(this.problems[i]).append('\n'); //$NON-NLS-1$
429 buffer.append("No PROBLEM\n"); //$NON-NLS-1$
431 return buffer.toString();