use of org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty in project groovy by apache.
the class MetaClassImpl method getEffectiveGetMetaProperty.
public MetaProperty getEffectiveGetMetaProperty(final Class sender, final Object object, String name, final boolean useSuper) {
//----------------------------------------------------------------------
// handling of static
//----------------------------------------------------------------------
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
return new MetaProperty(name, Object.class) {
final MetaClass mc = registry.getMetaClass((Class) object);
public Object getProperty(Object object) {
return mc.getProperty(sender, object, name, useSuper, false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
checkInitalised();
//----------------------------------------------------------------------
if (!isStatic && this.isMap) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return ((Map) object).get(name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
MetaMethod method = null;
//----------------------------------------------------------------------
// getter
//----------------------------------------------------------------------
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
method = mbp.getGetter();
mp = mbp.getField();
}
}
// check for a category method named like a getter
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
String getterName = GroovyCategorySupport.getPropertyCategoryGetterName(name);
if (getterName != null) {
MetaMethod categoryMethod = getCategoryMethodGetter(theClass, getterName, false);
if (categoryMethod != null)
method = categoryMethod;
}
}
//----------------------------------------------------------------------
if (method != null)
return new GetBeanMethodMetaProperty(name, method);
if (mp != null) {
return mp;
// try {
// return mp.getProperty(object);
// } catch (IllegalArgumentException e) {
// // can't access the field directly but there may be a getter
// mp = null;
// }
}
// check for a generic get method provided through a category
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
method = getCategoryMethodGetter(sender, "get", true);
if (method != null)
return new GetMethodMetaProperty(name, method);
}
// if it is not static and we do no static access
if (genericGetMethod != null && !(!genericGetMethod.isStatic() && isStatic)) {
method = genericGetMethod;
if (method != null)
return new GetMethodMetaProperty(name, method);
}
/** todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
MetaClass mc = registry.getMetaClass(Class.class);
return mc.getProperty(Class.class, object, name, useSuper, false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Collection) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt((Collection) object, name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Object[]) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else {
MetaMethod addListenerMethod = (MetaMethod) listeners.get(name);
if (addListenerMethod != null) {
//TODO: one day we could try return the previously registered Closure listener for easy removal
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return null;
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
}
//----------------------------------------------------------------------
if (isStatic || object instanceof Class)
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeStaticMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
else
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
use of org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty in project groovy-core by groovy.
the class MetaClassImpl method getEffectiveGetMetaProperty.
public MetaProperty getEffectiveGetMetaProperty(final Class sender, final Object object, String name, final boolean useSuper) {
//----------------------------------------------------------------------
// handling of static
//----------------------------------------------------------------------
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
return new MetaProperty(name, Object.class) {
final MetaClass mc = registry.getMetaClass((Class) object);
public Object getProperty(Object object) {
return mc.getProperty(sender, object, name, useSuper, false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
checkInitalised();
//----------------------------------------------------------------------
if (!isStatic && this.isMap) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return ((Map) object).get(name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
MetaMethod method = null;
//----------------------------------------------------------------------
// getter
//----------------------------------------------------------------------
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
method = mbp.getGetter();
mp = mbp.getField();
}
}
// check for a category method named like a getter
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
String getterName = GroovyCategorySupport.getPropertyCategoryGetterName(name);
if (getterName != null) {
MetaMethod categoryMethod = getCategoryMethodGetter(theClass, getterName, false);
if (categoryMethod != null)
method = categoryMethod;
}
}
//----------------------------------------------------------------------
if (method != null)
return new GetBeanMethodMetaProperty(name, method);
if (mp != null) {
return mp;
// try {
// return mp.getProperty(object);
// } catch (IllegalArgumentException e) {
// // can't access the field directly but there may be a getter
// mp = null;
// }
}
// check for a generic get method provided through a category
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
method = getCategoryMethodGetter(sender, "get", true);
if (method != null)
return new GetMethodMetaProperty(name, method);
}
// if it is not static and we do no static access
if (genericGetMethod != null && !(!genericGetMethod.isStatic() && isStatic)) {
method = genericGetMethod;
if (method != null)
return new GetMethodMetaProperty(name, method);
}
/** todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
MetaClass mc = registry.getMetaClass(Class.class);
return mc.getProperty(Class.class, object, name, useSuper, false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Collection) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt((Collection) object, name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Object[]) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else {
MetaMethod addListenerMethod = (MetaMethod) listeners.get(name);
if (addListenerMethod != null) {
//TODO: one day we could try return the previously registered Closure listener for easy removal
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return null;
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
}
//----------------------------------------------------------------------
if (isStatic || object instanceof Class)
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeStaticMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
else
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
Aggregations