1 12 package org.eclipse.jdt.internal.core.builder; 13 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.runtime.*; 16 17 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 18 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; 19 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 20 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; 21 import org.eclipse.jdt.internal.compiler.util.SimpleSet; 22 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 23 import org.eclipse.jdt.internal.core.util.Util; 24 25 import java.io.*; 26 import java.util.*; 27 import java.util.zip.*; 28 29 public class ClasspathJar extends ClasspathLocation { 30 31 static class PackageCacheEntry { 32 long lastModified; 33 long fileSize; 34 SimpleSet packageSet; 35 36 PackageCacheEntry(long lastModified, long fileSize, SimpleSet packageSet) { 37 this.lastModified = lastModified; 38 this.fileSize = fileSize; 39 this.packageSet = packageSet; 40 } 41 } 42 43 static SimpleLookupTable PackageCache = new SimpleLookupTable(); 44 45 50 static SimpleSet findPackageSet(ClasspathJar jar) { 51 String zipFileName = jar.zipFilename; 52 long lastModified = jar.lastModified(); 53 long fileSize = new File(zipFileName).length(); 54 PackageCacheEntry cacheEntry = (PackageCacheEntry) PackageCache.get(zipFileName); 55 if (cacheEntry != null && cacheEntry.lastModified == lastModified && cacheEntry.fileSize == fileSize) 56 return cacheEntry.packageSet; 57 58 SimpleSet packageSet = new SimpleSet(41); 59 packageSet.add(""); nextEntry : for (Enumeration e = jar.zipFile.entries(); e.hasMoreElements(); ) { 61 String fileName = ((ZipEntry) e.nextElement()).getName(); 62 63 int last = fileName.lastIndexOf('/'); 65 while (last > 0) { 66 String packageName = fileName.substring(0, last); 68 if (packageSet.addIfNotIncluded(packageName) == null) 69 continue nextEntry; last = packageName.lastIndexOf('/'); 71 } 72 } 73 74 PackageCache.put(zipFileName, new PackageCacheEntry(lastModified, fileSize, packageSet)); 75 return packageSet; 76 } 77 78 79 String zipFilename; IFile resource; 81 ZipFile zipFile; 82 long lastModified; 83 boolean closeZipFileAtEnd; 84 SimpleSet knownPackageNames; 85 AccessRuleSet accessRuleSet; 86 87 ClasspathJar(IFile resource, AccessRuleSet accessRuleSet) { 88 this.resource = resource; 89 try { 90 java.net.URI location = resource.getLocationURI(); 91 if (location == null) { 92 this.zipFilename = ""; } else { 94 File localFile = Util.toLocalFile(location, null); 95 this.zipFilename = localFile.getPath(); 96 } 97 } catch (CoreException e) { 98 } 100 this.zipFile = null; 101 this.knownPackageNames = null; 102 this.accessRuleSet = accessRuleSet; 103 } 104 105 ClasspathJar(String zipFilename, long lastModified, AccessRuleSet accessRuleSet) { 106 this.zipFilename = zipFilename; 107 this.lastModified = lastModified; 108 this.zipFile = null; 109 this.knownPackageNames = null; 110 this.accessRuleSet = accessRuleSet; 111 } 112 113 public ClasspathJar(ZipFile zipFile, AccessRuleSet accessRuleSet) { 114 this.zipFilename = zipFile.getName(); 115 this.zipFile = zipFile; 116 this.closeZipFileAtEnd = false; 117 this.knownPackageNames = null; 118 this.accessRuleSet = accessRuleSet; 119 } 120 121 public void cleanup() { 122 if (this.zipFile != null && this.closeZipFileAtEnd) { 123 try { 124 this.zipFile.close(); 125 } catch(IOException e) { } 127 this.zipFile = null; 128 } 129 this.knownPackageNames = null; 130 } 131 132 public boolean equals(Object o) { 133 if (this == o) return true; 134 if (!(o instanceof ClasspathJar)) return false; 135 136 ClasspathJar jar = (ClasspathJar) o; 137 if (this.accessRuleSet != jar.accessRuleSet) 138 if (this.accessRuleSet == null || !this.accessRuleSet.equals(jar.accessRuleSet)) 139 return false; 140 return this.zipFilename.equals(jar.zipFilename) && this.lastModified() == jar.lastModified(); 141 } 142 143 public NameEnvironmentAnswer findClass(String binaryFileName, String qualifiedPackageName, String qualifiedBinaryFileName) { 144 if (!isPackage(qualifiedPackageName)) return null; 146 try { 147 ClassFileReader reader = ClassFileReader.read(this.zipFile, qualifiedBinaryFileName); 148 if (reader != null) { 149 if (this.accessRuleSet == null) 150 return new NameEnvironmentAnswer(reader, null); 151 String fileNameWithoutExtension = qualifiedBinaryFileName.substring(0, qualifiedBinaryFileName.length() - SuffixConstants.SUFFIX_CLASS.length); 152 return new NameEnvironmentAnswer(reader, this.accessRuleSet.getViolatedRestriction(fileNameWithoutExtension.toCharArray())); 153 } 154 } catch (Exception e) { } 156 return null; 157 } 158 159 public IPath getProjectRelativePath() { 160 if (this.resource == null) return null; 161 return this.resource.getProjectRelativePath(); 162 } 163 164 public boolean isPackage(String qualifiedPackageName) { 165 if (this.knownPackageNames != null) 166 return this.knownPackageNames.includes(qualifiedPackageName); 167 168 try { 169 if (this.zipFile == null) { 170 if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) { 171 System.out.println("(" + Thread.currentThread() + ") [ClasspathJar.isPackage(String)] Creating ZipFile on " + zipFilename); } 173 this.zipFile = new ZipFile(zipFilename); 174 this.closeZipFileAtEnd = true; 175 } 176 this.knownPackageNames = findPackageSet(this); 177 } catch(Exception e) { 178 this.knownPackageNames = new SimpleSet(); } 180 return this.knownPackageNames.includes(qualifiedPackageName); 181 } 182 183 public long lastModified() { 184 if (this.lastModified == 0) 185 this.lastModified = new File(this.zipFilename).lastModified(); 186 return this.lastModified; 187 } 188 189 public String toString() { 190 String start = "Classpath jar file " + this.zipFilename; if (this.accessRuleSet == null) 192 return start; 193 return start + " with " + this.accessRuleSet; } 195 196 public String debugPathString() { 197 long time = lastModified(); 198 if (time == 0) 199 return this.zipFilename; 200 return this.zipFilename + '(' + (new Date(time)) + " : " + time + ')'; } 202 203 } 204 | Popular Tags |