added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / CompilationResult.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler;
12
13 /**
14  * A compilation result consists of all information returned by the compiler for 
15  * a single compiled compilation source unit.  This includes:
16  * <ul>
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
21  * </ul>
22  *
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.
25  *
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.
32  */
33
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;
39
40 import java.util.*;
41
42 public class CompilationResult {
43         
44         public IProblem problems[];
45         public IProblem tasks[];
46         public int problemCount;
47         public int taskCount;
48         public ICompilationUnit compilationUnit;
49         private Map problemsMap;
50         private Map firstErrorsMap;
51         private int maxProblemPerUnit;
52         public char[][][] qualifiedReferences;
53         public char[][] simpleNameReferences;
54
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
61         
62         public CompilationResult(
63                 char[] fileName,
64                 int unitIndex, 
65                 int totalUnitsKnown,
66                 int maxProblemPerUnit){
67         
68                 this.fileName = fileName;
69                 this.unitIndex = unitIndex;
70                 this.totalUnitsKnown = totalUnitsKnown;
71                 this.maxProblemPerUnit = maxProblemPerUnit;
72         }
73         
74         public CompilationResult(
75                 ICompilationUnit compilationUnit,
76                 int unitIndex, 
77                 int totalUnitsKnown,
78                 int maxProblemPerUnit){
79         
80                 this.fileName = compilationUnit.getFileName();
81                 this.compilationUnit = compilationUnit;
82                 this.unitIndex = unitIndex;
83                 this.totalUnitsKnown = totalUnitsKnown;
84                 this.maxProblemPerUnit = maxProblemPerUnit;
85         }
86
87         private int computePriority(IProblem problem){
88         
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;
93                 
94                 int priority = 10000 - problem.getSourceLineNumber(); // early problems first
95                 if (priority < 0) priority = 0;
96                 if (problem.isError()){
97                         priority += P_ERROR;
98                 }
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;
105                                 }
106                         } else {
107                         priority += P_OUTSIDE_METHOD;
108                         }
109                 } else {
110                         priority += P_OUTSIDE_METHOD;
111                 }
112                 if (firstErrorsMap.containsKey(problem)){
113                         priority += P_FIRST_ERROR;
114                 }
115                 return priority;
116         }
117
118         
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) {
125                         return onlyProblems;
126                 }
127                 if (onlyProblemCount == 0) {
128                         return onlyTasks;
129                 }
130
131                 int totalNumberOfProblem = onlyProblemCount + onlyTaskCount;
132                 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
133                 int allProblemIndex = 0;
134                 int taskIndex = 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];
141                         }
142                         if (problemIndex < onlyProblemCount) {
143                                 nextProblem = onlyProblems[problemIndex];
144                         }
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;
151                                                 problemIndex++;
152                                         } else {
153                                                 currentProblem = nextTask;
154                                                 taskIndex++;
155                                         }
156                                 } else {
157                                         currentProblem = nextProblem;
158                                         problemIndex++;
159                                 }
160                         } else {
161                                 if (nextTask != null) {
162                                         currentProblem = nextTask;
163                                         taskIndex++;
164                                 }
165                         }
166                         allProblems[allProblemIndex++] = currentProblem;
167                 }
168                 return allProblems;
169         }
170         
171         public ClassFile[] getClassFiles() {
172                 Enumeration files = compiledTypes.elements();
173                 ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
174                 int index = 0;
175                 while (files.hasMoreElements()){
176                         classFiles[index++] = (ClassFile)files.nextElement();
177                 }
178                 return classFiles;      
179         }
180
181         /**
182          * Answer the initial compilation unit corresponding to the present compilation result
183          */
184         public ICompilationUnit getCompilationUnit(){
185                 return compilationUnit;
186         }
187
188         /**
189          * Answer the initial file name
190          */
191         public char[] getFileName(){
192                 return fileName;
193         }
194         
195         /**
196          * Answer the errors encountered during compilation.
197          */
198         public IProblem[] getErrors() {
199         
200                 IProblem[] reportedProblems = getProblems();
201                 int errorCount = 0;
202                 for (int i = 0; i < this.problemCount; i++) {
203                         if (reportedProblems[i].isError()) errorCount++;
204                 }
205                 if (errorCount == this.problemCount) return reportedProblems;
206                 IProblem[] errors = new IProblem[errorCount];
207                 int index = 0;
208                 for (int i = 0; i < this.problemCount; i++) {
209                         if (reportedProblems[i].isError()) errors[index++] = reportedProblems[i];
210                 }
211                 return errors;
212         }
213         
214         /**
215          * Answer the problems (errors and warnings) encountered during compilation.
216          *
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
220          * problems.
221          */
222         public IProblem[] getProblems() {
223                 
224                 // Re-adjust the size of the problems if necessary.
225                 if (problems != null) {
226         
227                         if (this.problemCount != problems.length) {
228                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
229                         }
230         
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);
235                         }
236         
237                         // Sort problems per source positions.
238                         quickSort(problems, 0, problems.length-1);
239                 }
240                 return problems;
241         }
242
243         /**
244          * Answer the tasks (TO-DO, ...) encountered during compilation.
245          *
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
249          * problems.
250          */
251         public IProblem[] getTasks() {
252                 
253                 // Re-adjust the size of the tasks if necessary.
254                 if (this.tasks != null) {
255         
256                         if (this.taskCount != this.tasks.length) {
257                                 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
258                         }
259                         quickSort(tasks, 0, tasks.length-1);
260                 }
261                 return this.tasks;
262         }
263         
264         public boolean hasErrors() {
265
266                 if (problems != null)
267                         for (int i = 0; i < problemCount; i++) {
268                                 if (problems[i].isError())
269                                         return true;
270                         }
271                 return false;
272         }
273
274         public boolean hasProblems() {
275
276                 return problemCount != 0;
277         }
278
279         public boolean hasSyntaxError(){
280
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())
285                                         return true;
286                         }
287                 return false;
288         }
289
290         public boolean hasTasks() {
291                 return this.taskCount != 0;
292         }
293         
294         public boolean hasWarnings() {
295
296                 if (problems != null)
297                         for (int i = 0; i < problemCount; i++) {
298                                 if (problems[i].isWarning())
299                                         return true;
300                         }
301                 return false;
302         }
303         
304         private static void quickSort(IProblem[] list, int left, int right) {
305
306                 if (left >= right) return;
307         
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();
312                 do {
313                         while (list[left].getSourceStart() < mid)
314                                 left++;
315                         while (mid < list[right].getSourceStart())
316                                 right--;
317                         if (left <= right) {
318                                 IProblem tmp = list[left];
319                                 list[left] = list[right];
320                                 list[right] = tmp;
321                                 left++;
322                                 right--;
323                         }
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);
329         }
330         
331         private void quickPrioritize(IProblem[] list, int left, int right) {
332                 
333                 if (left >= right) return;
334         
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]);
339                 do {
340                         while (computePriority(list[right]) < mid)
341                                 right--;
342                         while (mid < computePriority(list[left]))
343                                 left++;
344                         if (left <= right) {
345                                 IProblem tmp = list[left];
346                                 list[left] = list[right];
347                                 list[right] = tmp;
348                                 left++;
349                                 right--;
350                         }
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);
356         }
357         
358         /**
359          * For now, remember the compiled type using its compound name.
360          */
361         public void record(char[] typeName, ClassFile classFile) {
362
363             SourceTypeBinding sourceType = classFile.referenceBinding;
364             if (!sourceType.isLocalType() && sourceType.isHierarchyInconsistent()) {
365                 this.hasInconsistentToplevelHierarchies = true;
366             }
367                 compiledTypes.put(typeName, classFile);
368         }
369
370         public void record(IProblem newProblem, ReferenceContext referenceContext) {
371
372                 if (newProblem.getID() == IProblem.Task) {
373                         recordTask(newProblem);
374                         return;
375                 }
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);
380                 }
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);
387                 }
388         }
389
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);
395                 }
396                 this.tasks[this.taskCount++] = newProblem;
397         }
398         
399         public CompilationResult tagAsAccepted(){
400
401                 this.hasBeenAccepted = true;
402                 this.problemsMap = null; // flush
403                 return this;
404         }
405         
406         public String toString(){
407
408                 StringBuffer buffer = new StringBuffer();
409                 if (this.fileName != null){
410                         buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
411                 }
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$
418                                 
419                         }
420                 } else {
421                         buffer.append("No COMPILED type\n");  //$NON-NLS-1$
422                 }
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$
427                         }
428                 } else {
429                         buffer.append("No PROBLEM\n"); //$NON-NLS-1$
430                 } 
431                 return buffer.toString();
432         }
433 }