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

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import static org.glavo.javah.Utils.*;

public interface SearchPath {
    Path search(ClassName name);

    default Path search(String fullName) {
        Objects.requireNonNull(fullName);
        return search(ClassName.of(fullName));
    }

    static Path searchFrom(Iterable<SearchPath> searchPaths, ClassName name) {
        Objects.requireNonNull(searchPaths);
        Objects.requireNonNull(name);

        for (SearchPath searchPath : searchPaths) {
            if (searchPath == null) {
                continue;
            }
            Path p = searchPath.search(name);
            if (p != null) {
                return p;
            }
        }
        return null;
    }

    static Path searchFromRoots(Iterable<Path> roots, ClassName name) {
        Objects.requireNonNull(roots);
        Objects.requireNonNull(name);
        for (Path root : roots) {
            if (root == null || !Files.isDirectory(root)) {
                continue;
            }

            Path p = root.resolve(name.relativePath());
            if (Files.isRegularFile(p)) {
                return p;
            }
            if (Files.isSymbolicLink(p)) {
                try {
                    p = Files.readSymbolicLink(p);
                    if (Files.isRegularFile(p)) {
                        return p;
                    }
                } catch (IOException ignored) {
                }
            }
        }

        return null;
    }

    static List<Path> multiReleaseRoots(Path root) {
        Objects.requireNonNull(root);
        if (!Files.isDirectory(root)) {
            return Collections.emptyList();
        }
        boolean isMultiRelease = false;
        try (InputStream in = Files.newInputStream(root.resolve("META-INF").resolve("MANIFEST.MF"))) {
            isMultiRelease = "true".equals(new Manifest(in).getMainAttributes().getValue("Multi-Release"));
        } catch (IOException | NullPointerException ignored) {
        }

        if (isMultiRelease) {
            Path base = root.resolve("META-INF").resolve("versions");
            if (Files.isDirectory(base)) {
                try {
                    List<Path> list = Files.list(base)
                            .map(Path::toAbsolutePath)
                            .filter(Files::isDirectory)
                            .filter(p -> MULTI_RELEASE_VERSIONS.contains(p.getFileName().toString()))
                            .sorted(Comparator.comparing((Path p) -> Integer.parseInt(p.getFileName().toString())).reversed())
                            .collect(Collectors.toCollection(LinkedList::new));
                    list.add(root);
                    return Collections.unmodifiableList(list);
                } catch (IOException ignored) {
                }
            }
        }
        return Collections.singletonList(root);
    }
}