Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6554525

javax.scripting javascript string equality incorrect for wrapped java Strings

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: 8-pool
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      h:\>java -version
      java version "1.6.0_01"
      Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :

      Microsoft Windows XP [Version 5.1.2600]


      A DESCRIPTION OF THE PROBLEM :
      This is not a swing problem, it is just that swing is the nearest matching subcategory it starts with pjavax !) and I had to choose one.

      When running the javascript scripting engine, java Strings that are equal() but != and have been wrapped as javascript objects after being returned from a method call on another java object are NOT considered == whereas they should be.

      However the same two java Strings, when wrapped using engine.put() do compare ==

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run attached junit test

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      eq4 is true
      ACTUAL -
      eq4 is false

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;
      import javax.script.ScriptException;
      import junit.framework.*;
      /*
       * StringEqualityTest.java
       * JUnit based test
       *
       * Created on 3 May 2007, 10:59
       */

      /**
       *
       * @author bchapman
       */
      public class StringEqualityTest extends TestCase {
          
          public StringEqualityTest(String testName) {
              super(testName);
          }

          public void testStringEquality() throws ScriptException {
              ScriptEngineManager manager = new ScriptEngineManager();
              ScriptEngine engine = manager.getEngineByName("JavaScript");
              String alpha = "ABCD";
              String beta = new String(alpha);
              assertTrue(alpha.equals(beta));
              assertFalse(alpha == beta);
              engine.put("alpha",alpha);
              engine.put("beta", beta);
              engine.put("list", Arrays.asList(alpha,beta,beta.intern()));
              engine.eval("var eq1 = (alpha == beta);");
              engine.eval("alpha = list.get(0); beta = list.get(1);");
              engine.eval("var eq2 = (alpha == list.get(2));");
              engine.eval("var eq3 = alpha.equals(beta);");
              engine.eval("var eq4 = (alpha == beta);");
              assertTrue((Boolean)engine.get("eq1")); // == when non identical strings via engine.put()
              assertTrue((Boolean)engine.get("eq2")); // == when java returns identical Strings
              assertTrue((Boolean)engine.get("eq3")); // .equal() when java (List) returns non identical Strings
              assertTrue((Boolean)engine.get("eq4")); // fails == when java (List) returns non identical Strings
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      call intern() on any String return values from java methods that are exposed to javascript scripts.

        Attachments

          Activity

            People

            • Assignee:
              sundar Sundararajan Athijegannathan
              Reporter:
              ryeung Roger Yeung (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: