/** * Sets 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(final Object object, Object newValue) { AccessPermissionChecker.checkAccessPermission(field); final Object goalValue = DefaultTypeTransformation.castToType(newValue, field.getType()); if (isFinal()) { throw new GroovyRuntimeException("Cannot set the property '" + name + "' because the backing field is final."); } try { field.set(object, goalValue); } catch (IllegalAccessException ex) { throw new GroovyRuntimeException("Cannot set the property '" + name + "'.", ex); } } }
/** * Sets 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(final Object object, Object newValue) { final Object goalValue = DefaultTypeTransformation.castToType(newValue, field.getType()); if (isFinal()) { throw new GroovyRuntimeException("Cannot set the property '" + name + "' because the backing field is final."); } try { field.set(object, goalValue); } catch (IllegalAccessException ex) { throw new GroovyRuntimeException("Cannot set the property '" + name + "'.", ex); } } }
public static final MetaProperty getSettableMetaProperty(Object o, String name) { final Class<?> c = o.getClass(); ConcurrentHashMap<String, MetaProperty> properties = singlePropertyCache.get(c); if(properties == null) { MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(c); List<MetaProperty> mps = mc.getProperties(); properties = new ConcurrentHashMap<>(); for(MetaProperty mp: mps) { if(mp instanceof MetaBeanProperty) { MetaBeanProperty mbp = ((MetaBeanProperty)mp); if(mbp.getSetter()==null) { CachedField cf = mbp.getField(); if(cf==null || cf.isFinal() || cf.isStatic()) { continue; } } } else if(mp instanceof MethodMetaProperty) { continue; } properties.put(mp.getName(), mp); } singlePropertyCache.putIfAbsent(c, properties); } return properties.get(name); }
public static final MetaProperty getSettableMetaProperty(Object o, String name) { final Class<?> c = o.getClass(); ConcurrentHashMap<String, MetaProperty> properties = singlePropertyCache.get(c); if(properties == null) { MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(c); List<MetaProperty> mps = mc.getProperties(); properties = new ConcurrentHashMap<>(); for(MetaProperty mp: mps) { if(mp instanceof MetaBeanProperty) { MetaBeanProperty mbp = ((MetaBeanProperty)mp); if(mbp.getSetter()==null) { CachedField cf = mbp.getField(); if(cf==null || cf.isFinal() || cf.isStatic()) { continue; } } } else if(mp instanceof MethodMetaProperty) { continue; } properties.put(mp.getName(), mp); } singlePropertyCache.putIfAbsent(c, properties); } return properties.get(name); }