Skip to main content

First Java SE 6 bug!

Posted by mister__m on January 11, 2007 at 4:13 PM PST

As genesis 3.0 is approaching Release Candidate, I decided to test it using the newly released Java SE 6. I ran the test suite and a single test failed, one involving script evaluation (I've blogged about genesis script support almost two years ago).

Since JSR-223 was about to become part of Java SE 6, we've added support for it six months ago. By that time, either the test worked or it hadn't been written yet, but the genesis useradmin sample was running flawless. Well, I've filed a bug in genesis issue tracker and after some investigation, narrowed the problem to its real cause.

Basically, you cannot invoke a static method that overloads an instance method using variable.method(arg0, arg1). The following test case demonstrates the problem:

package test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class FunctionsClass {
   public static boolean xpto(Object arg1, Object arg2) {
      return equals(arg1, arg2);
   }
  
   public static boolean equals(Object arg1, Object arg2) {
      return arg1 == null ? arg2 == null : arg1.equals(arg2);
   }
  
   public static Object echo(Object o) {
      return o;
   }

   public static void main(String[] args) throws Exception {
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
      engine.put("f", new FunctionsClass());
      System.out.println(engine.eval("f.echo(f)"));
      System.out.println(engine.eval("f.xpto('a', 'a')"));
      System.out.println(engine.eval("f.equals('a', 'a')"));
   }
}

This test output should be something like:

test.FunctionsClass@14a9972
true
true

it actually is:

test.FunctionsClass@14a9972
true
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method java.lang.Object.equals(string,string). (#1) in at line number 1
       at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
       at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
       at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
       at test.FunctionsClass.main(FunctionsClass.java:24)

If you can change the script or the class being called, it is easy to work around this issue (by invoking the method on the class itself with, renaming it or rewriting it to become an instance method). However, if you cannot, you better use Rhino directly. When I run the same test using BSF over Rhino, it works.

For future reference, this has been filed as bug # 6512123 (it might take one or two days for it to show up). Let's hope the fix make it to Java SE 6 Update 1. :-)

Related Topics >>