Example #1
0
 /**
  * Get the property of the given object.
  *
  * @param object which to be got
  * @return the property of the given object
  * @throws RuntimeException if the property could not be evaluated
  */
 public Object getProperty(Object object) {
   MetaMethod getter = getGetter();
   if (getter == null) {
     if (field != null) return field.getProperty(object);
     // TODO: create a WriteOnlyException class?
     throw new GroovyRuntimeException("Cannot read write-only property: " + name);
   }
   return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY);
 }
Example #2
0
 /**
  * Set the property on the given object to the new value.
  *
  * @param object on which to set the property
  * @param newValue the new value of the property
  * @throws RuntimeException if the property could not be set
  */
 public void setProperty(Object object, Object newValue) {
   MetaMethod setter = getSetter();
   if (setter == null) {
     if (field != null && !Modifier.isFinal(field.getModifiers())) {
       field.setProperty(object, newValue);
       return;
     }
     throw new GroovyRuntimeException("Cannot set read-only property: " + name);
   }
   newValue = DefaultTypeTransformation.castToType(newValue, getType());
   setter.invoke(object, new Object[] {newValue});
 }