-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSourceFileLookup.java
More file actions
46 lines (37 loc) · 1.67 KB
/
Copy pathSourceFileLookup.java
File metadata and controls
46 lines (37 loc) · 1.67 KB
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
package fi.helsinki.cs.tmc.model;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.openide.filesystems.FileObject;
/**
* Looks up the source FileObject of a given fully qualified class name.
*/
public class SourceFileLookup {
public static SourceFileLookup getDefault() {
return new SourceFileLookup(ProjectMediator.getInstance(), GlobalPathRegistry.getDefault());
}
private final ProjectMediator projectMediator;
private final GlobalPathRegistry globalPathRegistry;
private SourceFileLookup(
ProjectMediator projectMediator, GlobalPathRegistry globalPathRegistry) {
this.projectMediator = projectMediator;
this.globalPathRegistry = globalPathRegistry;
}
public FileObject findSourceFileFor(Exercise exercise, String className) {
String outerClassName = className.replaceAll("\\$.*$", "");
Path path = Paths.get(outerClassName.replace('.', '/') + ".java");
TmcProjectInfo correctProject = projectMediator.tryGetProjectForExercise(exercise);
for (FileObject sr : globalPathRegistry.getSourceRoots()) {
TmcProjectInfo p = projectMediator.tryGetProjectOwningFile(sr);
if (p != null && p.equals(correctProject)) {
FileObject result = sr.getFileObject(path.toString());
if (result != null) {
return result;
}
}
}
// Fall back to findResource picking a source root from any project.
return GlobalPathRegistry.getDefault().findResource(path.toString());
}
}