1 46 package groovy.lang; 47 48 import java.util.HashMap ; 49 import java.util.Map ; 50 51 59 public class Binding extends GroovyObjectSupport { 60 private Map variables; 61 62 public Binding() { 63 variables = new HashMap (); 64 } 65 66 public Binding(Map variables) { 67 this.variables = variables; 68 } 69 70 75 public Binding(String [] args) { 76 this(); 77 setVariable("args", args); 78 } 79 80 84 public Object getVariable(String name) { 85 Object result = variables.get(name); 86 87 if (result == null && !variables.containsKey(name)) { 88 throw new MissingPropertyException("The property '" + name + "' is missing from the binding.", 89 name, Binding.class); 90 } 91 92 return result; 93 } 94 95 100 public void setVariable(String name, Object value) { 101 variables.put(name, value); 102 } 103 104 public Map getVariables() { 105 return variables; 106 } 107 108 111 public Object getProperty(String property) { 112 113 try { 114 return super.getProperty(property); 115 } 116 catch (MissingPropertyException e) { 117 return getVariable(property); 118 } 119 } 120 121 124 public void setProperty(String property, Object newValue) { 125 126 try { 127 super.setProperty(property, newValue); 128 } 129 catch (MissingPropertyException e) { 130 setVariable(property, newValue); 131 } 132 } 133 134 } 135 | Popular Tags |