|
15 | 15 | import java.io.InputStreamReader;
|
16 | 16 | import java.io.PrintStream;
|
17 | 17 | import java.net.URISyntaxException;
|
| 18 | +import java.net.URL; |
18 | 19 | import java.security.CodeSource;
|
19 | 20 | import java.util.Calendar;
|
20 | 21 | import java.util.HashSet;
|
21 | 22 | import java.util.Set;
|
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
22 | 25 |
|
23 | 26 | import javax.annotation.processing.AbstractProcessor;
|
24 | 27 | import javax.annotation.processing.ProcessingEnvironment;
|
@@ -64,19 +67,31 @@ public synchronized void init(ProcessingEnvironment env) {
|
64 | 67 | }
|
65 | 68 |
|
66 | 69 | private File findHome() throws URISyntaxException {
|
67 |
| - CodeSource codeSource = getClass().getProtectionDomain().getCodeSource(); |
68 |
| - if (codeSource == null) { |
69 |
| - throw new RuntimeException("Could not find the source code for " + getClass()); |
| 70 | + final CodeSource codeSource = getClass().getProtectionDomain().getCodeSource(); |
| 71 | + final File jarOrClassPath; |
| 72 | + if (codeSource == null || codeSource.getLocation() == null) { |
| 73 | + String className = getClass().getName().replace('.', '/') + ".class"; |
| 74 | + URL location = getClass().getClassLoader().getResource(className); |
| 75 | + if (location == null) { |
| 76 | + throw new RuntimeException("Could not find the source code for " + getClass()); |
| 77 | + } |
| 78 | + Matcher matcher = Pattern.compile("^file:(.+)!/.+\\.class$").matcher(location.getPath()); |
| 79 | + if (!matcher.matches()) { |
| 80 | + throw new RuntimeException("Could not parse URL " + location.getPath()); |
| 81 | + } |
| 82 | + jarOrClassPath = new File(matcher.group(1)); |
| 83 | + } else { |
| 84 | + jarOrClassPath = new File(codeSource.getLocation().toURI()); |
70 | 85 | }
|
71 |
| - File source = new File(codeSource.getLocation().toURI()); |
| 86 | + |
72 | 87 | // this is probably `mxbuild/org.truffleruby.processor/bin` or `mxbuild/dists/jdk1.8/truffleruby-processor.jar`
|
73 | 88 | // let's try to find `mxbuild`
|
| 89 | + File source = jarOrClassPath; |
74 | 90 | while (!source.getName().equals("mxbuild")) {
|
75 | 91 | source = source.getParentFile();
|
76 | 92 | if (source == null) {
|
77 | 93 | throw new RuntimeException(
|
78 |
| - "Could not find `mxbuild` in the source path for " + getClass() + ": " + |
79 |
| - codeSource.getLocation()); |
| 94 | + "Could not find `mxbuild` in the source path for " + getClass() + ": " + jarOrClassPath); |
80 | 95 | }
|
81 | 96 | }
|
82 | 97 | return source.getParentFile();
|
|
0 commit comments