aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/glavo/javah/JavahTask.java
blob: 561485463280508b48f68514c42c3493bec176ff (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
152
153
154
155
156
157
158
159
160
161
162
package org.glavo.javah;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class JavahTask {
    private PrintWriter errorHandle = new PrintWriter(System.err);
    private boolean outputToSignalFile = false;
    private Path outputPath = null;

    private final List<SearchPath> searchPaths = new ArrayList<>();
    private final List<String> classList = new ArrayList<>();

    public void run() throws IOException {
        if (outputPath == null) {
            errorHandle.println("output path is not set");
            return;
        }

        if (outputToSignalFile) {
            try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(outputPath))) {
                writer.write(JNIGenerator.FILE_HEADER);
                classList.stream()
                        .map(this::search)
                        .filter(Objects::nonNull)
                        .filter(Files::isReadable)
                        .map(p -> new JNIGenerator(writer, p))
                        .forEachOrdered(JNIGenerator::generate);

                writer.write(JNIGenerator.FILE_END);
            }
        } else {
            for (String c : classList) {
                Path p = search(c);
                if (p == null) {
                    continue;
                }
                try (PrintWriter output = new PrintWriter(Files.newBufferedWriter(
                        outputPath.resolve(Utils.encode(c).replace('.', '_') + ".h")))) {
                    output.write(JNIGenerator.FILE_HEADER);
                    new JNIGenerator(output, p).generate();
                    output.write(JNIGenerator.FILE_END);
                }
            }
        }
    }

    private Path search(String className) {
        for (SearchPath searchPath : searchPaths) {
            Path c = searchPath.searchClass(className);
            if (c != null) {
                return c;
            }
        }
        errorHandle.println("class" + className + " not found");
        return null;
    }

    //
    // Getters and Setters
    //

    public void addSearchPath(SearchPath searchPath) {
        Objects.requireNonNull(searchPath);
        searchPaths.add(searchPath);
    }

    public void addModulePath(Path modulePath) {
        Objects.requireNonNull(modulePath);
        searchPaths.add(new ModulePath(modulePath));
    }

    public void addModulePaths(String modulePaths) {
        Objects.requireNonNull(modulePaths);
        Arrays.stream(modulePaths.split(File.pathSeparator))
                .filter(s -> !"".equals(s))
                .map(Paths::get)
                .filter(Files::isDirectory)
                .forEachOrdered(this::addModulePath);
    }

    public void addClasspath(Path classPath) {
        Objects.requireNonNull(classPath);
        searchPaths.add(new ClassPath(classPath));
    }

    public void addClasspaths(String cps) {
        Arrays.stream(cps.split(File.pathSeparator))
                .filter(s -> !"".equals(s))
                .flatMap(s -> {
                    if (s.endsWith(File.separatorChar + "*")) {
                        try {
                            return Files.list(Paths.get(s.substring(0, s.length() - 2)))
                                    .filter(p -> p.getFileName().toString().toLowerCase().endsWith(".jar"));
                        } catch (IOException e) {
                            return Stream.empty();
                        }
                    }
                    return Stream.of(Paths.get(s));
                })
                .filter(Files::exists)
                .map(ClassPath::new)
                .forEachOrdered(searchPaths::add);
    }

    public void addRuntimeClasspath() {
        searchPaths.add(RuntimeClassPath.INSTANCE);
    }

    public List<SearchPath> searchPaths() {
        return searchPaths;
    }

    public PrintWriter getErrorHandle() {
        return errorHandle;
    }

    public void setErrorHandle(Writer handle) {
        if (handle == null || handle instanceof PrintWriter) {
            errorHandle = (PrintWriter) handle;
        } else {
            errorHandle = new PrintWriter(handle);
        }
    }

    public boolean isOutputToSignalFile() {
        return outputToSignalFile;
    }

    public JavahTask setOutputToSignalFile(boolean outputToSignalFile) {
        this.outputToSignalFile = outputToSignalFile;
        return this;
    }

    public Path getOutputPath() {
        return outputPath;
    }

    public JavahTask setOutputPath(Path outputPath) {
        this.outputPath = outputPath;
        return this;
    }

    public void addClass(String name) {
        classList.add(name);
    }

    public List<String> getClassList() {
        return classList;
    }
}