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

import java.net.URI;
import java.net.URL;
import java.nio.file.*;

public class RuntimeClassPath implements SearchPath {
    public static final RuntimeClassPath INSTANCE = new RuntimeClassPath();

    private RuntimeClassPath() {
    }

    @Override
    @SuppressWarnings("ConstantConditions")
    public Path searchClass(String className) {
        className = Utils.fullClassNameOf(className);
        URI uri = null;
        try {
            Class<?> cls = Class.forName(className);
            URL url = cls.getResource(Utils.simpleNameOf(className) + ".class");
            if (url == null) {
                return null;
            }
            uri = url.toURI();
            return Paths.get(uri);
        } catch (FileSystemNotFoundException e) {
            try {
                FileSystem fs = FileSystems.newFileSystem(uri, null);
                Path p = fs.getPath(className.replace('.', '/') + ".class");
                if (Files.isRegularFile(p)) {
                    return p;
                }
                return null;
            } catch (Exception ex) {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
}