public Object getProperty()

in src/main/java/groovy/lang/MetaClassImpl.java [1822:1932]


    public Object getProperty(final Class sender, final Object object, final String name, final boolean useSuper, final boolean fromInsideClass) {

        //----------------------------------------------------------------------
        // handling of static
        //----------------------------------------------------------------------
        boolean isStatic = (theClass != Class.class && object instanceof Class);
        if (isStatic && object != theClass) {
            MetaClass mc = registry.getMetaClass((Class<?>) object);
            return mc.getProperty(sender, object, name, useSuper, false);
        }

        checkInitalised();

        //----------------------------------------------------------------------
        // getter
        //----------------------------------------------------------------------
        Tuple2<MetaMethod, MetaProperty> methodAndProperty = createMetaMethodAndMetaProperty(sender, name, useSuper, isStatic);
        MetaMethod method = methodAndProperty.getV1();

        if (method == null || isSpecialProperty(name)) {
            //------------------------------------------------------------------
            // public field
            //------------------------------------------------------------------
            MetaProperty mp = methodAndProperty.getV2();
            if (mp != null && mp.isPublic()) {
                try {
                    return mp.getProperty(object);
                } catch (GroovyRuntimeException e) {
                    // can't access the field directly but there may be a getter
                    mp = null;
                }
            }

            //------------------------------------------------------------------
            // java.util.Map get method
            //------------------------------------------------------------------
            if (isMap && !isStatic) {
                return ((Map<?,?>) object).get(name);
            }

            //------------------------------------------------------------------
            // non-public field
            //------------------------------------------------------------------
            if (mp != null) {
                try {
                    return mp.getProperty(object);
                } catch (GroovyRuntimeException e) {
                }
            }
        }

        //----------------------------------------------------------------------
        // java.util.Map get method before non-public getter -- see GROOVY-11367
        //----------------------------------------------------------------------
        if (isMap && !isStatic && !method.isPublic()) {
            return ((Map<?,?>) object).get(name);
        }

        //----------------------------------------------------------------------
        // propertyMissing (via category) or generic get method
        //----------------------------------------------------------------------
        Object[] arguments = EMPTY_ARGUMENTS;
        if (method == null && !useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
            // check for propertyMissing provided through a category; TODO:should this have lower precedence?
            method = getCategoryMethodGetter(theClass, PROPERTY_MISSING, true);
            if (method == null) {
                // check for a generic get method provided through a category
                method = getCategoryMethodGetter(theClass, "get", true);
            }
            if (method != null) arguments = new Object[]{name};
        }
        if (method == null && genericGetMethod != null && (genericGetMethod.isStatic() || !isStatic)) {
            arguments = new Object[]{name};
            method = genericGetMethod;
        }

        if (method != null) {
            //------------------------------------------------------------------
            // executing the method
            //------------------------------------------------------------------
            MetaMethod metaMethod = VM_PLUGIN.transformMetaMethod(this, method);
            return metaMethod.doMethodInvoke(object, arguments);
        } else {
            //------------------------------------------------------------------
            // special cases -- maybe these cases should be special MetaClasses!
            //------------------------------------------------------------------
            if (isStatic) {
                MetaClass cmc = registry.getMetaClass(Class.class);
                return cmc.getProperty(Class.class, object, name, false, false);
            }
            if (object instanceof Collection) {
                return DefaultGroovyMethods.getAt((Collection<?>) object, name);
            }
            if (object instanceof Object[]) {
                return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
            }
            MetaMethod addListenerMethod = listeners.get(name);
            if (addListenerMethod != null) {
                // TODO: one day we could try return the previously registered Closure listener for easy removal
                return null;
            }
        }

        //----------------------------------------------------------------------
        // missing property protocol
        //----------------------------------------------------------------------
        if (object instanceof Class) {
            return invokeStaticMissingProperty(object, name, null, true);
        }
        return invokeMissingProperty(object, name, null, true);
    }