1 /*******************************************************************************
2 * Copyright (c) 2000, 2020 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.jdt.internal.core.builder;
15
16 import java.io.IOException;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.function.Predicate;
20 import java.util.zip.ZipFile;
21
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
29 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
30 import org.eclipse.jdt.internal.compiler.classfmt.ExternalAnnotationDecorator;
31 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
32 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
33 import org.eclipse.jdt.internal.compiler.env.IModule;
34 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
35 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
36 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
37 import org.eclipse.jdt.internal.core.util.Util;
38
39
40 public class ClasspathDirectory extends ClasspathLocation {
41
42 final boolean isOnModulePath;
43 IContainer binaryFolder; // includes .class files for a single directory
44 boolean isOutputFolder;
45 SimpleLookupTable directoryCache;
46 String[] missingPackageHolder = new String[1];
47 AccessRuleSet accessRuleSet;
48 ZipFile annotationZipFile;
49 String externalAnnotationPath;
50
ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder, AccessRuleSet accessRuleSet, IPath externalAnnotationPath, boolean isOnModulePath)51 ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder, AccessRuleSet accessRuleSet, IPath externalAnnotationPath, boolean isOnModulePath) {
52 this.binaryFolder = binaryFolder;
53 this.isOutputFolder = isOutputFolder || binaryFolder.getProjectRelativePath().isEmpty(); // if binaryFolder == project, then treat it as an outputFolder
54 this.directoryCache = new SimpleLookupTable(5);
55 this.accessRuleSet = accessRuleSet;
56 if (externalAnnotationPath != null)
57 this.externalAnnotationPath = externalAnnotationPath.toOSString();
58 this.isOnModulePath = isOnModulePath;
59 }
60
61 @Override
cleanup()62 public void cleanup() {
63 if (this.annotationZipFile != null) {
64 try {
65 this.annotationZipFile.close();
66 } catch(IOException e) { // ignore it
67 }
68 this.annotationZipFile = null;
69 }
70 this.directoryCache = null;
71 }
72
initializeModule()73 IModule initializeModule() {
74 IResource[] members = null;
75 try {
76 members = this.binaryFolder.members();
77 if (members != null) {
78 for (int i = 0, l = members.length; i < l; i++) {
79 IResource m = members[i];
80 String name = m.getName();
81 // Note: Look only inside the default package.
82 if (m.getType() == IResource.FILE && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name)) {
83 if (name.equalsIgnoreCase(IModule.MODULE_INFO_CLASS)) {
84 try {
85 ClassFileReader cfr = Util.newClassFileReader(m);
86 return cfr.getModuleDeclaration();
87 } catch (ClassFormatException | IOException e) {
88 // TODO Java 9 Auto-generated catch block
89 e.printStackTrace();
90 }
91 }
92 }
93 }
94 }
95 } catch (CoreException e1) {
96 e1.printStackTrace();
97 }
98 return null;
99 }
100 /** Lists all java-like files and also sub-directories (for recursive tests). */
directoryList(String qualifiedPackageName)101 String[] directoryList(String qualifiedPackageName) {
102 String[] dirList = (String[]) this.directoryCache.get(qualifiedPackageName);
103 if (dirList == this.missingPackageHolder) return null; // package exists in another classpath directory or jar
104 if (dirList != null) return dirList;
105
106 try {
107 IResource container = this.binaryFolder.findMember(qualifiedPackageName); // this is a case-sensitive check
108 if (container instanceof IContainer) {
109 IResource[] members = ((IContainer) container).members();
110 dirList = new String[members.length];
111 int index = 0;
112 for (int i = 0, l = members.length; i < l; i++) {
113 IResource m = members[i];
114 String name = m.getName();
115 if (m.getType() == IResource.FOLDER || // include folders so we recognize empty parent packages
116 (m.getType() == IResource.FILE && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name))) {
117 // add exclusion pattern check here if we want to hide .class files
118 dirList[index++] = name;
119 }
120 }
121 if (index < dirList.length)
122 System.arraycopy(dirList, 0, dirList = new String[index], 0, index);
123 this.directoryCache.put(qualifiedPackageName, dirList);
124 return dirList;
125 }
126 } catch(CoreException ignored) {
127 // ignore
128 }
129 this.directoryCache.put(qualifiedPackageName, this.missingPackageHolder);
130 return null;
131 }
doesFileExist(String fileName, String qualifiedPackageName, String qualifiedFullName)132 boolean doesFileExist(String fileName, String qualifiedPackageName, String qualifiedFullName) {
133 String[] dirList = directoryList(qualifiedPackageName);
134 if (dirList == null) return false; // most common case
135
136 for (int i = dirList.length; --i >= 0;)
137 if (fileName.equals(dirList[i]))
138 return true;
139 return false;
140 }
141
142 @Override
equals(Object o)143 public boolean equals(Object o) {
144 if (this == o) return true;
145 if (!(o instanceof ClasspathDirectory)) return false;
146
147 ClasspathDirectory dir = (ClasspathDirectory) o;
148 if (this.accessRuleSet != dir.accessRuleSet)
149 if (this.accessRuleSet == null || !this.accessRuleSet.equals(dir.accessRuleSet))
150 return false;
151 if (this.isOnModulePath != dir.isOnModulePath)
152 return false;
153
154 return this.binaryFolder.equals(dir.binaryFolder) && areAllModuleOptionsEqual(dir);
155 }
156 @Override
findClass(String binaryFileName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly, Predicate<String> moduleNameFilter)157 public NameEnvironmentAnswer findClass(String binaryFileName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName, boolean asBinaryOnly, Predicate<String> moduleNameFilter) {
158 if (!doesFileExist(binaryFileName, qualifiedPackageName, qualifiedBinaryFileName)) return null; // most common case
159
160 IBinaryType reader = null;
161 try {
162 reader = Util.newClassFileReader(this.binaryFolder.getFile(new Path(qualifiedBinaryFileName)));
163 } catch (CoreException | ClassFormatException | IOException e) {
164 return null;
165 }
166 if (reader != null) {
167 char[] modName = this.module == null ? null : this.module.name();
168 if (reader instanceof ClassFileReader) {
169 ClassFileReader cfReader = (ClassFileReader) reader;
170 if (cfReader.moduleName == null)
171 cfReader.moduleName = modName;
172 else
173 modName = cfReader.moduleName;
174 }
175 String fileNameWithoutExtension = qualifiedBinaryFileName.substring(0, qualifiedBinaryFileName.length() - SuffixConstants.SUFFIX_CLASS.length);
176 if (this.externalAnnotationPath != null) {
177 try {
178 if (this.annotationZipFile == null) {
179 this.annotationZipFile = ExternalAnnotationDecorator
180 .getAnnotationZipFile(this.externalAnnotationPath, null);
181 }
182 reader = ExternalAnnotationDecorator.create(reader, this.externalAnnotationPath,
183 fileNameWithoutExtension, this.annotationZipFile);
184 } catch (IOException e) {
185 // don't let error on annotations fail class reading
186 }
187 }
188 if (this.accessRuleSet == null)
189 return this.module == null ? new NameEnvironmentAnswer(reader, null) : new NameEnvironmentAnswer(reader, null, modName);
190 return new NameEnvironmentAnswer(reader, this.accessRuleSet.getViolatedRestriction(fileNameWithoutExtension.toCharArray()), modName);
191 }
192 return null;
193 }
194
195 @Override
getProjectRelativePath()196 public IPath getProjectRelativePath() {
197 return this.binaryFolder.getProjectRelativePath();
198 }
199
200 @Override
hashCode()201 public int hashCode() {
202 return this.binaryFolder == null ? super.hashCode() : this.binaryFolder.hashCode();
203 }
204
isExcluded(IResource resource)205 protected boolean isExcluded(IResource resource) {
206 return false;
207 }
208
209 @Override
isOutputFolder()210 public boolean isOutputFolder() {
211 return this.isOutputFolder;
212 }
213
214 @Override
isPackage(String qualifiedPackageName, String moduleName)215 public boolean isPackage(String qualifiedPackageName, String moduleName) {
216 if (moduleName != null) {
217 if (this.module == null || !moduleName.equals(String.valueOf(this.module.name())))
218 return false;
219 }
220 String[] list = directoryList(qualifiedPackageName);
221 if (list != null) {
222 // 1. search files here:
223 for (String entry : list) {
224 String entryLC = entry.toLowerCase();
225 if (entryLC.endsWith(SuffixConstants.SUFFIX_STRING_class) || entryLC.endsWith(SuffixConstants.SUFFIX_STRING_java))
226 return true;
227 }
228 // 2. recurse into sub directories
229 for (String entry : list) {
230 if (entry.indexOf('.') == -1) { // no plain files without '.' are returned by directoryList()
231 if (isPackage(qualifiedPackageName+'/'+entry, null/*already checked*/))
232 return true;
233 }
234 }
235 }
236 return false;
237 }
238 @Override
hasCompilationUnit(String qualifiedPackageName, String moduleName)239 public boolean hasCompilationUnit(String qualifiedPackageName, String moduleName) {
240 String[] dirList = directoryList(qualifiedPackageName);
241 if (dirList != null) {
242 for (String entry : dirList) {
243 String entryLC = entry.toLowerCase();
244 if (entryLC.endsWith(SuffixConstants.SUFFIX_STRING_class) || entryLC.endsWith(SuffixConstants.SUFFIX_STRING_java))
245 return true;
246 }
247 }
248 return false;
249 }
250
251 @Override
reset()252 public void reset() {
253 this.directoryCache = new SimpleLookupTable(5);
254 }
255
256 @Override
toString()257 public String toString() {
258 String start = "Binary classpath directory " + this.binaryFolder.getFullPath().toString(); //$NON-NLS-1$
259 if (this.accessRuleSet == null)
260 return start;
261 return start + " with " + this.accessRuleSet; //$NON-NLS-1$
262 }
263
264 @Override
debugPathString()265 public String debugPathString() {
266 return this.binaryFolder.getFullPath().toString();
267 }
268
269 @Override
findClass(String typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName)270 public NameEnvironmentAnswer findClass(String typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
271 //
272 return findClass(typeName, qualifiedPackageName, moduleName, qualifiedBinaryFileName, false, null);
273 }
274
275 @Override
listPackages()276 public char[][] listPackages() {
277 Set<String> packageNames = new HashSet<>();
278 IPath basePath = this.binaryFolder.getFullPath();
279 try {
280 this.binaryFolder.accept(r -> {
281 String extension = r.getFileExtension();
282 if (r instanceof IFile && extension != null && SuffixConstants.EXTENSION_class.equals(extension.toLowerCase())) {
283 packageNames.add(r.getParent().getFullPath().makeRelativeTo(basePath).toString().replace('/', '.'));
284 }
285 return true;
286 });
287 } catch (CoreException e) {
288 Util.log(e, "Failed to scan packages of "+this.binaryFolder); //$NON-NLS-1$
289 }
290 return packageNames.stream().map(String::toCharArray).toArray(char[][]::new);
291 }
292
293 }
294