Skip to content

Commit 8aecf15

Browse files
committed
[GR-19520] Fix BuildInformationProcessor to work when run by IGV / NetBeans.
PullRequest: truffleruby/1156
2 parents 3a584db + c0cc1a7 commit 8aecf15

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/processor/java/org/truffleruby/processor/BuildInformationProcessor.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
import java.io.InputStreamReader;
1616
import java.io.PrintStream;
1717
import java.net.URISyntaxException;
18+
import java.net.URL;
1819
import java.security.CodeSource;
1920
import java.util.Calendar;
2021
import java.util.HashSet;
2122
import java.util.Set;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
2225

2326
import javax.annotation.processing.AbstractProcessor;
2427
import javax.annotation.processing.ProcessingEnvironment;
@@ -64,19 +67,31 @@ public synchronized void init(ProcessingEnvironment env) {
6467
}
6568

6669
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());
7085
}
71-
File source = new File(codeSource.getLocation().toURI());
86+
7287
// this is probably `mxbuild/org.truffleruby.processor/bin` or `mxbuild/dists/jdk1.8/truffleruby-processor.jar`
7388
// let's try to find `mxbuild`
89+
File source = jarOrClassPath;
7490
while (!source.getName().equals("mxbuild")) {
7591
source = source.getParentFile();
7692
if (source == null) {
7793
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);
8095
}
8196
}
8297
return source.getParentFile();

0 commit comments

Comments
 (0)