Skip to content

Commit 4d15013

Browse files
committed
Fix the "Open .java file for class" command
The "http://fiji.sc/Foo.java" shortcut has not worked for many years. And will not be made to work again, due to security considerations. Instead, we look in the POM and/or JAR manifest for the given class.
1 parent 917d1b3 commit 4d15013

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/java/org/scijava/ui/swing/script/FileFunctions.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Iterator;
4848
import java.util.List;
4949
import java.util.Map;
50+
import java.util.Objects;
5051
import java.util.jar.JarEntry;
5152
import java.util.jar.JarFile;
5253
import java.util.regex.Matcher;
@@ -65,7 +66,10 @@
6566
import org.scijava.util.AppUtils;
6667
import org.scijava.util.FileUtils;
6768
import org.scijava.util.LineOutputStream;
69+
import org.scijava.util.Manifest;
70+
import org.scijava.util.POM;
6871
import org.scijava.util.ProcessUtils;
72+
import org.scijava.util.Types;
6973

7074
/**
7175
* TODO
@@ -165,7 +169,22 @@ public String getSourcePath(
165169
}
166170

167171
public String getSourceURL(final String className) {
168-
return "http://fiji.sc/" + className.replace('.', '/') + ".java";
172+
final Class<?> c = Types.load(className, false);
173+
final POM pom = POM.getPOM(c);
174+
final String scmPath = pom.getSCMURL();
175+
if (scmPath == null || scmPath.isEmpty()) return null;
176+
final String branch;
177+
final String scmTag = pom.getSCMTag();
178+
if (scmTag == null || scmTag.isEmpty() || Objects.equals(scmTag, "HEAD")) {
179+
final Manifest m = Manifest.getManifest(c);
180+
final String commit = m == null ? null : m.getImplementationBuild();
181+
branch = commit == null || commit.isEmpty() ? "master" : commit;
182+
}
183+
else branch = scmTag;
184+
185+
final String prefix = scmPath.endsWith("/") ? scmPath : scmPath + "/";
186+
return prefix + "blob/" + branch + "/src/main/java/" + //
187+
className.replace('.', '/') + ".java";
169188
}
170189

171190
protected static Map<String, List<String>> class2source;

src/main/java/org/scijava/ui/swing/script/TextEditor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,8 +1403,8 @@ else if (source == openMacroFunctions) try {
14031403
else if (source == openSourceForClass) {
14041404
final String className = getSelectedClassNameOrAsk();
14051405
if (className != null) {
1406-
final String url = new FileFunctions(this).getSourceURL(className);
14071406
try {
1407+
final String url = new FileFunctions(this).getSourceURL(className);
14081408
platformService.open(new URL(url));
14091409
}
14101410
catch (final Throwable e) {

0 commit comments

Comments
 (0)