/
BuildClosureQueue.java
122 lines (106 loc) · 3.3 KB
/
BuildClosureQueue.java
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2objc;
import com.google.common.collect.Sets;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.FileUtil;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;
/**
* Looks up and queues dependencies for --build-closure.
*/
public class BuildClosureQueue {
private static final Logger logger = Logger.getLogger(BuildClosureQueue.class.getName());
private final Set<String> processedNames = Sets.newHashSet();
private final Set<String> queuedNames = Sets.newLinkedHashSet();
/**
* Returns the next Java source file to be processed. Returns null if the
* queue is empty.
*/
public InputFile getNextFile() {
for (Iterator<String> iter = queuedNames.iterator(); iter.hasNext(); ) {
String name = iter.next();
iter.remove();
processedNames.add(name);
InputFile file = getFileForName(name);
if (file != null) {
return file;
}
}
return null;
}
/**
* Adds a name to the queue. The name must be the fully qualified type name
* to search for (dot separated).
*/
public void addName(String name) {
if (!processedNames.contains(name)) {
queuedNames.add(name);
}
}
/**
* Adds the name of a file that has been processed to ensure that this name is
* not searched for in the future.
*/
public void addProcessedName(String name) {
processedNames.add(name);
queuedNames.remove(name);
}
private static InputFile getFileForName(String name) {
// Check if class exists on classpath.
if (findClassFile(name)) {
logger.finest("no source for " + name + ", class found");
return null;
}
InputFile inputFile = null;
try {
inputFile = FileUtil.findOnSourcePath(name);
} catch (IOException e) {
ErrorUtil.warning(e.getMessage());
}
if (inputFile == null) {
return null;
}
// Check if the source file is older than the generated header file.
File headerSource =
new File(Options.getOutputDirectory(), name.replace('.', File.separatorChar) + ".h");
if (headerSource.exists() && inputFile.lastModified() < headerSource.lastModified()) {
return null;
}
return inputFile;
}
private static boolean findClassFile(String name) {
InputFile f = null;
try {
f = FileUtil.findOnClassPath(name);
} catch (IOException e) {
ErrorUtil.warning(e.getMessage());
}
if (f != null) {
return true;
}
// See if it's a JRE class.
try {
Class.forName(name);
return true;
} catch (ClassNotFoundException e) {
// Fall-through.
}
return false;
}
}