cbuschka / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 0 forks source link

using JSR-223, exception is thrown for clause that shouldn't be evaluated #60

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
    public static void main(String[] args) {

        String script = "String foo=null; if (foo!=null && foo.length()>0) System.out.println(\"bar\")";

        System.out.print("Using script engine: ");
        try {
            new ScriptEngineManager().getEngineByName("beanshell").eval(script);
        } catch (ScriptException e) {
            System.out.println(e.getMessage());
        }

        System.out.print("\nUsing interpreter: ");
        try {
            new Interpreter().eval(script);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

What is the expected output? What do you see instead?
Since foo==null, i would not expect foo.length to be evaluated; this 
expectation is met by the direct Bsh interpreter but not by the ScriptEngine. 
Here's the output:
Using script engine: bsh.EvalError: Sourced file: inline evaluation of: 
``String foo=null; if (foo!=null && foo.length()>0) 
System.out.println("bar");'' : Attempt to resolve method: length() on undefined 
variable or class name: foo : at Line: 1 : in file: inline evaluation of: 
``String foo=null; if (foo!=null && foo.length()>0) 
System.out.println("bar");'' : foo .length ( ) 
 in inline evaluation of: ``String foo=null; if (foo!=null && foo.length()>0) System.out.println("bar");'' at line number 1

Using interpreter: 

What version of the product are you using? On what operating system?
bsh-2.1b4 on windows xp

Please provide any additional information below.

Original issue reported on code.google.com by etans...@gmail.com on 11 Nov 2011 at 7:12

GoogleCodeExporter commented 8 years ago
The bsh.BshScriptEngine just delegates to the interpreter.
You are getting the NPE because

    new ScriptEngineManager().getEngineByName("beanshell")

evaluates to <null>.
Nevertheless, I can't reproduce this. 
Could you verify that your jar is correct and contains a file 
"META-INF/services/javax.script.ScriptEngineFactory"? If not you may run into 
issue#43
http://code.google.com/p/beanshell2/issues/detail?id=43
Did you build the jar on your own? You must not use ant 1.7 to build..

Original comment by pejob...@gmail.com on 12 Nov 2011 at 3:51

GoogleCodeExporter commented 8 years ago
Thanks for taking a look, but i'm still having the issue.
i'm using the 2.1b4 jarfile which contains the javax.script.ScriptEngineFactory.
i did not build my own jar.
note that the exception is not an NPE, but a complaint about foo being unknown.
the exception occurs only when foo is null.

Original comment by etans...@gmail.com on 14 Nov 2011 at 5:59

GoogleCodeExporter commented 8 years ago
note: in addition to delegating to an interpreter, the ScriptingEngine calls 
setNameSpace() which may be causing the issue.

Original comment by etans...@gmail.com on 14 Nov 2011 at 9:32

GoogleCodeExporter commented 8 years ago
Ok, i drilled down the problem again. It's a duplicate to issue#55 which is 
already fixed in trunk and branch v2.1. So the fix will be included in the next 
version 2.1b5.

Original comment by pejob...@gmail.com on 16 Nov 2011 at 9:26