Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25129fa304185d4b868d5ffa0a67554c15eecf17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package org.eclipse.jdt.internal.compiler.batch;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import java.io.*;

import org.eclipse.jdt.internal.compiler.env.*;
import org.eclipse.jdt.internal.compiler.util.*;

public class FileSystem implements INameEnvironment  {
	Classpath[] classpaths;
	String[] knownFileNames;

	interface Classpath {
		boolean exists(String filename, char[][] packageName);
		long lastModified(String filename, char[][] packageName);
		NameEnvironmentAnswer readClassFile(String filename, char[][] packageName);
		NameEnvironmentAnswer readJavaFile(String filename, char[][] packageName);
		boolean isPackage(char[][] compoundName, char[] packageName); 
	}
/*
	classPathNames is a collection is Strings representing the full path of each class path
	initialFileNames is a collection is Strings, the trailing '.java' will be removed if its not already.
*/

public FileSystem(String[] classpathNames, String[] initialFileNames) {
	int classpathSize = classpathNames.length;
	classpaths = new Classpath[classpathSize];
	String[] pathNames = new String[classpathSize];
	int problemsOccured = 0;
	for (int i = 0; i < classpathSize; i++) {
		try {
			File file = new File(convertPathSeparators(classpathNames[i]));
			if (file.exists()) {
				if (file.isDirectory()) {
					classpaths[i] = new ClasspathDirectory(file);
					pathNames[i] = ((ClasspathDirectory) classpaths[i]).path;
				} else if (classpathNames[i].endsWith(".jar") | (classpathNames[i].endsWith(".zip"))) {
					classpaths[i] = new ClasspathJar(file);
					pathNames[i] = classpathNames[i].substring(0, classpathNames[i].lastIndexOf('.'));
				}
			}
		} catch (IOException e) {
			classpaths[i] = null;
		}
		if (classpaths[i] == null)
			problemsOccured++;
	}
	if (problemsOccured > 0) {
		Classpath[] newPaths = new Classpath[classpathSize - problemsOccured];
		String[] newNames = new String[classpathSize - problemsOccured];
		for (int i = 0, current = 0; i < classpathSize; i++)
			if (classpaths[i] != null) {
				newPaths[current] = classpaths[i];
				newNames[current++] = pathNames[i];
			}
		classpathSize = newPaths.length;
		classpaths = newPaths;
		pathNames = newNames;
	}

	knownFileNames = new String[initialFileNames.length];
	for (int i = initialFileNames.length; --i >= 0;) {
		String fileName = initialFileNames[i];
		String matchingPathName = null;
		if (fileName.lastIndexOf(".") != -1)
			fileName = fileName.substring(0, fileName.lastIndexOf('.')); // remove trailing ".java"

		fileName = convertPathSeparators(fileName);
		for (int j = 0; j < classpathSize; j++)
			if (fileName.startsWith(pathNames[j]))
				matchingPathName = pathNames[j];
		if (matchingPathName == null)
			knownFileNames[i] = fileName; // leave as is...
		else
			knownFileNames[i] = fileName.substring(matchingPathName.length());
	}
}
static String assembleName(char[] fileName, char[][] packageName, char separator) {
	return new String(CharOperation.concatWith(packageName, fileName, separator));
}
static String assembleName(String fileName, char[][] packageName, char separator) {
	return new String(
		CharOperation.concatWith(
			packageName,
			fileName == null ? null : fileName.toCharArray(),
			separator));
}
private String convertPathSeparators(String path) {
	if (File.separatorChar == '/')
		return path.replace('\\', '/');
	else
		return path.replace('/', '\\');
}
private NameEnvironmentAnswer findClass(char[] name, char[][] packageName) {
	String fullName = assembleName(name, packageName, File.separatorChar);
	for (int i = 0, length = knownFileNames.length; i < length; i++)
		if (fullName.equals(knownFileNames[i]))
			return null; // looking for a file which we know was provided at the beginning of the compilation

	String filename = new String(name);
	String binaryFilename = filename + ".class";
	String sourceFilename = filename + ".java";
	for (int i = 0, length = classpaths.length; i < length; i++) {
		Classpath classpath = classpaths[i];
		boolean binaryExists = classpath.exists(binaryFilename, packageName);
		boolean sourceExists = classpath.exists(sourceFilename, packageName);
		if (binaryExists == sourceExists) {
			if (binaryExists) { // so both are true
				long binaryModified = classpath.lastModified(binaryFilename, packageName);
				long sourceModified = classpath.lastModified(sourceFilename, packageName);
				if (binaryModified > sourceModified)
					return classpath.readClassFile(binaryFilename, packageName);
				if (sourceModified > 0)
					return classpath.readJavaFile(sourceFilename, packageName);
			}
		} else {
			if (binaryExists)
				return classpath.readClassFile(binaryFilename, packageName);
			else
				return classpath.readJavaFile(sourceFilename, packageName);
		}
	}
	return null; 
}
public NameEnvironmentAnswer findType(char[][] compoundName) {
	if (compoundName == null)
		return null;
	else
		return findClass(
			compoundName[compoundName.length - 1],
			CharOperation.subarray(compoundName, 0, compoundName.length - 1));
}
public NameEnvironmentAnswer findType(char[] name, char[][] compoundName) {
	if (name == null)
		return null;
	else
		return findClass(name, compoundName);
}
public boolean isPackage(char[][] compoundName, char[] packageName) {
	if (compoundName == null)
		compoundName = new char[0][];

	for (int i = 0, length = classpaths.length; i < length; i++)
		if (classpaths[i].isPackage(compoundName, packageName))
			return true;
	return false;
}
}

Back to the top