aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/glavo/javah/ModulePath.java
blob: 0a9c2ccd0f97f0f74c73f02bcb2aec4c5d59ec66 (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
package org.glavo.javah;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ModulePath implements SearchPath {
    private final Path path;
    private List<Path> roots;

    public ModulePath(Path path) {
        Objects.requireNonNull(path);
        path = path.toAbsolutePath();
        this.path = path;
        if (Files.notExists(path) || !Files.isDirectory(path)) {
            roots = Collections.emptyList();
        } else {
            try {
                roots = Files.list(path)
                        .map(Path::toAbsolutePath)
                        .filter(Files::isRegularFile)
                        .filter(p -> {
                            String n = p.getFileName().toString().toLowerCase();
                            return n.endsWith(".jar") || n.endsWith(".zip") || n.endsWith(".jmod");
                        })
                        .map(Utils::classPathRoot)
                        .filter(Objects::nonNull)
                        .flatMap(p -> SearchPath.multiReleaseRoots(p).stream())
                        .collect(Collectors.toList());
            } catch (IOException e) {
                roots = Collections.emptyList();
            }
        }
    }

    @Override
    public Path search(ClassName name) {
        Objects.requireNonNull(name);
        return SearchPath.searchFromRoots(roots, name);
    }

    @Override
    public String toString() {
        return "ModulePath[" + path + "]";
    }
}