GroupStrategy getGroupStrategy()

in bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/MetadataReader.java [221:283]


        GroupStrategy getGroupStrategy() {
            final Class<T> host = meta.getHost();
            if (host.isInterface()) {
                return validatorFactory.getGroupsComputer().computeGroups(host).asStrategy();
            }
            final GroupStrategy parentStrategy = Optional.ofNullable(host.getSuperclass()).filter(JDK.negate())
                .map(validatorFactory.getDescriptorManager()::getBeanDescriptor).map(BeanD.class::cast)
                .map(BeanD::getGroupStrategy).orElse(null);

            final List<Class<?>> groupSequence = builder.getGroupSequence(meta);

            final Set<Group> parentGroups = parentStrategy == null ? null : parentStrategy.getGroups();

            Group localGroup = Group.of(host);
            if (groupSequence == null) {
                final Set<Group> groups = new HashSet<>();
                groups.add(localGroup);

                for (Class<?> t : Reflection.hierarchy(host, Interfaces.INCLUDE)) {
                    if (JDK.test(t)) {
                        continue;
                    }
                    if (!t.isInterface()) {
                        continue;
                    }
                    if (AnnotationsManager.isAnnotationDirectlyPresent(t, GroupSequence.class)) {
                        continue;
                    }
                    final Group g = Group.of(t);
                    if (parentGroups != null && parentGroups.contains(g)) {
                        continue;
                    }
                    groups.add(g);
                }
                final GroupStrategy strategy = GroupStrategy.simple(groups);
                return parentStrategy == null ? strategy : GroupStrategy.composite(strategy, parentStrategy);
            }
            if (groupSequence.contains(Default.class)) {
                Exceptions.raise(GroupDefinitionException::new, "@%s for %s must not contain %s",
                    GroupSequence.class.getSimpleName(), host, Default.class.getName());
            }
            if (!groupSequence.contains(host)) {
                Exceptions.raise(GroupDefinitionException::new, "@%s for %s must contain %<s",
                    GroupSequence.class.getSimpleName(), host);
            }
            final Group.Sequence result =
                Group.sequence(groupSequence.stream().map(Group::of).collect(Collectors.toList()));

            final Deque<Group> expanded = new ArrayDeque<>();
            for (Class<?> t : Reflection.hierarchy(host, Interfaces.INCLUDE)) {
                if (JDK.test(t)) {
                    continue;
                }
                if (t.isInterface() && AnnotationsManager.isAnnotationDirectlyPresent(t, GroupSequence.class)) {
                    continue;
                }
                expanded.push(Group.of(t));
            }
            if (expanded.size() == 1) {
                return result;
            }
            return result.redefining(Collections.singletonMap(localGroup, GroupStrategy.simple(expanded)));
        }