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

javax.script.ScriptContext setAttribute method should clarify behavior when GLOBAL_SCOPE is used and global scope object is null

    XMLWordPrintable

    Details

      Description

      Spec
      http://docs.oracle.com/javase/8/docs/api/javax/script/ScriptContext.html#getAttribute-java.lang.String-
      says "Returns null if no attribute with the name exists in any scope."

      Spec
      http://docs.oracle.com/javase/8/docs/api/javax/script/ScriptContext.html#getAttributesScope-java.lang.String-
      says: "Returns: The lowest scope. Returns -1 if no attribute with the given name is defined in any scope."


      However for GLOBAL_SCOPE setting attribute value leads to inability to get the value which was set and the scope which was used.

      Consider the following code:

              new ScriptEngineManager()
                  .getEngineFactories()
                  .stream()
                  .map(ScriptEngineFactory::getScriptEngine)
                  .map((engine) -> {
                      System.err.println("engine: " + engine);
                      return engine.getContext();
                  })
                  .forEach(c -> {
                      c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE);
                      System.err.println("value = " + c.getAttribute("name1234"));
                      System.err.println("scope = " + c.getAttributesScope("name1234"));
                  });

      The output will contain

      engine: jdk.nashorn.api.scripting.NashornScriptEngine@f3eef9
      value = null
      scope = -1

      The following JCK9 test will fail due to this issue:

      api/javax_script/ScriptContext/index.html#SetGetAttribute[setGet]
      api/javax_script/ScriptContext/index.html#SetGetAttribute[getAttributesScope]


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sundar Sundararajan Athijegannathan
              Reporter:
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: