1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.core.builder;
13 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
14 import net.sourceforge.phpdt.internal.core.Util;
15 import net.sourceforge.phpdt.internal.core.util.SimpleLookupTable;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
22 class ClasspathDirectory extends ClasspathLocation {
24 IContainer binaryFolder; // includes .class files for a single directory
25 boolean isOutputFolder;
26 String binaryLocation;
27 SimpleLookupTable directoryCache;
28 String[] missingPackageHolder = new String[1];
30 ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder) {
31 this.binaryFolder = binaryFolder;
32 this.isOutputFolder = isOutputFolder;
33 IPath location = binaryFolder.getLocation();
34 this.binaryLocation = location != null ? location.addTrailingSeparator().toString() : ""; //$NON-NLS-1$
36 this.directoryCache = new SimpleLookupTable(5);
39 public void cleanup() {
40 this.directoryCache = null;
43 String[] directoryList(String qualifiedPackageName) {
44 String[] dirList = (String[]) directoryCache.get(qualifiedPackageName);
45 if (dirList == missingPackageHolder) return null; // package exists in another classpath directory or jar
46 if (dirList != null) return dirList;
49 IResource container = binaryFolder.findMember(qualifiedPackageName); // this is a case-sensitive check
50 if (container instanceof IContainer) {
51 IResource[] members = ((IContainer) container).members();
52 dirList = new String[members.length];
54 for (int i = 0, l = members.length; i < l; i++) {
55 IResource m = members[i];
56 // if (m.getType() == IResource.FILE && Util.isClassFileName(m.getName()))
57 // // add exclusion pattern check here if we want to hide .class files
58 // dirList[index++] = m.getName();
60 if (index < dirList.length)
61 System.arraycopy(dirList, 0, dirList = new String[index], 0, index);
62 directoryCache.put(qualifiedPackageName, dirList);
65 } catch(CoreException ignored) {
67 directoryCache.put(qualifiedPackageName, missingPackageHolder);
71 boolean doesFileExist(String fileName, String qualifiedPackageName, String qualifiedFullName) {
72 String[] dirList = directoryList(qualifiedPackageName);
73 if (dirList == null) return false; // most common case
75 for (int i = dirList.length; --i >= 0;)
76 if (fileName.equals(dirList[i]))
81 public boolean equals(Object o) {
82 if (this == o) return true;
83 if (!(o instanceof ClasspathDirectory)) return false;
85 return binaryFolder.equals(((ClasspathDirectory) o).binaryFolder);
88 public NameEnvironmentAnswer findClass(String binaryFileName, String qualifiedPackageName, String qualifiedBinaryFileName) {
89 if (!doesFileExist(binaryFileName, qualifiedPackageName, qualifiedBinaryFileName)) return null; // most common case
92 // ClassFileReader reader = ClassFileReader.read(binaryLocation + qualifiedBinaryFileName);
93 // if (reader != null) return new NameEnvironmentAnswer(reader);
94 // } catch (Exception e) {} // treat as if class file is missing
98 public IPath getProjectRelativePath() {
99 return binaryFolder.getProjectRelativePath();
102 public boolean isOutputFolder() {
103 return isOutputFolder;
106 public boolean isPackage(String qualifiedPackageName) {
107 return directoryList(qualifiedPackageName) != null;
110 public void reset() {
111 this.directoryCache = new SimpleLookupTable(5);
114 public String toString() {
115 return "Binary classpath directory " + binaryFolder.getFullPath().toString(); //$NON-NLS-1$