Skip to content

Commit c5a6aad

Browse files
committed
Merge branch 'legacy-editor'
This topic branch fixes bugs identified while improving ImageJ2's script editor with an eye to the legacy code. In particular, we need to be able to run ImageJ 1.x plugins (with a LegacyJavaRunner contained in the imagej-legacy project) so that Fiji can switch over to using ImageJ2's script editor instead of its stale Script Editor (from where ImageJ2's version was developed). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents a84364d + e17a93f commit c5a6aad

File tree

6 files changed

+140
-15
lines changed

6 files changed

+140
-15
lines changed

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>2.2</version>
8+
<version>2.5</version>
99
<relativePath />
1010
</parent>
1111

@@ -97,12 +97,6 @@
9797
<artifactId>junit</artifactId>
9898
<scope>test</scope>
9999
</dependency>
100-
<dependency>
101-
<groupId>org.scijava</groupId>
102-
<artifactId>scijava-common</artifactId>
103-
<classifier>tests</classifier>
104-
<scope>test</scope>
105-
</dependency>
106100
</dependencies>
107101

108102
<build>

src/main/java/org/scijava/plugins/scripting/java/JavaEngine.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ public void compile(final File file, final Writer errorWriter) {
171171
}
172172
}
173173

174+
/**
175+
* Packages the build product into a {@code .jar} file.
176+
*
177+
* @param file a {@code .java} or {@code pom.xml} file
178+
* @param includeSources whether to include the sources or not
179+
* @param output the {@code .jar} file to write to
180+
* @param errorWriter the destination for error messages
181+
*/
174182
public void makeJar(final File file, final boolean includeSources, final File output, final Writer errorWriter) {
175183
try {
176184
final Builder builder = new Builder(file, null, errorWriter);
@@ -288,9 +296,7 @@ private MavenProject getMavenProject(final BuildEnvironment env,
288296
final File pom = new File(path, "pom.xml");
289297
if (pom.exists()) return env.parse(pom, null);
290298
}
291-
final File rootDirectory = file.getParentFile();
292-
final String artifactId = fakeArtifactId(env, file.getName());
293-
return fakePOM(env, rootDirectory, artifactId, mainClass);
299+
return writeTemporaryProject(env, new FileReader(file));
294300
}
295301

296302
private static String getFullClassName(final File file) throws IOException {
@@ -361,7 +367,8 @@ private static MavenProject writeTemporaryProject(final BuildEnvironment env,
361367
}
362368

363369
// write POM
364-
return fakePOM(env, directory, fakeArtifactId(env, directory.getName()), mainClass);
370+
final String artifactId = mainClass.substring(mainClass.lastIndexOf('.') + 1);
371+
return fakePOM(env, directory, artifactId, mainClass, true);
365372
}
366373

367374
private static String fakeArtifactId(final BuildEnvironment env, final String name) {
@@ -379,7 +386,7 @@ private static String fakeArtifactId(final BuildEnvironment env, final String na
379386
}
380387

381388
private static MavenProject fakePOM(final BuildEnvironment env,
382-
final File directory, final String artifactId, final String mainClass)
389+
final File directory, final String artifactId, final String mainClass, boolean writePOM)
383390
throws IOException, ParserConfigurationException, SAXException,
384391
TransformerConfigurationException, TransformerException,
385392
TransformerFactoryConfigurationError {
@@ -396,7 +403,6 @@ private static MavenProject fakePOM(final BuildEnvironment env,
396403
append(pom, project, "version", DEFAULT_VERSION);
397404

398405
final Element build = append(pom, project, "build", null);
399-
append(pom, build, "sourceDirectory", directory.getPath());
400406

401407
if (mainClass != null) {
402408
final Element plugins = append(pom, build, "plugins", null);
@@ -428,6 +434,9 @@ private static MavenProject fakePOM(final BuildEnvironment env,
428434
return env.parse(pomFile);
429435
}
430436
}
437+
if (writePOM) {
438+
transformer.transform(new DOMSource(pom), new StreamResult(new File(directory, "pom.xml")));
439+
}
431440
final ByteArrayOutputStream out = new ByteArrayOutputStream();
432441
transformer.transform(new DOMSource(pom), new StreamResult(out));
433442
return env.parse(new ByteArrayInputStream(out.toByteArray()), directory, null, null);

src/main/java/org/scijava/plugins/scripting/java/JavaScriptLanguage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public String getEngineName() {
6767
return "MiniMaven";
6868
}
6969

70+
@Override
71+
public List<String> getNames() {
72+
return Arrays.asList("Java");
73+
}
74+
7075
@Override
7176
public List<String> getMimeTypes() {
7277
return Arrays.asList("application/x-java");

src/test/java/org/scijava/plugins/scripting/java/JavaEngineTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public void minimalProjectFromPOM() throws Exception {
9797
assertTrue(jar.exists());
9898

9999
System.gc();
100-
assertTrue(FileUtils.deleteRecursively(dir));
101100
}
102101

103102
@Test
@@ -134,7 +133,6 @@ public void minimalProjectFromSource() throws Exception {
134133
assertTrue(jar.exists());
135134

136135
System.gc();
137-
assertTrue(FileUtils.deleteRecursively(dir));
138136
}
139137

140138
@Test
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Java scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.plugins.scripting.java;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertTrue;
36+
37+
import java.io.File;
38+
import java.io.IOException;
39+
import java.io.StringWriter;
40+
import java.util.Arrays;
41+
import java.util.Set;
42+
import java.util.TreeSet;
43+
import java.util.jar.JarEntry;
44+
import java.util.jar.JarFile;
45+
46+
import org.junit.Test;
47+
import org.scijava.test.TestUtils;
48+
import org.scijava.util.FileUtils;
49+
import org.scijava.util.IteratorPlus;
50+
51+
public class MakeJarTest {
52+
@Test
53+
public void testSingle() throws Exception {
54+
final StringWriter writer = new StringWriter();
55+
final JavaEngine engine = new JavaEngine();
56+
final File file = FileUtils.urlToFile(getClass().getResource("/Dummy.java"));
57+
final File tmpDir = TestUtils.createTemporaryDirectory("jar-test-");
58+
final File output = new File(tmpDir, "test.jar");
59+
engine.makeJar(file, false, output, writer);
60+
assertJarEntries(output, "META-INF/MANIFEST.MF",
61+
"META-INF/maven/net.imagej/Dummy/pom.xml", "Dummy.class");
62+
engine.makeJar(file, true, output, writer);
63+
assertJarEntries(output, "META-INF/MANIFEST.MF",
64+
"META-INF/maven/net.imagej/Dummy/pom.xml", "Dummy.class",
65+
"pom.xml", "src/main/java/Dummy.java");
66+
}
67+
68+
private void assertJarEntries(File output, String... paths) throws IOException {
69+
final Set<String> set = new TreeSet<String>(Arrays.asList(paths));
70+
final JarFile jar = new JarFile(output);
71+
for (final JarEntry entry : new IteratorPlus<JarEntry>(jar.entries())) {
72+
final String path = entry.getName();
73+
assertTrue("Unexpected: " + path, set.remove(path));
74+
}
75+
jar.close();
76+
assertEquals("Missing: " + set, 0, set.size());
77+
}
78+
}

src/test/resources/Dummy.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Java scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
/**
33+
* Dummy class for testing {@code .jar} file packaging.
34+
*
35+
* @author Johannes Schindelin
36+
*/
37+
public class Dummy {
38+
public static void main(final String... args) {
39+
throw new RuntimeException("Civilized people do not execute!");
40+
}
41+
}

0 commit comments

Comments
 (0)