Search in sources :

Example 1 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy by apache.

the class MetaClassImpl method applyStrayPropertyMethods.

private void applyStrayPropertyMethods(LinkedList<CachedClass> superClasses, Index classPropertyIndex, boolean isThis) {
    // now look for any stray getters that may be used to define a property
    for (CachedClass klass : superClasses) {
        MetaMethodIndex.Header header = metaMethodIndex.getHeader(klass.getTheClass());
        SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
        for (MetaMethodIndex.Entry e = header.head; e != null; e = e.nextClassEntry) {
            String methodName = e.name;
            // name too short?
            if (methodName.length() < 3 || (!methodName.startsWith("is") && methodName.length() < 4))
                continue;
            // possible getter/setter?
            boolean isGetter = methodName.startsWith("get") || methodName.startsWith("is");
            boolean isBooleanGetter = methodName.startsWith("is");
            boolean isSetter = methodName.startsWith("set");
            if (!isGetter && !isSetter)
                continue;
            Object propertyMethods = filterPropertyMethod(isThis ? e.methods : e.methodsForSuper, isGetter, isBooleanGetter);
            if (propertyMethods == null)
                continue;
            String propName = getPropName(methodName);
            if (propertyMethods instanceof MetaMethod) {
                createMetaBeanProperty(propertyIndex, propName, isGetter, (MetaMethod) propertyMethods);
            } else {
                LinkedList<MetaMethod> methods = (LinkedList<MetaMethod>) propertyMethods;
                for (MetaMethod m : methods) {
                    createMetaBeanProperty(propertyIndex, propName, isGetter, m);
                }
            }
        }
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) 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) MetaMethodIndex(org.codehaus.groovy.runtime.metaclass.MetaMethodIndex) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 2 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy by apache.

the class MetaClassImpl method getMetaProperty.

private MetaProperty getMetaProperty(String name, boolean useStatic) {
    CachedClass clazz = theCachedClass;
    SingleKeyHashMap propertyMap;
    if (useStatic) {
        propertyMap = staticPropertyIndex;
    } else {
        propertyMap = classPropertyIndex.getNullable(clazz);
    }
    if (propertyMap == null) {
        return null;
    }
    return (MetaProperty) propertyMap.get(name);
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)

Example 3 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy by apache.

the class MetaClassImpl method copyNonPrivateFields.

private static void copyNonPrivateFields(SingleKeyHashMap from, SingleKeyHashMap to, CachedClass klass) {
    for (ComplexKeyHashMap.EntryIterator iter = from.getEntrySetIterator(); iter.hasNext(); ) {
        SingleKeyHashMap.Entry entry = (SingleKeyHashMap.Entry) iter.next();
        CachedField mfp = (CachedField) entry.getValue();
        if (!inheritedOrPublic(mfp) && !packageLocal(mfp, klass))
            continue;
        to.put(entry.getKey(), mfp);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 4 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy by apache.

the class MetaClassImpl method setupProperties.

/**
     * This will build up the property map (Map of MetaProperty objects, keyed on
     * property name).
     *
     * @param propertyDescriptors
     */
@SuppressWarnings("unchecked")
private void setupProperties(PropertyDescriptor[] propertyDescriptors) {
    if (theCachedClass.isInterface) {
        LinkedList<CachedClass> superClasses = new LinkedList<CachedClass>();
        superClasses.add(ReflectionCache.OBJECT_CLASS);
        Set interfaces = theCachedClass.getInterfaces();
        LinkedList<CachedClass> superInterfaces = new LinkedList<CachedClass>(interfaces);
        // ambiguous fields (class implementing two interfaces using the same field)
        if (superInterfaces.size() > 1) {
            Collections.sort(superInterfaces, CACHED_CLASS_NAME_COMPARATOR);
        }
        SingleKeyHashMap iPropertyIndex = classPropertyIndex.getNotNull(theCachedClass);
        for (CachedClass iclass : superInterfaces) {
            SingleKeyHashMap sPropertyIndex = classPropertyIndex.getNotNull(iclass);
            copyNonPrivateFields(sPropertyIndex, iPropertyIndex);
            addFields(iclass, iPropertyIndex);
        }
        addFields(theCachedClass, iPropertyIndex);
        applyPropertyDescriptors(propertyDescriptors);
        applyStrayPropertyMethods(superClasses, classPropertyIndex, true);
        makeStaticPropertyIndex();
    } else {
        LinkedList<CachedClass> superClasses = getSuperClasses();
        LinkedList<CachedClass> interfaces = new LinkedList<CachedClass>(theCachedClass.getInterfaces());
        // ambiguous fields (class implementing two interfaces using the same field)
        if (interfaces.size() > 1) {
            Collections.sort(interfaces, CACHED_CLASS_NAME_COMPARATOR);
        }
        // if this an Array, then add the special read-only "length" property
        if (theCachedClass.isArray) {
            SingleKeyHashMap map = new SingleKeyHashMap();
            map.put("length", arrayLengthProperty);
            classPropertyIndex.put(theCachedClass, map);
        }
        inheritStaticInterfaceFields(superClasses, new LinkedHashSet(interfaces));
        inheritFields(superClasses);
        applyPropertyDescriptors(propertyDescriptors);
        applyStrayPropertyMethods(superClasses, classPropertyIndex, true);
        applyStrayPropertyMethods(superClasses, classPropertyIndexForSuper, false);
        copyClassPropertyIndexForSuper(classPropertyIndexForSuper);
        makeStaticPropertyIndex();
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 5 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy-core by groovy.

the class MetaClassImpl method applyStrayPropertyMethods.

private void applyStrayPropertyMethods(LinkedList<CachedClass> superClasses, Index classPropertyIndex, boolean isThis) {
    // now look for any stray getters that may be used to define a property
    for (CachedClass klass : superClasses) {
        MetaMethodIndex.Header header = metaMethodIndex.getHeader(klass.getTheClass());
        SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
        for (MetaMethodIndex.Entry e = header.head; e != null; e = e.nextClassEntry) {
            String methodName = e.name;
            // name too short?
            if (methodName.length() < 3 || (!methodName.startsWith("is") && methodName.length() < 4))
                continue;
            // possible getter/setter?
            boolean isGetter = methodName.startsWith("get") || methodName.startsWith("is");
            boolean isBooleanGetter = methodName.startsWith("is");
            boolean isSetter = methodName.startsWith("set");
            if (!isGetter && !isSetter)
                continue;
            Object propertyMethods = filterPropertyMethod(isThis ? e.methods : e.methodsForSuper, isGetter, isBooleanGetter);
            if (propertyMethods == null)
                continue;
            String propName = getPropName(methodName);
            if (propertyMethods instanceof MetaMethod) {
                createMetaBeanProperty(propertyIndex, propName, isGetter, (MetaMethod) propertyMethods);
            } else {
                LinkedList<MetaMethod> methods = (LinkedList<MetaMethod>) propertyMethods;
                for (MetaMethod m : methods) {
                    createMetaBeanProperty(propertyIndex, propName, isGetter, m);
                }
            }
        }
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) 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) MetaMethodIndex(org.codehaus.groovy.runtime.metaclass.MetaMethodIndex) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)20 CachedClass (org.codehaus.groovy.reflection.CachedClass)10 CachedField (org.codehaus.groovy.reflection.CachedField)8 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)8 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)8 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)8 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)4 MultipleSetterProperty (org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MetaMethodIndex (org.codehaus.groovy.runtime.metaclass.MetaMethodIndex)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)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