Search in sources :

Example 1 with GetMethodMetaProperty

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();
            }
        };
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

Example 2 with GetMethodMetaProperty

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();
            }
        };
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

Aggregations

CachedClass (org.codehaus.groovy.reflection.CachedClass)2 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)2 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)2 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1