Search in sources :

Example 1 with ModelSkip

use of com.disney.groovity.model.ModelSkip in project groovity by disney.

the class MetaPropertyLookup method getOrderedGettableProperties.

public static final MetaProperty[] getOrderedGettableProperties(Object o) {
    final Class<?> c = o.getClass();
    MetaProperty[] properties = orderedPropertiesCache.get(c);
    if (properties == null) {
        MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(c);
        List<MetaProperty> mps = mc.getProperties();
        TreeMap<String, MetaProperty> sortedProps;
        String[] declaredOrder = null;
        ModelOrder order = c.getAnnotation(ModelOrder.class);
        if (order != null) {
            declaredOrder = order.value();
        } else {
            XmlType xmlType = c.getAnnotation(XmlType.class);
            if (xmlType != null) {
                declaredOrder = xmlType.propOrder();
            }
        }
        if (declaredOrder != null && declaredOrder.length > 0) {
            final String[] declared = declaredOrder;
            final int dl = declared.length;
            Comparator<String> comp = new Comparator<String>() {

                @Override
                public int compare(String o1, String o2) {
                    int p1 = dl;
                    int p2 = dl;
                    for (int i = 0; i < dl; i++) {
                        String d = declared[i];
                        if (o1.equals(d)) {
                            p1 = i;
                        }
                        if (o2.equals(d)) {
                            p2 = i;
                        }
                        if (p1 < dl && p2 < dl) {
                            break;
                        }
                    }
                    if (p1 == p2) {
                        return o1.compareTo(o2);
                    }
                    return p1 - p2;
                }
            };
            sortedProps = new TreeMap<>(comp);
        } else {
            sortedProps = new TreeMap<>();
        }
        List<String> skipProperties = new ArrayList<>();
        skipProperties.add("class");
        skipProperties.add("binding");
        if (Map.class.isAssignableFrom(c) || Collection.class.isAssignableFrom(c)) {
            skipProperties.add("empty");
            if (NavigableMap.class.isAssignableFrom(c)) {
                skipProperties.add("firstEntry");
                skipProperties.add("lastEntry");
            }
        }
        for (MetaProperty mp : mps) {
            if (!skipProperties.contains(mp.getName()) && !Closeable.class.isAssignableFrom(mp.getType())) {
                // now check for skip annotations
                if (mp instanceof MetaBeanProperty) {
                    MetaBeanProperty mbp = ((MetaBeanProperty) mp);
                    if (mbp.getField() != null) {
                        ModelSkip skipAnn = mbp.getField().field.getAnnotation(ModelSkip.class);
                        if (skipAnn != null) {
                            continue;
                        }
                        if (mbp.getField().isStatic()) {
                            continue;
                        }
                    }
                    MetaMethod getter = mbp.getGetter();
                    if (getter instanceof CachedMethod) {
                        CachedMethod cm = (CachedMethod) getter;
                        ModelSkip skipAnn = cm.getCachedMethod().getAnnotation(ModelSkip.class);
                        if (skipAnn != null) {
                            continue;
                        }
                        if (cm.isStatic()) {
                            continue;
                        }
                        if (!cm.getCachedMethod().getDeclaringClass().equals(c)) {
                            // we may have an overridden method here
                            try {
                                Method override = c.getDeclaredMethod(cm.getCachedMethod().getName(), cm.getCachedMethod().getParameterTypes());
                                if (override.getAnnotation(ModelSkip.class) != null) {
                                    continue;
                                }
                            } catch (NoSuchMethodException | SecurityException e) {
                            }
                        }
                    }
                    if (mbp.getGetter() == null && mbp.getField() == null) {
                        continue;
                    }
                }
                sortedProps.put(mp.getName(), mp);
            }
        }
        properties = sortedProps.values().toArray(new MetaProperty[0]);
        orderedPropertiesCache.putIfAbsent(c, properties);
    }
    return properties;
}
Also used : ArrayList(java.util.ArrayList) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) Comparator(java.util.Comparator) MethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty) MetaProperty(groovy.lang.MetaProperty) MetaMethod(groovy.lang.MetaMethod) MetaBeanProperty(groovy.lang.MetaBeanProperty) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) MetaMethod(groovy.lang.MetaMethod) Method(java.lang.reflect.Method) ModelOrder(com.disney.groovity.model.ModelOrder) XmlType(javax.xml.bind.annotation.XmlType) ModelSkip(com.disney.groovity.model.ModelSkip) MetaClass(groovy.lang.MetaClass) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

ModelOrder (com.disney.groovity.model.ModelOrder)1 ModelSkip (com.disney.groovity.model.ModelSkip)1 MetaBeanProperty (groovy.lang.MetaBeanProperty)1 MetaClass (groovy.lang.MetaClass)1 MetaMethod (groovy.lang.MetaMethod)1 MetaProperty (groovy.lang.MetaProperty)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 XmlType (javax.xml.bind.annotation.XmlType)1 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)1 MethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty)1