Search in sources :

Example 1 with Exclude

use of com.vaadin.flow.templatemodel.Exclude in project flow by vaadin.

the class TemplateModelUtil method getFilterFromIncludeExclude.

/**
 * Gets a filter based on any <code>@Include</code> and/or
 * <code>@Exclude</code> annotations present on the given method.
 *
 * @param method
 *            the method to check
 * @return a filter based on the given annotations
 */
public static Predicate<String> getFilterFromIncludeExclude(Method method) {
    Exclude exclude = method.getAnnotation(Exclude.class);
    Include include = method.getAnnotation(Include.class);
    Set<String> toExclude = new HashSet<>();
    Set<String> toInclude = new HashSet<>();
    if (exclude != null) {
        Collections.addAll(toExclude, exclude.value());
    }
    if (include != null) {
        for (String includeProperty : include.value()) {
            toInclude.add(includeProperty);
            // If "some.bean.value" is included,
            // we should automatically include "some" and "some.bean"
            String property = includeProperty;
            int dotLocation = property.lastIndexOf('.');
            while (dotLocation != -1) {
                property = property.substring(0, dotLocation);
                toInclude.add(property);
                dotLocation = property.lastIndexOf('.');
            }
        }
    }
    return propertyName -> {
        if (toExclude.contains(propertyName)) {
            return false;
        }
        if (!toInclude.isEmpty()) {
            return toInclude.contains(propertyName);
        }
        return true;
    };
}
Also used : HashSet(java.util.HashSet) ModelType(com.vaadin.flow.templatemodel.ModelType) StateNode(com.vaadin.flow.internal.StateNode) Predicate(java.util.function.Predicate) BiFunction(java.util.function.BiFunction) Exclude(com.vaadin.flow.templatemodel.Exclude) Set(java.util.Set) ModelList(com.vaadin.flow.internal.nodefeature.ModelList) Method(java.lang.reflect.Method) Collections(java.util.Collections) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Include(com.vaadin.flow.templatemodel.Include) Exclude(com.vaadin.flow.templatemodel.Exclude) Include(com.vaadin.flow.templatemodel.Include) HashSet(java.util.HashSet)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)1 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)1 Exclude (com.vaadin.flow.templatemodel.Exclude)1 Include (com.vaadin.flow.templatemodel.Include)1 ModelType (com.vaadin.flow.templatemodel.ModelType)1 Method (java.lang.reflect.Method)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 Predicate (java.util.function.Predicate)1