private void addGroups(List<Group> resolvedGroupSequence, List<Group> groups) { for (Group tmpGroup : groups) { if (resolvedGroupSequence.contains(tmpGroup) && resolvedGroupSequence.indexOf(tmpGroup) < resolvedGroupSequence.size() - 1) { throw new GroupDefinitionException("Unable to expand group sequence."); } resolvedGroupSequence.add(tmpGroup); } }
@Override public final GroupDefinitionException getCyclicDependencyInGroupsDefinitionException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getCyclicDependencyInGroupsDefinitionException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getUnableToExpandGroupSequenceException = "HV000048: Unable to expand group sequence.";
@Override public final GroupDefinitionException getSequenceDefinitionsNotAllowedException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getSequenceDefinitionsNotAllowedException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getCyclicDependencyInGroupsDefinitionException = "HV000047: Cyclic dependency in groups definition";
@Override public final GroupDefinitionException getSequenceDefinitionsNotAllowedException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getSequenceDefinitionsNotAllowedException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getCyclicDependencyInGroupsDefinitionException = "HV000047: Cyclic dependency in groups definition";
private void ensureDefaultGroupSequenceIsExpandable(List<Group> groupList, List<Group> defaultGroupList, int defaultGroupIndex) throws GroupDefinitionException { for (int i = 0; i < defaultGroupList.size(); i++) { Group group = defaultGroupList.get(i); if (group.getGroup().equals(Default.class)) { // we don't have to consider the default group since it is the one we want to replace continue; } // check whether the sequence contains group of the default group sequence int index = groupList.indexOf(group); if (index == -1) { continue; // if the group is not in the sequence we can continue } if ((i == 0 && index == defaultGroupIndex - 1) || (i == defaultGroupList.size() - 1 && index == defaultGroupIndex + 1)) { // if we are at the beginning or end of he defaultGroupSequence and the matches are either // directly before resp after we can continue as well, since we basically have two groups continue; } throw new GroupDefinitionException("Unable to expand default group list " + defaultGroupList + " into sequence " + groupList); } } }
@Override public final GroupDefinitionException getNoDefaultGroupInGroupSequenceException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getNoDefaultGroupInGroupSequenceException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getBeanClassMustBePartOfRedefinedDefaultGroupSequenceException = "HV000054: %s must be part of the redefined default group sequence.";
private List<Group> resolveSequence(Class<?> group, List<Class<?>> processedSequences) { if (processedSequences.contains(group)) { throw new GroupDefinitionException("Cyclic dependency in groups definition"); } else { processedSequences.add(group); } List<Group> resolvedGroupSequence = new ArrayList<Group>(); List<Class<?>> sequenceList = validationGroupsMetadata.getSequenceList(group); for (Class<?> clazz : sequenceList ) { if (isGroupSequence(clazz)) { List<Group> tmpSequence = resolveSequence(clazz, processedSequences); addGroups(resolvedGroupSequence, tmpSequence); } else { List<Group> list = new ArrayList<Group>(); list.add(new Group(clazz, group)); addGroups(resolvedGroupSequence, list); } } resolvedSequences.put(group, resolvedGroupSequence); return resolvedGroupSequence; } }
@Override public final GroupDefinitionException getUnableToExpandGroupSequenceException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getUnableToExpandGroupSequenceException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getInvalidDefaultGroupSequenceDefinitionException = "HV000052: Default group sequence and default group sequence provider cannot be defined at the same time.";
private void addInheritedGroups(Group group, List<Group> expandedGroups) { Set<Class<?>> inheritedGroups = validationGroupsMetadata.getParentsOfGroup(group.getGroup()); if (inheritedGroups != null) { for (Class<?> inheritedGroup : inheritedGroups) { if (isGroupSequence(inheritedGroup)) { throw new GroupDefinitionException("Sequence definitions are not allowed as composing " + "parts of a sequence."); } Group g = new Group(inheritedGroup, group.getSequence()); expandedGroups.add(g); addInheritedGroups(g, expandedGroups); } } }
@Override public final GroupDefinitionException getUnableToExpandGroupSequenceException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getUnableToExpandGroupSequenceException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getInvalidDefaultGroupSequenceDefinitionException = "HV000052: Default group sequence and default group sequence provider cannot be defined at the same time.";
private void addGroups(List<Group> resolvedGroupSequence, List<Group> groups) { for (Group tmpGroup : groups) { if (resolvedGroupSequence.contains(tmpGroup) && resolvedGroupSequence.indexOf(tmpGroup) < resolvedGroupSequence.size() - 1) { throw new GroupDefinitionException("Unable to expand group sequence."); } resolvedGroupSequence.add(tmpGroup); } }
@Override public final GroupDefinitionException getInvalidDefaultGroupSequenceDefinitionException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getInvalidDefaultGroupSequenceDefinitionException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getNoDefaultGroupInGroupSequenceException = "HV000053: 'Default.class' cannot appear in default group sequence list.";
private void addGroups(List<Group> resolvedGroupSequence, List<Group> groups) { for (Group tmpGroup : groups) { if (resolvedGroupSequence.contains(tmpGroup) && resolvedGroupSequence.indexOf(tmpGroup) < resolvedGroupSequence.size() - 1) { throw new GroupDefinitionException("Unable to expand group sequence."); } resolvedGroupSequence.add(tmpGroup); } }
@Override public final GroupDefinitionException getNoDefaultGroupInGroupSequenceException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getNoDefaultGroupInGroupSequenceException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getBeanClassMustBePartOfRedefinedDefaultGroupSequenceException = "HV000054: %s must be part of the redefined default group sequence.";
private void addGroups(final List<Group> resolvedGroupSequence, final List<Group> groups) { groups.forEach(tmpGroup -> { if (resolvedGroupSequence.contains(tmpGroup) && resolvedGroupSequence.indexOf(tmpGroup) < resolvedGroupSequence.size() - 1) { throw new GroupDefinitionException("Unable to expand group sequence."); } resolvedGroupSequence.add(tmpGroup); }); }
@Override public final GroupDefinitionException getCyclicDependencyInGroupsDefinitionException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getCyclicDependencyInGroupsDefinitionException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getUnableToExpandGroupSequenceException = "HV000048: Unable to expand group sequence.";
private <T> Set<ConstraintViolation<T>> validate(Set<Class<?>> previousGroups, T object, Class<?>... groups) { IReflector reflector = ReflectorFactory.INSTANCE.getReflector(object.getClass()); Set<ConstraintViolation<T>> violations = new HashSet<ConstraintViolation<T>>(); for(Class<?> group : reflector.getGroupSequence()) { if(previousGroups.contains(group)) { throw new GroupDefinitionException("The group sequence on " + object.getClass().getName() + " already contains " + group.getName() + ". Definitions may not be cyclic or circular"); } violations.addAll(this.coreValidator.validate(object, group)); previousGroups.add(group); if(!violations.isEmpty()) { break; } } return violations; }
@Override public final GroupDefinitionException getInvalidDefaultGroupSequenceDefinitionException() { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getInvalidDefaultGroupSequenceDefinitionException$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getNoDefaultGroupInGroupSequenceException = "HV000053: 'Default.class' cannot appear in default group sequence list.";
private void ensureExpandable(List<Group> groupList, List<Group> defaultGroupList, int defaultGroupIndex) { for (int i = 0; i < defaultGroupList.size(); i++) { Group group = defaultGroupList.get(i); if (group.isDefault()) { continue; // the default group is the one we want to replace } int index = groupList .indexOf(group); // sequence contains group of default group sequence if (index == -1) { continue; // if group is not in the sequence } if ((i == 0 && index == defaultGroupIndex - 1) || (i == defaultGroupList.size() - 1 && index == defaultGroupIndex + 1)) { // if we are at the beginning or end of he defaultGroupSequence and the // matches are either directly before or after we can continue, // since we basically have two groups continue; } throw new GroupDefinitionException("Unable to expand default group list" + defaultGroupList + " into sequence " + groupList); } }
@Override public final GroupDefinitionException getWrongDefaultGroupSequenceProviderTypeException(final Class<?> beanClass) { final GroupDefinitionException result = new GroupDefinitionException(String.format(getLoggingLocale(), getWrongDefaultGroupSequenceProviderTypeException$str(), new org.hibernate.validator.internal.util.logging.formatter.ClassObjectFormatter(beanClass))); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String getInvalidExecutableParameterIndexException = "HV000056: Method or constructor %1$s doesn't have a parameter with index %2$d.";