KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > State


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.builder;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.*;
15
16 import org.eclipse.jdt.core.compiler.CharOperation;
17 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
18 import org.eclipse.jdt.internal.compiler.env.AccessRule;
19 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
20 import org.eclipse.jdt.internal.core.ClasspathAccessRule;
21 import org.eclipse.jdt.internal.core.JavaModelManager;
22
23 import java.io.*;
24 import java.util.*;
25
26 public class State {
27 // NOTE: this state cannot contain types that are not defined in this project
28

29 String JavaDoc javaProjectName;
30 ClasspathMultiDirectory[] sourceLocations;
31 ClasspathLocation[] binaryLocations;
32 // keyed by the project relative path of the type (ie. "src1/p1/p2/A.java"), value is a ReferenceCollection or an AdditionalTypeCollection
33
SimpleLookupTable references;
34 // keyed by qualified type name "p1/p2/A", value is the project relative path which defines this type "src1/p1/p2/A.java"
35
public SimpleLookupTable typeLocators;
36
37 int buildNumber;
38 long lastStructuralBuildTime;
39 SimpleLookupTable structuralBuildTimes;
40
41 private String JavaDoc[] knownPackageNames; // of the form "p1/p2"
42

43 private long previousStructuralBuildTime;
44 private StringSet structurallyChangedTypes;
45 public static int MaxStructurallyChangedTypes = 100; // keep track of ? structurally changed types, otherwise consider all to be changed
46

47 public static final byte VERSION = 0x0015; // changed access rule presentation
48

49 static final byte SOURCE_FOLDER = 1;
50 static final byte BINARY_FOLDER = 2;
51 static final byte EXTERNAL_JAR = 3;
52 static final byte INTERNAL_JAR = 4;
53
54 State() {
55     // constructor with no argument
56
}
57
58 protected State(JavaBuilder javaBuilder) {
59     this.knownPackageNames = null;
60     this.previousStructuralBuildTime = -1;
61     this.structurallyChangedTypes = null;
62     this.javaProjectName = javaBuilder.currentProject.getName();
63     this.sourceLocations = javaBuilder.nameEnvironment.sourceLocations;
64     this.binaryLocations = javaBuilder.nameEnvironment.binaryLocations;
65     this.references = new SimpleLookupTable(7);
66     this.typeLocators = new SimpleLookupTable(7);
67
68     this.buildNumber = 0; // indicates a full build
69
this.lastStructuralBuildTime = computeStructuralBuildTime(javaBuilder.lastState == null ? 0 : javaBuilder.lastState.lastStructuralBuildTime);
70     this.structuralBuildTimes = new SimpleLookupTable(3);
71 }
72
73 long computeStructuralBuildTime(long previousTime) {
74     long newTime = System.currentTimeMillis();
75     if (newTime <= previousTime)
76         newTime = previousTime + 1;
77     return newTime;
78 }
79
80 void copyFrom(State lastState) {
81     this.knownPackageNames = null;
82     this.previousStructuralBuildTime = lastState.previousStructuralBuildTime;
83     this.structurallyChangedTypes = lastState.structurallyChangedTypes;
84     this.buildNumber = lastState.buildNumber + 1;
85     this.lastStructuralBuildTime = lastState.lastStructuralBuildTime;
86     this.structuralBuildTimes = lastState.structuralBuildTimes;
87
88     try {
89         this.references = (SimpleLookupTable) lastState.references.clone();
90         this.typeLocators = (SimpleLookupTable) lastState.typeLocators.clone();
91     } catch (CloneNotSupportedException JavaDoc e) {
92         this.references = new SimpleLookupTable(lastState.references.elementSize);
93         Object JavaDoc[] keyTable = lastState.references.keyTable;
94         Object JavaDoc[] valueTable = lastState.references.valueTable;
95         for (int i = 0, l = keyTable.length; i < l; i++)
96             if (keyTable[i] != null)
97                 this.references.put(keyTable[i], valueTable[i]);
98
99         this.typeLocators = new SimpleLookupTable(lastState.typeLocators.elementSize);
100         keyTable = lastState.typeLocators.keyTable;
101         valueTable = lastState.typeLocators.valueTable;
102         for (int i = 0, l = keyTable.length; i < l; i++)
103             if (keyTable[i] != null)
104                 this.typeLocators.put(keyTable[i], valueTable[i]);
105     }
106 }
107 public char[][] getDefinedTypeNamesFor(String JavaDoc typeLocator) {
108     Object JavaDoc c = references.get(typeLocator);
109     if (c instanceof AdditionalTypeCollection)
110         return ((AdditionalTypeCollection) c).definedTypeNames;
111     return null; // means only one type is defined with the same name as the file... saves space
112
}
113
114 StringSet getStructurallyChangedTypes(State prereqState) {
115     if (prereqState != null && prereqState.previousStructuralBuildTime > 0) {
116         Object JavaDoc o = structuralBuildTimes.get(prereqState.javaProjectName);
117         long previous = o == null ? 0 : ((Long JavaDoc) o).longValue();
118         if (previous == prereqState.previousStructuralBuildTime)
119             return prereqState.structurallyChangedTypes;
120     }
121     return null;
122 }
123
124 boolean isDuplicateLocator(String JavaDoc qualifiedTypeName, String JavaDoc typeLocator) {
125     String JavaDoc existing = (String JavaDoc) typeLocators.get(qualifiedTypeName);
126     return existing != null && !existing.equals(typeLocator);
127 }
128
129 boolean isKnownPackage(String JavaDoc qualifiedPackageName) {
130     if (knownPackageNames == null) {
131         ArrayList names = new ArrayList(typeLocators.elementSize);
132         Object JavaDoc[] keyTable = typeLocators.keyTable;
133         for (int i = 0, l = keyTable.length; i < l; i++) {
134             if (keyTable[i] != null) {
135                 String JavaDoc packageName = (String JavaDoc) keyTable[i]; // is a type name of the form p1/p2/A
136
int last = packageName.lastIndexOf('/');
137                 packageName = last == -1 ? null : packageName.substring(0, last);
138                 while (packageName != null && !names.contains(packageName)) {
139                     names.add(packageName);
140                     last = packageName.lastIndexOf('/');
141                     packageName = last == -1 ? null : packageName.substring(0, last);
142                 }
143             }
144         }
145         knownPackageNames = new String JavaDoc[names.size()];
146         names.toArray(knownPackageNames);
147     }
148     for (int i = 0, l = knownPackageNames.length; i < l; i++)
149         if (knownPackageNames[i].equals(qualifiedPackageName))
150             return true;
151     return false;
152 }
153
154 boolean isKnownType(String JavaDoc qualifiedTypeName) {
155     return typeLocators.containsKey(qualifiedTypeName);
156 }
157
158 void record(String JavaDoc typeLocator, char[][][] qualifiedRefs, char[][] simpleRefs, char[] mainTypeName, ArrayList typeNames) {
159     if (typeNames.size() == 1 && CharOperation.equals(mainTypeName, (char[]) typeNames.get(0))) {
160         references.put(typeLocator, new ReferenceCollection(qualifiedRefs, simpleRefs));
161     } else {
162         char[][] definedTypeNames = new char[typeNames.size()][]; // can be empty when no types are defined
163
typeNames.toArray(definedTypeNames);
164         references.put(typeLocator, new AdditionalTypeCollection(definedTypeNames, qualifiedRefs, simpleRefs));
165     }
166 }
167
168 void recordLocatorForType(String JavaDoc qualifiedTypeName, String JavaDoc typeLocator) {
169     this.knownPackageNames = null;
170     // in the common case, the qualifiedTypeName is a substring of the typeLocator so share the char[] by using String.substring()
171
int start = typeLocator.indexOf(qualifiedTypeName, 0);
172     if (start > 0)
173         qualifiedTypeName = typeLocator.substring(start, start + qualifiedTypeName.length());
174     typeLocators.put(qualifiedTypeName, typeLocator);
175 }
176
177 void recordStructuralDependency(IProject prereqProject, State prereqState) {
178     if (prereqState != null)
179         if (prereqState.lastStructuralBuildTime > 0) // can skip if 0 (full build) since its assumed to be 0 if unknown
180
structuralBuildTimes.put(prereqProject.getName(), new Long JavaDoc(prereqState.lastStructuralBuildTime));
181 }
182
183 void removeLocator(String JavaDoc typeLocatorToRemove) {
184     this.knownPackageNames = null;
185     references.removeKey(typeLocatorToRemove);
186     typeLocators.removeValue(typeLocatorToRemove);
187 }
188
189 void removePackage(IResourceDelta sourceDelta) {
190     IResource resource = sourceDelta.getResource();
191     switch(resource.getType()) {
192         case IResource.FOLDER :
193             IResourceDelta[] children = sourceDelta.getAffectedChildren();
194             for (int i = 0, l = children.length; i < l; i++)
195                 removePackage(children[i]);
196             return;
197         case IResource.FILE :
198             IPath typeLocatorPath = resource.getProjectRelativePath();
199             if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(typeLocatorPath.lastSegment()))
200                 removeLocator(typeLocatorPath.toString());
201     }
202 }
203
204 void removeQualifiedTypeName(String JavaDoc qualifiedTypeNameToRemove) {
205     this.knownPackageNames = null;
206     typeLocators.removeKey(qualifiedTypeNameToRemove);
207 }
208
209 static State read(IProject project, DataInputStream in) throws IOException {
210     if (JavaBuilder.DEBUG)
211         System.out.println("About to read state " + project.getName()); //$NON-NLS-1$
212
if (VERSION != in.readByte()) {
213         if (JavaBuilder.DEBUG)
214             System.out.println("Found non-compatible state version... answered null for " + project.getName()); //$NON-NLS-1$
215
return null;
216     }
217
218     State newState = new State();
219     newState.javaProjectName = in.readUTF();
220     if (!project.getName().equals(newState.javaProjectName)) {
221         if (JavaBuilder.DEBUG)
222             System.out.println("Project's name does not match... answered null"); //$NON-NLS-1$
223
return null;
224     }
225     newState.buildNumber = in.readInt();
226     newState.lastStructuralBuildTime = in.readLong();
227
228     int length = in.readInt();
229     newState.sourceLocations = new ClasspathMultiDirectory[length];
230     for (int i = 0; i < length; i++) {
231         IContainer sourceFolder = project, outputFolder = project;
232         String JavaDoc folderName;
233         if ((folderName = in.readUTF()).length() > 0) sourceFolder = project.getFolder(folderName);
234         if ((folderName = in.readUTF()).length() > 0) outputFolder = project.getFolder(folderName);
235         ClasspathMultiDirectory md =
236             (ClasspathMultiDirectory) ClasspathLocation.forSourceFolder(sourceFolder, outputFolder, readNames(in), readNames(in));
237         if (in.readBoolean())
238             md.hasIndependentOutputFolder = true;
239         newState.sourceLocations[i] = md;
240     }
241
242     length = in.readInt();
243     newState.binaryLocations = new ClasspathLocation[length];
244     IWorkspaceRoot root = project.getWorkspace().getRoot();
245     for (int i = 0; i < length; i++) {
246         switch (in.readByte()) {
247             case SOURCE_FOLDER :
248                 newState.binaryLocations[i] = newState.sourceLocations[in.readInt()];
249                 break;
250             case BINARY_FOLDER :
251                 IPath path = new Path(in.readUTF());
252                 IContainer outputFolder = path.segmentCount() == 1
253                     ? (IContainer) root.getProject(path.toString())
254                     : (IContainer) root.getFolder(path);
255                 newState.binaryLocations[i] = ClasspathLocation.forBinaryFolder(outputFolder, in.readBoolean(), readRestriction(in));
256                 break;
257             case EXTERNAL_JAR :
258                 newState.binaryLocations[i] = ClasspathLocation.forLibrary(in.readUTF(), in.readLong(), readRestriction(in));
259                 break;
260             case INTERNAL_JAR :
261                 newState.binaryLocations[i] = ClasspathLocation.forLibrary(root.getFile(new Path(in.readUTF())), readRestriction(in));
262         }
263     }
264
265     newState.structuralBuildTimes = new SimpleLookupTable(length = in.readInt());
266     for (int i = 0; i < length; i++)
267         newState.structuralBuildTimes.put(in.readUTF(), new Long JavaDoc(in.readLong()));
268
269     String JavaDoc[] internedTypeLocators = new String JavaDoc[length = in.readInt()];
270     for (int i = 0; i < length; i++)
271         internedTypeLocators[i] = in.readUTF();
272
273     newState.typeLocators = new SimpleLookupTable(length = in.readInt());
274     for (int i = 0; i < length; i++)
275         newState.recordLocatorForType(in.readUTF(), internedTypeLocators[in.readInt()]);
276
277     char[][] internedSimpleNames = ReferenceCollection.internSimpleNames(readNames(in), false);
278     char[][][] internedQualifiedNames = new char[length = in.readInt()][][];
279     for (int i = 0; i < length; i++) {
280         int qLength = in.readInt();
281         char[][] qName = new char[qLength][];
282         for (int j = 0; j < qLength; j++)
283             qName[j] = internedSimpleNames[in.readInt()];
284         internedQualifiedNames[i] = qName;
285     }
286     internedQualifiedNames = ReferenceCollection.internQualifiedNames(internedQualifiedNames);
287
288     newState.references = new SimpleLookupTable(length = in.readInt());
289     for (int i = 0; i < length; i++) {
290         String JavaDoc typeLocator = internedTypeLocators[in.readInt()];
291         ReferenceCollection collection = null;
292         switch (in.readByte()) {
293             case 1 :
294                 char[][] additionalTypeNames = readNames(in);
295                 char[][][] qualifiedNames = new char[in.readInt()][][];
296                 for (int j = 0, m = qualifiedNames.length; j < m; j++)
297                     qualifiedNames[j] = internedQualifiedNames[in.readInt()];
298                 char[][] simpleNames = new char[in.readInt()][];
299                 for (int j = 0, m = simpleNames.length; j < m; j++)
300                     simpleNames[j] = internedSimpleNames[in.readInt()];
301                 collection = new AdditionalTypeCollection(additionalTypeNames, qualifiedNames, simpleNames);
302                 break;
303             case 2 :
304                 char[][][] qNames = new char[in.readInt()][][];
305                 for (int j = 0, m = qNames.length; j < m; j++)
306                     qNames[j] = internedQualifiedNames[in.readInt()];
307                 char[][] sNames = new char[in.readInt()][];
308                 for (int j = 0, m = sNames.length; j < m; j++)
309                     sNames[j] = internedSimpleNames[in.readInt()];
310                 collection = new ReferenceCollection(qNames, sNames);
311         }
312         newState.references.put(typeLocator, collection);
313     }
314     if (JavaBuilder.DEBUG)
315         System.out.println("Successfully read state for " + newState.javaProjectName); //$NON-NLS-1$
316
return newState;
317 }
318
319 private static char[] readName(DataInputStream in) throws IOException {
320     int nLength = in.readInt();
321     char[] name = new char[nLength];
322     for (int j = 0; j < nLength; j++)
323         name[j] = in.readChar();
324     return name;
325 }
326
327 private static char[][] readNames(DataInputStream in) throws IOException {
328     int length = in.readInt();
329     char[][] names = new char[length][];
330     for (int i = 0; i < length; i++)
331         names[i] = readName(in);
332     return names;
333 }
334
335 private static AccessRuleSet readRestriction(DataInputStream in) throws IOException {
336     int length = in.readInt();
337     if (length == 0) return null; // no restriction specified
338
AccessRule[] accessRules = new AccessRule[length];
339     for (int i = 0; i < length; i++) {
340         char[] pattern = readName(in);
341         int problemId = in.readInt();
342         accessRules[i] = new ClasspathAccessRule(pattern, problemId);
343     }
344     JavaModelManager manager = JavaModelManager.getJavaModelManager();
345     String JavaDoc[] messageTemplates = new String JavaDoc[AccessRuleSet.MESSAGE_TEMPLATES_LENGTH];
346     for (int i = 0; i < AccessRuleSet.MESSAGE_TEMPLATES_LENGTH; i++) {
347         messageTemplates[i] = manager.intern(in.readUTF());
348     }
349     AccessRuleSet accessRuleSet = new AccessRuleSet(accessRules, messageTemplates);
350     return accessRuleSet;
351 }
352
353 void tagAsNoopBuild() {
354     this.buildNumber = -1; // tag the project since it has no source folders and can be skipped
355
}
356
357 boolean wasNoopBuild() {
358     return buildNumber == -1;
359 }
360
361 void tagAsStructurallyChanged() {
362     this.previousStructuralBuildTime = this.lastStructuralBuildTime;
363     this.structurallyChangedTypes = new StringSet(7);
364     this.lastStructuralBuildTime = computeStructuralBuildTime(this.previousStructuralBuildTime);
365 }
366
367 boolean wasStructurallyChanged(IProject prereqProject, State prereqState) {
368     if (prereqState != null) {
369         Object JavaDoc o = structuralBuildTimes.get(prereqProject.getName());
370         long previous = o == null ? 0 : ((Long JavaDoc) o).longValue();
371         if (previous == prereqState.lastStructuralBuildTime) return false;
372     }
373     return true;
374 }
375
376 void wasStructurallyChanged(String JavaDoc typeName) {
377     if (this.structurallyChangedTypes != null) {
378         if (this.structurallyChangedTypes.elementSize > MaxStructurallyChangedTypes)
379             this.structurallyChangedTypes = null; // too many to keep track of
380
else
381             this.structurallyChangedTypes.add(typeName);
382     }
383 }
384
385 void write(DataOutputStream out) throws IOException {
386     int length;
387     Object JavaDoc[] keyTable;
388     Object JavaDoc[] valueTable;
389
390 /*
391  * byte VERSION
392  * String project name
393  * int build number
394  * int last structural build number
395 */

396     out.writeByte(VERSION);
397     out.writeUTF(javaProjectName);
398     out.writeInt(buildNumber);
399     out.writeLong(lastStructuralBuildTime);
400
401 /*
402  * ClasspathMultiDirectory[]
403  * int id
404  * String path(s)
405 */

406     out.writeInt(length = sourceLocations.length);
407     for (int i = 0; i < length; i++) {
408         ClasspathMultiDirectory md = sourceLocations[i];
409         out.writeUTF(md.sourceFolder.getProjectRelativePath().toString());
410         out.writeUTF(md.binaryFolder.getProjectRelativePath().toString());
411         writeNames(md.inclusionPatterns, out);
412         writeNames(md.exclusionPatterns, out);
413         out.writeBoolean(md.hasIndependentOutputFolder);
414     }
415
416 /*
417  * ClasspathLocation[]
418  * int id
419  * String path(s)
420 */

421     out.writeInt(length = binaryLocations.length);
422     next : for (int i = 0; i < length; i++) {
423         ClasspathLocation c = binaryLocations[i];
424         if (c instanceof ClasspathMultiDirectory) {
425             out.writeByte(SOURCE_FOLDER);
426             for (int j = 0, m = sourceLocations.length; j < m; j++) {
427                 if (sourceLocations[j] == c) {
428                     out.writeInt(j);
429                     continue next;
430                 }
431             }
432         } else if (c instanceof ClasspathDirectory) {
433             out.writeByte(BINARY_FOLDER);
434             ClasspathDirectory cd = (ClasspathDirectory) c;
435             out.writeUTF(cd.binaryFolder.getFullPath().toString());
436             out.writeBoolean(cd.isOutputFolder);
437             writeRestriction(cd.accessRuleSet, out);
438         } else {
439             ClasspathJar jar = (ClasspathJar) c;
440             if (jar.resource == null) {
441                 out.writeByte(EXTERNAL_JAR);
442                 out.writeUTF(jar.zipFilename);
443                 out.writeLong(jar.lastModified());
444             } else {
445                 out.writeByte(INTERNAL_JAR);
446                 out.writeUTF(jar.resource.getFullPath().toString());
447             }
448             writeRestriction(jar.accessRuleSet, out);
449         }
450     }
451
452 /*
453  * Structural build numbers table
454  * String prereq project name
455  * int last structural build number
456 */

457     out.writeInt(length = structuralBuildTimes.elementSize);
458     if (length > 0) {
459         keyTable = structuralBuildTimes.keyTable;
460         valueTable = structuralBuildTimes.valueTable;
461         for (int i = 0, l = keyTable.length; i < l; i++) {
462             if (keyTable[i] != null) {
463                 length--;
464                 out.writeUTF((String JavaDoc) keyTable[i]);
465                 out.writeLong(((Long JavaDoc) valueTable[i]).longValue());
466             }
467         }
468         if (JavaBuilder.DEBUG && length != 0)
469             System.out.println("structuralBuildNumbers table is inconsistent"); //$NON-NLS-1$
470
}
471
472 /*
473  * String[] Interned type locators
474  */

475     out.writeInt(length = references.elementSize);
476     SimpleLookupTable internedTypeLocators = new SimpleLookupTable(length);
477     if (length > 0) {
478         keyTable = references.keyTable;
479         for (int i = 0, l = keyTable.length; i < l; i++) {
480             if (keyTable[i] != null) {
481                 length--;
482                 String JavaDoc key = (String JavaDoc) keyTable[i];
483                 out.writeUTF(key);
484                 internedTypeLocators.put(key, new Integer JavaDoc(internedTypeLocators.elementSize));
485             }
486         }
487         if (JavaBuilder.DEBUG && length != 0)
488             System.out.println("references table is inconsistent"); //$NON-NLS-1$
489
}
490
491 /*
492  * Type locators table
493  * String type name
494  * int interned locator id
495  */

496     out.writeInt(length = typeLocators.elementSize);
497     if (length > 0) {
498         keyTable = typeLocators.keyTable;
499         valueTable = typeLocators.valueTable;
500         for (int i = 0, l = keyTable.length; i < l; i++) {
501             if (keyTable[i] != null) {
502                 length--;
503                 out.writeUTF((String JavaDoc) keyTable[i]);
504                 Integer JavaDoc index = (Integer JavaDoc) internedTypeLocators.get(valueTable[i]);
505                 out.writeInt(index.intValue());
506             }
507         }
508         if (JavaBuilder.DEBUG && length != 0)
509             System.out.println("typeLocators table is inconsistent"); //$NON-NLS-1$
510
}
511
512 /*
513  * char[][][] Interned qualified names
514  * char[][] Interned simple names
515  */

516     SimpleLookupTable internedQualifiedNames = new SimpleLookupTable(31);
517     SimpleLookupTable internedSimpleNames = new SimpleLookupTable(31);
518     valueTable = references.valueTable;
519     for (int i = 0, l = valueTable.length; i < l; i++) {
520         if (valueTable[i] != null) {
521             ReferenceCollection collection = (ReferenceCollection) valueTable[i];
522             char[][][] qNames = collection.qualifiedNameReferences;
523             for (int j = 0, m = qNames.length; j < m; j++) {
524                 char[][] qName = qNames[j];
525                 if (!internedQualifiedNames.containsKey(qName)) { // remember the names have been interned
526
internedQualifiedNames.put(qName, new Integer JavaDoc(internedQualifiedNames.elementSize));
527                     for (int k = 0, n = qName.length; k < n; k++) {
528                         char[] sName = qName[k];
529                         if (!internedSimpleNames.containsKey(sName)) // remember the names have been interned
530
internedSimpleNames.put(sName, new Integer JavaDoc(internedSimpleNames.elementSize));
531                     }
532                 }
533             }
534             char[][] sNames = collection.simpleNameReferences;
535             for (int j = 0, m = sNames.length; j < m; j++) {
536                 char[] sName = sNames[j];
537                 if (!internedSimpleNames.containsKey(sName)) // remember the names have been interned
538
internedSimpleNames.put(sName, new Integer JavaDoc(internedSimpleNames.elementSize));
539             }
540         }
541     }
542     char[][] internedArray = new char[internedSimpleNames.elementSize][];
543     Object JavaDoc[] simpleNames = internedSimpleNames.keyTable;
544     Object JavaDoc[] positions = internedSimpleNames.valueTable;
545     for (int i = positions.length; --i >= 0; ) {
546         if (positions[i] != null) {
547             int index = ((Integer JavaDoc) positions[i]).intValue();
548             internedArray[index] = (char[]) simpleNames[i];
549         }
550     }
551     writeNames(internedArray, out);
552     // now write the interned qualified names as arrays of interned simple names
553
char[][][] internedQArray = new char[internedQualifiedNames.elementSize][][];
554     Object JavaDoc[] qualifiedNames = internedQualifiedNames.keyTable;
555     positions = internedQualifiedNames.valueTable;
556     for (int i = positions.length; --i >= 0; ) {
557         if (positions[i] != null) {
558             int index = ((Integer JavaDoc) positions[i]).intValue();
559             internedQArray[index] = (char[][]) qualifiedNames[i];
560         }
561     }
562     out.writeInt(length = internedQArray.length);
563     for (int i = 0; i < length; i++) {
564         char[][] qName = internedQArray[i];
565         int qLength = qName.length;
566         out.writeInt(qLength);
567         for (int j = 0; j < qLength; j++) {
568             Integer JavaDoc index = (Integer JavaDoc) internedSimpleNames.get(qName[j]);
569             out.writeInt(index.intValue());
570         }
571     }
572
573 /*
574  * References table
575  * int interned locator id
576  * ReferenceCollection
577 */

578     out.writeInt(length = references.elementSize);
579     if (length > 0) {
580         keyTable = references.keyTable;
581         for (int i = 0, l = keyTable.length; i < l; i++) {
582             if (keyTable[i] != null) {
583                 length--;
584                 Integer JavaDoc index = (Integer JavaDoc) internedTypeLocators.get(keyTable[i]);
585                 out.writeInt(index.intValue());
586                 ReferenceCollection collection = (ReferenceCollection) valueTable[i];
587                 if (collection instanceof AdditionalTypeCollection) {
588                     out.writeByte(1);
589                     AdditionalTypeCollection atc = (AdditionalTypeCollection) collection;
590                     writeNames(atc.definedTypeNames, out);
591                 } else {
592                     out.writeByte(2);
593                 }
594                 char[][][] qNames = collection.qualifiedNameReferences;
595                 int qLength = qNames.length;
596                 out.writeInt(qLength);
597                 for (int j = 0; j < qLength; j++) {
598                     index = (Integer JavaDoc) internedQualifiedNames.get(qNames[j]);
599                     out.writeInt(index.intValue());
600                 }
601                 char[][] sNames = collection.simpleNameReferences;
602                 int sLength = sNames.length;
603                 out.writeInt(sLength);
604                 for (int j = 0; j < sLength; j++) {
605                     index = (Integer JavaDoc) internedSimpleNames.get(sNames[j]);
606                     out.writeInt(index.intValue());
607                 }
608             }
609         }
610         if (JavaBuilder.DEBUG && length != 0)
611             System.out.println("references table is inconsistent"); //$NON-NLS-1$
612
}
613 }
614
615 private void writeName(char[] name, DataOutputStream out) throws IOException {
616     int nLength = name.length;
617     out.writeInt(nLength);
618     for (int j = 0; j < nLength; j++)
619         out.writeChar(name[j]);
620 }
621
622 private void writeNames(char[][] names, DataOutputStream out) throws IOException {
623     int length = names == null ? 0 : names.length;
624     out.writeInt(length);
625     for (int i = 0; i < length; i++)
626         writeName(names[i], out);
627 }
628
629 private void writeRestriction(AccessRuleSet accessRuleSet, DataOutputStream out) throws IOException {
630     if (accessRuleSet == null) {
631         out.writeInt(0);
632     } else {
633         AccessRule[] accessRules = accessRuleSet.getAccessRules();
634         int length = accessRules.length;
635         out.writeInt(length);
636         if (length != 0) {
637             for (int i = 0; i < length; i++) {
638                 AccessRule accessRule = accessRules[i];
639                 writeName(accessRule.pattern, out);
640                 out.writeInt(accessRule.problemId);
641             }
642             for (int i = 0; i < AccessRuleSet.MESSAGE_TEMPLATES_LENGTH; i++)
643                 out.writeUTF(accessRuleSet.messageTemplates[i]);
644         }
645     }
646 }
647
648 /**
649  * Returns a string representation of the receiver.
650  */

