Skip to content

Commit 518f84a

Browse files
committed
Do not claim ownership of .java source files
BeanShell is a very close approximation to Java syntax. But it does not handle everything you can write into a .java file. And it certainly does not actually try to compile and run it with javac. It is wrong for BeanShell to snarf up these files, and in particular it clashes with the SciJava scripting-java component, which *does* actually use javac.
1 parent 41582b5 commit 518f84a

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/main/java/org/scijava/plugins/scripting/beanshell/BeanshellScriptLanguage.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
package org.scijava.plugins.scripting.beanshell;
3333

34+
import java.util.List;
35+
import java.util.stream.Collectors;
36+
3437
import javax.script.ScriptEngine;
3538

3639
import org.scijava.plugin.Plugin;
@@ -44,6 +47,7 @@
4447
* An adapter of the {@link BshScriptEngineFactory} to the SciJava scripting interface.
4548
*
4649
* @author Mark Hiner
50+
* @author Curtis Rueden
4751
* @author Johannes Schindelin
4852
* @see ScriptEngine
4953
*/
@@ -54,6 +58,8 @@ public BeanshellScriptLanguage() {
5458
super(new BshScriptEngineFactory());
5559
}
5660

61+
// -- ScriptLanguage methods --
62+
5763
@Override
5864
public Object decode(final Object object) {
5965
if (object instanceof Primitive) {
@@ -66,4 +72,29 @@ public Object decode(final Object object) {
6672
return object;
6773
}
6874

75+
// -- ScriptEngineFactory methods --
76+
77+
// NB: BeanShell must *not* claim ownership of .java files!
78+
// It clashes with the scripting-java script language.
79+
80+
@Override
81+
public List<String> getExtensions() {
82+
return super.getExtensions().stream()//
83+
.filter(extension -> !"java".equals(extension))//
84+
.collect(Collectors.toList());
85+
}
86+
87+
@Override
88+
public List<String> getMimeTypes() {
89+
return super.getMimeTypes().stream()//
90+
.filter(mimeType -> !"application/x-java-source".equals(mimeType))//
91+
.collect(Collectors.toList());
92+
}
93+
94+
@Override
95+
public List<String> getNames() {
96+
return super.getNames().stream()//
97+
.filter(name -> !"java".equalsIgnoreCase(name))//
98+
.collect(Collectors.toList());
99+
}
69100
}

‎src/test/java/org/scijava/plugins/scripting/beanshell/BeanshellTest.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
import static org.junit.Assert.assertEquals;
3535

36-
import java.io.IOException;
36+
import java.util.Arrays;
37+
import java.util.List;
3738
import java.util.concurrent.ExecutionException;
3839

3940
import javax.script.Bindings;
@@ -137,4 +138,17 @@ public void testNull() throws InterruptedException, ExecutionException {
137138
final Object result = m.getReturnValue();
138139
assertEquals(expected, result);
139140
}
141+
142+
@Test
143+
public void testExtensionsMimeTypesNames() {
144+
// NB: We *don't* want BeanShell to claim ownership of .java files!
145+
final ScriptLanguage lang = scriptService.getLanguageByExtension("bsh");
146+
final List<String> expectedExtensions = Arrays.asList("bsh");
147+
assertEquals(expectedExtensions, lang.getExtensions());
148+
final List<String> expectedMimeTypes = Arrays.asList(
149+
"application/x-beanshell", "application/x-bsh");
150+
assertEquals(expectedMimeTypes, lang.getMimeTypes());
151+
final List<String> expectedNames = Arrays.asList("beanshell", "bsh");
152+
assertEquals(expectedNames, lang.getNames());
153+
}
140154
}

0 commit comments

Comments
 (0)