Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2008 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package groovy.lang;
 
 import java.util.Map;
Represents the variable bindings of a script which can be altered from outside the script object or created outside of a script and passed into it.

Author(s):
James Strachan
Version:
$Revision: 14401 $
 
 public class Binding extends GroovyObjectSupport implements Serializable {
     private Map variables;
 
     public Binding() {
     }
 
     public Binding(Map variables) {
         this. = variables;
     }

    
A helper constructor used in main(String[]) method calls

Parameters:
args are the command line arguments from a main()
 
     public Binding(String[] args) {
         this();
         setVariable("args"args);
     }

    

Parameters:
name the name of the variable to lookup
Returns:
the variable value
 
     public Object getVariable(String name) {
         if ( == null)
             throw new MissingPropertyException(namethis.getClass());
 
         Object result = .get(name);
 
         if (result == null && !.containsKey(name)) {
             throw new MissingPropertyException(namethis.getClass());
         }
 
         return result;
     }

    
Sets the value of the given variable

Parameters:
name the name of the variable to set
value the new value for the given variable
 
     public void setVariable(String nameObject value) {
         if ( == null)
              = new LinkedHashMap();
         .put(namevalue);
     }
 
     public Map getVariables() {
         if ( == null)
              = new LinkedHashMap();
         return ;
     }

    
Overloaded to make variables appear as bean properties or via the subscript operator
 
     public Object getProperty(String property) {
        

Todo:
we should check if we have the property with the metaClass instead of try/catch
 
         try {
             return super.getProperty(property);
         }
         catch (MissingPropertyException e) {
             return getVariable(property);
        }
    }

    
Overloaded to make variables appear as bean properties or via the subscript operator
    public void setProperty(String propertyObject newValue) {
        

Todo:
we should check if we have the property with the metaClass instead of try/catch
        try {
            super.setProperty(propertynewValue);
        }
        catch (MissingPropertyException e) {
            setVariable(propertynewValue);
        }
    }
  private void writeObject(ObjectOutputStream oosthrows IOException {
      if(!=null) {
          for (Entry e : (Collection<Entry>).entrySet()) {
              Object v = e.getValue();
              if(v instanceof Serializable) {
                  oos.writeObject(e.getKey());
                  oos.writeObject(v);
              }
          }
      }
      oos.writeObject(null);
  }
  private void readObject(ObjectInputStream oisthrows IOExceptionClassNotFoundException {
      while(true) {
          Object key = ois.readObject();
          if(key==nullreturn;
          if(==null)
               = new LinkedHashMap();
          .put(key,ois.readObject());
      }
  }    
New to GrepCode? Check out our FAQ X