@Override public Object getProperty(Object object) { MetaMethod getter = getGetter(); if (getter == null) { if (field != null) return field.getProperty(object); throw new GroovyRuntimeException("Cannot read write-only property: " + name); } return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY); }
public MetaProperty createStaticVersion() { boolean mf = field==null || field.isStatic(); boolean mg = getter==null || getter.isStatic(); if (mf && mg) return this; if (mg) { MultipleSetterProperty newMsp = new MultipleSetterProperty(name); newMsp.setGetter(getter); return newMsp; } else if (mf) { MultipleSetterProperty newMsp = new MultipleSetterProperty(name); newMsp.setField(field); return newMsp; } return null; } }
/** * 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}); }
public synchronized void setStaticMetaClassField(MetaClass mc) { if (staticMetaClassField == NONE) return; if (staticMetaClassField == null) { final CachedField[] cachedFields = getFields(); for (int i = 0; i < cachedFields.length; i++) { CachedField cachedField = cachedFields[i]; if (cachedField.getName().startsWith("$staticMetaClass") && cachedField.getType() == MetaClass.class && cachedField.isStatic()) { staticMetaClassField = cachedField; break; } } } if (staticMetaClassField == null) { staticMetaClassField = NONE; return; } ((CachedField)staticMetaClassField).setProperty(null,mc); }
private static boolean inheritedOrPublic(CachedField mfp) { return Modifier.isPublic(mfp.getModifiers()) || Modifier.isProtected(mfp.getModifiers()); }
private synchronized void initAttributes() { if (!attributes.isEmpty()) return; attributes.put("!", null); // just a dummy for later CachedField[] fieldArray = theCachedClass.getFields(); for (CachedField aFieldArray : fieldArray) { attributes.put(aFieldArray.getName(), aFieldArray); } attributeInitDone = !attributes.isEmpty(); }
@Override public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) { if (object instanceof Class) { getStaticMetaClass().setAttribute(sender, object, attribute, newValue, useSuper, fromInsideClass); } else { if (!attributeInitDone) initAttributes(); CachedField mfp = attributes.get(attribute); if (mfp == null) { CLOSURE_METACLASS.setAttribute(sender, object, attribute, newValue, useSuper, fromInsideClass); } else { mfp.setProperty(object, newValue); } } }
MetaBeanProperty mbp = new MetaBeanProperty(propName, mfp.getType(), isGetter? propertyMethod: null, isGetter? null: propertyMethod);
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); }
throw setReadOnlyProperty(name); value = propertySetTransformer.transformValue(metaBeanProperty.getField().getType(), value); metaBeanProperty.getField().setProperty(bean, value); } else {
public CachedField[] initValue() { final Field[] declaredFields = (Field[]) AccessController.doPrivileged(new PrivilegedAction<Field[]>() { public Field[] run() { Field[] df = getTheClass().getDeclaredFields(); df = (Field[]) makeAccessible(df); return df; } }); CachedField [] fields = new CachedField[declaredFields.length]; for (int i = 0; i != fields.length; ++i) fields[i] = new CachedField(declaredFields[i]); return fields; } };
/** * 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); } } }
public boolean isStatic() { return Modifier.isStatic(getModifiers()); }
private static void addFields(final CachedClass klass, SingleKeyHashMap propertyIndex) { CachedField[] fields = klass.getFields(); for (CachedField field : fields) { propertyIndex.put(field.getName(), field); } }
public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) { if (object instanceof Class) { getStaticMetaClass().setAttribute(sender, object, attribute, newValue, useSuper, fromInsideClass); } else { if (!attributeInitDone) initAttributes(); CachedField mfp = (CachedField) attributes.get(attribute); if (mfp == null) { CLOSURE_METACLASS.setAttribute(sender, object, attribute, newValue, useSuper, fromInsideClass); } else { mfp.setProperty(object, newValue); } } }
mfp = (CachedField) mp; mbp = new MetaBeanProperty(propName, mfp.getType(), null, null); } else {
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 synchronized CachedField[] getFields() { if (fields == null) { final Field[] declaredFields = (Field[]) AccessController.doPrivileged(new PrivilegedAction/*<Field[]>*/() { public /*Field[]*/ Object run() { return getTheClass().getDeclaredFields(); } }); fields = new CachedField[declaredFields.length]; for (int i = 0; i != fields.length; ++i) fields[i] = new CachedField(this, declaredFields[i]); } return fields; }
/** * 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); } } }
private void makeStaticPropertyIndex() { SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass); for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext();) { SingleKeyHashMap.Entry entry = ((SingleKeyHashMap.Entry) iter.next()); MetaProperty mp = (MetaProperty) entry.getValue(); if (mp instanceof CachedField) { CachedField mfp = (CachedField) mp; if (!mfp.isStatic()) continue; } else if (mp instanceof MetaBeanProperty) { MetaProperty result = establishStaticMetaProperty(mp); if (result == null) continue; else { mp = result; } } else if (mp instanceof MultipleSetterProperty) { MultipleSetterProperty msp = (MultipleSetterProperty) mp; mp = msp.createStaticVersion(); } else { continue; // ignore all other types } staticPropertyIndex.put(entry.getKey(), mp); } }