651 public String JavaDoc toString() {
652     return "State for " + javaProjectName //$NON-NLS-1$
653
+ " (#" + buildNumber //$NON-NLS-1$
654
+ " @ " + new Date(lastStructuralBuildTime) //$NON-NLS-1$
655
+ ")"; //$NON-NLS-1$
656
}
657
658 /* Debug helper
659 void dump() {
660     System.out.println("State for " + javaProjectName + " (" + buildNumber + " @ " + new Date(lastStructuralBuildTime) + ")");
661     System.out.println("\tClass path source locations:");
662     for (int i = 0, l = sourceLocations.length; i < l; i++)
663         System.out.println("\t\t" + sourceLocations[i]);
664     System.out.println("\tClass path binary locations:");
665     for (int i = 0, l = binaryLocations.length; i < l; i++)
666         System.out.println("\t\t" + binaryLocations[i]);
667
668     System.out.print("\tStructural build numbers table:");
669     if (structuralBuildTimes.elementSize == 0) {
670         System.out.print(" <empty>");
671     } else {
672         Object[] keyTable = structuralBuildTimes.keyTable;
673         Object[] valueTable = structuralBuildTimes.valueTable;
674         for (int i = 0, l = keyTable.length; i < l; i++)
675             if (keyTable[i] != null)
676                 System.out.print("\n\t\t" + keyTable[i].toString() + " -> " + valueTable[i].toString());
677     }
678
679     System.out.print("\tType locators table:");
680     if (typeLocators.elementSize == 0) {
681         System.out.print(" <empty>");
682     } else {
683         Object[] keyTable = typeLocators.keyTable;
684         Object[] valueTable = typeLocators.valueTable;
685         for (int i = 0, l = keyTable.length; i < l; i++)
686             if (keyTable[i] != null)
687                 System.out.print("\n\t\t" + keyTable[i].toString() + " -> " + valueTable[i].toString());
688     }
689
690     System.out.print("\n\tReferences table:");
691     if (references.elementSize == 0) {
692         System.out.print(" <empty>");
693     } else {
694         Object[] keyTable = references.keyTable;
695         Object[] valueTable = references.valueTable;
696         for (int i = 0, l = keyTable.length; i < l; i++) {
697             if (keyTable[i] != null) {
698                 System.out.print("\n\t\t" + keyTable[i].toString());
699                 ReferenceCollection c = (ReferenceCollection) valueTable[i];
700                 char[][][] qRefs = c.qualifiedNameReferences;
701                 System.out.print("\n\t\t\tqualified:");
702                 if (qRefs.length == 0)
703                     System.out.print(" <empty>");
704                 else for (int j = 0, m = qRefs.length; j < m; j++)
705                         System.out.print(" '" + CharOperation.toString(qRefs[j]) + "'");
706                 char[][] sRefs = c.simpleNameReferences;
707                 System.out.print("\n\t\t\tsimple:");
708                 if (sRefs.length == 0)
709                     System.out.print(" <empty>");
710                 else for (int j = 0, m = sRefs.length; j < m; j++)
711                         System.out.print(" " + new String(sRefs[j]));
712                 if (c instanceof AdditionalTypeCollection) {
713                     char[][] names = ((AdditionalTypeCollection) c).definedTypeNames;
714                     System.out.print("\n\t\t\tadditional type names:");
715                     for (int j = 0, m = names.length; j < m; j++)
716                         System.out.print(" " + new String(names[j]));
717                 }
718             }
719         }
720     }
721     System.out.print("\n\n");
722 }
723 */

724 }
725
Popular Tags