< prev index next >

src/java.scripting/share/classes/javax/script/SimpleScriptContext.java

Print this page

        

@@ -138,10 +138,11 @@
      * null if no attribute with the name exists in any scope.
      * @throws NullPointerException if the name is null.
      * @throws IllegalArgumentException if the name is empty.
      */
     public Object getAttribute(String name) {
+        checkName(name);
         if (engineScope.containsKey(name)) {
             return getAttribute(name, ENGINE_SCOPE);
         } else if (globalScope != null && globalScope.containsKey(name)) {
             return getAttribute(name, GLOBAL_SCOPE);
         }

@@ -160,11 +161,11 @@
      * @throws IllegalArgumentException
      *         if the name is empty or if the value of scope is invalid.
      * @throws NullPointerException if the name is null.
      */
     public Object getAttribute(String name, int scope) {
-
+        checkName(name);
         switch (scope) {
 
             case ENGINE_SCOPE:
                 return engineScope.get(name);
 

@@ -189,11 +190,11 @@
      * @throws IllegalArgumentException
      *         if the name is empty or if the scope is invalid.
      * @throws NullPointerException if the name is null.
      */
     public Object removeAttribute(String name, int scope) {
-
+        checkName(name);
         switch (scope) {
 
             case ENGINE_SCOPE:
                 if (getBindings(ENGINE_SCOPE) != null) {
                     return getBindings(ENGINE_SCOPE).remove(name);

@@ -221,11 +222,11 @@
      * @throws IllegalArgumentException
      *         if the name is empty or if the scope is invalid.
      * @throws NullPointerException if the name is null.
      */
     public void setAttribute(String name, Object value, int scope) {
-
+        checkName(name);
         switch (scope) {
 
             case ENGINE_SCOPE:
                 engineScope.put(name, value);
                 return;

@@ -279,10 +280,11 @@
      * name is defined in any scope.
      * @throws NullPointerException if name is null.
      * @throws IllegalArgumentException if name is empty.
      */
     public int getAttributesScope(String name) {
+        checkName(name);
         if (engineScope.containsKey(name)) {
             return ENGINE_SCOPE;
         } else if (globalScope != null && globalScope.containsKey(name)) {
             return GLOBAL_SCOPE;
         } else {

@@ -312,10 +314,17 @@
     /** {@inheritDoc} */
     public List<Integer> getScopes() {
         return scopes;
     }
 
+    private void checkName(String name) {
+        Objects.requireNonNull(name);
+        if (name.isEmpty()) {
+            throw new IllegalArgumentException("name cannot be empty");
+        }
+    }
+
     private static List<Integer> scopes;
     static {
         scopes = new ArrayList<Integer>(2);
         scopes.add(ENGINE_SCOPE);
         scopes.add(GLOBAL_SCOPE);
< prev index next >